Package org.jboss.jca.adapters.jdbc.util
Class LRUCache<K,V>
- java.lang.Object
-
- org.jboss.jca.adapters.jdbc.util.LRUCache<K,V>
-
- Type Parameters:
K- The keyV- The valueK- The keyV- The value
- All Implemented Interfaces:
Cache<K,V>
public class LRUCache<K,V> extends Object implements Cache<K,V>
Implementation of a Least Recently Used cache policy.- Author:
- Simone Bordet
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classLRUCache.LRUCacheEntry<K,V>Double linked cell used as entry in the cache list.classLRUCache.LRUListDouble queued list used to store cache entries.
-
Constructor Summary
Constructors Constructor Description LRUCache(int max)Creates a LRU cache
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidflush()Flushes the cached objects from the cache.Vget(K key)Returns the object paired with the specified key if it's present in the cache, otherwise must return null.voidinsert(K key, V o)Inserts the specified object into the cache following the implemented policy.Vpeek(K key)Returns the object paired with the specified key if it's present in the cache, otherwise must return null.voidremove(K key)Remove the cached object paired with the specified key.voidsetListener(CacheListener listener)Set the cache listenerintsize()The cache size
-
-
-
Method Detail
-
get
public V get(K key)
Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
Implementations of this method must have complexity of order O(1). Differently fromCache.peek(K)this method not only return whether the object is present in the cache or not, but also applies the implemented policy that will "refresh" the cached object in the cache, because this cached object was really requested.- Specified by:
getin interfaceCache<K,V>- Parameters:
key- the key paired with the object- Returns:
- the object
- See Also:
Cache.peek(K)
-
peek
public V peek(K key)
Returns the object paired with the specified key if it's present in the cache, otherwise must return null.
Implementations of this method must have complexity of order O(1). This method should not apply the implemented caching policy to the object paired with the given key, so that a client can query if an object is cached without "refresh" its cache status. Real requests for the object must be done usingCache.get(K).- Specified by:
peekin interfaceCache<K,V>- Parameters:
key- the key paired with the object- Returns:
- the object
- See Also:
Cache.get(K)
-
insert
public void insert(K key, V o)
Inserts the specified object into the cache following the implemented policy.
Implementations of this method must have complexity of order O(1).- Specified by:
insertin interfaceCache<K,V>- Parameters:
key- the key paired with the objecto- the object to cache- See Also:
Cache.remove(K)
-
remove
public void remove(K key)
Remove the cached object paired with the specified key.
Implementations of this method must have complexity of order O(1).- Specified by:
removein interfaceCache<K,V>- Parameters:
key- the key paired with the object- See Also:
Cache.insert(K, V)
-
flush
public void flush()
Flushes the cached objects from the cache.
-
size
public int size()
The cache size
-
setListener
public void setListener(CacheListener listener)
Set the cache listener- Specified by:
setListenerin interfaceCache<K,V>- Parameters:
listener- The listener
-
-