1 28 29 package com.opencms.template.cache; 30 31 import java.util.Vector ; 32 33 45 46 public class CmsLruCache { 47 48 private static final boolean C_DEBUG = false; 50 51 private CacheItem[] m_cache; 53 54 private int m_maxSize; 56 57 private int m_size = 0; 59 60 private CacheItem head; 62 63 private CacheItem tail; 65 66 static class CacheItem { 67 Object key; 68 Object value; 69 70 CacheItem chain; 72 73 CacheItem previous; 75 CacheItem next; 76 } 77 78 82 public CmsLruCache(int size) { 83 if(C_DEBUG){ 84 System.err.println("--LruCache started with "+size); 85 } 86 m_cache = new CacheItem[size]; 87 m_maxSize = size; 88 } 89 90 96 public synchronized Vector put (Object key, Object value){ 97 98 int hashIndex = (key.hashCode() & 0x7FFFFFFF) % m_maxSize; 99 CacheItem item = m_cache[hashIndex]; 100 CacheItem newItem = null; 101 Vector returnValue = null; 102 if(C_DEBUG){ 103 System.err.println("put in cache: "+key); 104 } 105 if(item != null){ 106 while(item.chain != null){ 109 if(item.key.equals(key)){ 110 item.value = value; 111 return null; 113 } 114 item = item.chain; 115 } 116 if(item.key.equals(key)){ 117 item.value = value; 118 return null; 120 } 121 if(m_size >= m_maxSize){ 122 CacheItem helper = head.next; 124 if (item == head){ 125 newItem = item; 126 returnValue = new Vector (2); 127 returnValue.add(0, item.key); 128 returnValue.add(1, item.value); 129 }else{ 130 newItem = head; 131 returnValue = new Vector (2); 132 returnValue.add(0, head.key); 133 returnValue.add(1, head.value); 134 removeFromTable(head); 135 newItem.chain = null; 136 item.chain = newItem; 137 } 138 newItem.next = null; 139 head = helper; 140 head.previous = null; 141 }else{ 142 m_size++; 143 newItem = new CacheItem(); 144 item.chain = newItem; 145 } 146 }else{ 147 if(head != null){ 149 if(m_size >= m_maxSize){ 150 CacheItem helper = head.next; 152 newItem = head; 153 returnValue = new Vector (2); 154 returnValue.add(0, head.key); 155 returnValue.add(1, head.value); 156 removeFromTable(head); 157 newItem.next = null; 158 newItem.chain = null; 159 head = helper; 160 head.previous = null; 161 }else{ 162 m_size++; 163 newItem = new CacheItem(); 164 } 165 }else{ 166 newItem = new CacheItem(); 168 m_size++; 169 head = newItem; 170 tail = newItem; 171 } 172 item = m_cache[hashIndex] = newItem; 173 } 174 newItem.key = key; 176 newItem.value = value; 177 if(tail != newItem){ 178 tail.next = newItem; 179 newItem.previous = tail; 180 tail = newItem; 181 } 182 return returnValue; 183 } 184 185 191 public synchronized Object get(Object key){ 192 int hashIndex = (key.hashCode() & 0x7FFFFFFF) % m_maxSize; 193 CacheItem item = m_cache[hashIndex]; 194 if(C_DEBUG){ 195 System.err.println("get from Cache: "+key); 196 } 198 while (item != null){ 199 if(item.key.equals(key)){ 200 if(item != tail){ 202 if(item != head){ 204 item.previous.next = item.next; 205 }else{ 206 head = head.next; 207 } 208 item.next.previous = item.previous; 209 tail.next = item; 210 item.previous = tail; 211 tail = item; 212 tail.next = null; 213 } 214 return item.value; 215 } 216 item = item.chain; 217 } 218 if(C_DEBUG){ 219 System.err.println(" not found in Cache!!!!"); 220 } 221 return null; 222 } 223 224 228 public void remove(Object key){ 229 if(key != null){ 230 int hashIndex = (key.hashCode() & 0x7FFFFFFF) % m_maxSize; 231 CacheItem item = m_cache[hashIndex]; 232 while (item != null){ 233 if(item.key.equals(key)){ 234 removeItem(item); 236 } 237 item = item.chain; 238 } 239 } 240 } 241 242 246 private void removeFromTable(CacheItem oldItem){ 247 if(C_DEBUG){ 248 System.err.println(" --remove from chaincache: "+oldItem.key); 249 } 250 int hashIndex = ((oldItem.key).hashCode() & 0x7FFFFFFF) % m_maxSize; 251 CacheItem item = m_cache[hashIndex]; 252 if(item == oldItem){ 253 m_cache[hashIndex] = item.chain; 254 }else{ 255 if(item != null){ 256 while(item.chain != null){ 257 if(item.chain == oldItem){ 258 item.chain = item.chain.chain; 259 return; 260 } 261 item = item.chain; 262 } 263 } 264 } 265 } 266 267 270 private synchronized void removeItem(CacheItem item){ 271 272 if(C_DEBUG){ 273 System.err.println("--remove item from cache: "+item.key); 274 } 275 if(m_size < 2){ 276 clearCache(); 277 }else{ 278 removeFromTable(item); 280 if((item != head) && (item != tail)){ 282 item.previous.next = item.next; 283 item.next.previous = item.previous; 284 }else{ 285 if(item == head){ 286 head = item.next; 287 head.previous = null; 288 } 289 if (item == tail){ 290 tail = item.previous; 291 tail.next = null; 292 } 293 } 294 m_size--; 295 } 296 } 297 298 304 public synchronized Vector deleteElementsByTemplate(String templateName){ 305 CacheItem item = head; 306 Vector ret = new Vector (); 307 while (item != null){ 308 if(templateName.equals(((CmsElementDescriptor)item.key).getTemplateName())){ 309 Vector actItem = new Vector (); 310 actItem.add(0, item.key); 311 actItem.add(1, item.value); 312 ret.add(actItem); 313 removeItem(item); 314 } 315 item = item.next; 316 } 317 return ret; 318 } 319 320 325 public synchronized Vector deleteElementsByClass(String className){ 326 CacheItem item = head; 327 Vector ret = new Vector (); 328 while (item != null){ 329 if(className.equals(((CmsElementDescriptor)item.key).getClassName())){ 330 Vector actItem = new Vector (); 331 actItem.add(0, item.key); 332 actItem.add(1, item.value); 333 ret.add(actItem); 334 removeItem(item); 335 } 336 item = item.next; 337 } 338 return ret; 339 } 340 341 347 public synchronized Vector deleteElementsAfterPublish(){ 348 CacheItem item = head; 349 Vector ret = new Vector (); 350 while (item != null){ 351 try{ 352 if (((A_CmsElement)item.value).getCacheDirectives().shouldRenew()){ 353 Vector actItem = new Vector (); 354 actItem.add(0, item.key); 355 actItem.add(1, item.value); 356 ret.add(actItem); 357 removeItem(item); 358 } 359 }catch(NullPointerException e){ 360 Vector actItem = new Vector (); 362 actItem.add(0, item.key); 363 actItem.add(1, item.value); 364 ret.add(actItem); 365 removeItem(item); 366 } 367 item = item.next; 368 } 369 return ret; 370 } 371 372 376 public synchronized void deleteUri(String uri){ 377 CacheItem item = head; 378 while (item != null){ 379 if(uri.equals(((CmsUriDescriptor)item.key).getKey())){ 380 removeItem(item); 381 return; 383 } 384 item = item.next; 385 } 386 } 387 388 391 public synchronized void clearCache(){ 392 if(C_DEBUG){ 393 System.err.println("--LruCache restarted"); 394 } 395 m_cache = new CacheItem[m_maxSize]; 396 m_size = 0; 397 head = null; 398 tail = null; 399 } 400 401 406 public Vector getCacheInfo(){ 407 408 if(C_DEBUG){ 409 System.err.println("...Cache size should be:"+ m_size + " by a max size of "+m_maxSize); 410 int count = 0; 411 CacheItem item = head; 412 while (item != null){ 413 count++; 414 item = item.next; 415 } 416 System.err.println("...Cache size is:"+count); 417 } 418 Vector info = new Vector (); 419 info.addElement(new Integer (m_maxSize )); 420 info.addElement(new Integer (m_size)); 421 return info; 422 } 423 424 428 public synchronized Vector getAllKeys(){ 429 Vector ret = new Vector (); 430 CacheItem item = head; 431 while (item != null){ 432 ret.add(item.key); 433 item = item.next; 434 } 435 return ret; 436 } 437 438 483 }
| Popular Tags
|