Sets the maximum number of elements in the cache. If 0, there is no maximum. @param maxSize The maximum number of elements in the cache
(long maxSize)
| 312 | * The maximum number of elements in the cache |
| 313 | */ |
| 314 | public void setMaxSize(long maxSize) { |
| 315 | // if the new maxSize is <= 0, clear keyLRUList |
| 316 | if (maxSize <= 0) { |
| 317 | keyLRUList.clear(); |
| 318 | } else if (maxSize > 0 && this.maxSize <= 0) { |
| 319 | // if the new maxSize > 0 and the old is <= 0, fill in LRU list - |
| 320 | // order will be meaningless for now |
| 321 | Iterator keys = cacheLineTable.keySet().iterator(); |
| 322 | |
| 323 | while (keys.hasNext()) { |
| 324 | keyLRUList.add(keys.next()); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // if the new maxSize is less than the current cache size, shrink the |
| 329 | // cache. |
| 330 | if (maxSize > 0 && cacheLineTable.size() > maxSize) { |
| 331 | while (cacheLineTable.size() > maxSize) { |
| 332 | Object lastKey = keyLRUList.getLast(); |
| 333 | |
| 334 | removeObject(lastKey); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | this.maxSize = maxSize; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Returns the current maximum number of elements in the cache |