jodd.cache
Class LRUCache<K,V>

java.lang.Object
  extended by jodd.cache.AbstractCacheMap<K,V>
      extended by jodd.cache.LRUCache<K,V>
All Implemented Interfaces:
Cache<K,V>

public class LRUCache<K,V>
extends AbstractCacheMap<K,V>

LRU (least recently used) cache.

Items are added to the cache as they are accessed; when the cache is full, the least recently used item is ejected. This type of cache is typically implemented as a linked list, so that an item in cache, when it is accessed again, can be moved back up to the head of the queue; items are ejected from the tail of the queue. Cache access overhead is again constant time. This algorithm is simple and fast, and it has a significant advantage over FIFO in being able to adapt somewhat to the data access pattern; frequently used items are less likely to be ejected from the cache. The main disadvantage is that it can still get filled up with items that are unlikely to be reaccessed soon; in particular, it can become useless in the face of scanning type accesses. Nonetheless, this is by far the most frequently used caching algorithm.

Implementation note: unfortunately, it was not possible to have onRemove callback method, since LinkedHashMap has its removal methods private.

Summary for LRU: fast, adaptive, not scan resistant.


Field Summary
 
Fields inherited from class jodd.cache.AbstractCacheMap
cacheLock, cacheMap, cacheSize, existCustomTimeout, timeout
 
Constructor Summary
LRUCache(int cacheSize)
           
LRUCache(int cacheSize, long timeout)
          Creates a new LRU cache.
 
Method Summary
protected  int pruneCache()
          Prune only expired objects, LinkedHashMap will take care of LRU if needed.
protected  boolean removeEldestEntry(int currentSize)
          Removes the eldest entry if current cache size exceed cache size.
 
Methods inherited from class jodd.cache.AbstractCacheMap
clear, get, getCacheSize, getCacheTimeout, isEmpty, isFull, isPruneExpiredActive, iterator, prune, put, put, remove, size
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LRUCache

public LRUCache(int cacheSize)

LRUCache

public LRUCache(int cacheSize,
                long timeout)
Creates a new LRU cache.

Method Detail

removeEldestEntry

protected boolean removeEldestEntry(int currentSize)
Removes the eldest entry if current cache size exceed cache size.


pruneCache

protected int pruneCache()
Prune only expired objects, LinkedHashMap will take care of LRU if needed.

Specified by:
pruneCache in class AbstractCacheMap<K,V>


Copyright © 2003-2012 Jodd Team