Class LRUCacheCheapRead<K,V>
- java.lang.Object
-
- org.glassfish.hk2.utilities.cache.LRUCache<K,V>
-
- org.glassfish.hk2.utilities.cache.internal.LRUCacheCheapRead<K,V>
-
- Type Parameters:
K
- The key of the cacheV
- The values in the cache
public class LRUCacheCheapRead<K,V> extends LRUCache<K,V>
LRU Cache implementation that relies on entries that keep last hit (get/put) timestamp in order to be able to remove least recently accessed items when running out of cache capacity. Item order is not being maintained during regular cache usage (mainly reads). This makes pruning operation expensive in exchange for making reads quite cheap in a multi-threaded environment.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
LRUCacheCheapRead.CacheEntryImpl<K,V>
private static class
LRUCacheCheapRead.CacheEntryImplComparator
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<K,LRUCacheCheapRead.CacheEntryImpl<K,V>>
cache
private static LRUCacheCheapRead.CacheEntryImplComparator
COMPARATOR
(package private) int
maxCacheSize
(package private) java.lang.Object
prunningLock
-
Constructor Summary
Constructors Constructor Description LRUCacheCheapRead(int maxCacheSize)
Create new cache with given maximum capacity.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description V
get(K key)
Returns the value associated with the given key.int
getMaxCacheSize()
Returns the maximum number of entries that will be stored in this cacheCacheEntry
put(K key, V value)
Adds the given key and value pair into the cachevoid
releaseCache()
Clears all entries in the cache, for use when a known event makes the cache incorrectvoid
releaseMatching(CacheKeyFilter<K> filter)
This method will remove all cache entries for which this filter matchesprivate void
removeLRUItem()
Remove least recently used item form the cache.-
Methods inherited from class org.glassfish.hk2.utilities.cache.LRUCache
createCache
-
-
-
-
Field Detail
-
prunningLock
final java.lang.Object prunningLock
-
maxCacheSize
final int maxCacheSize
-
cache
java.util.Map<K,LRUCacheCheapRead.CacheEntryImpl<K,V>> cache
-
COMPARATOR
private static final LRUCacheCheapRead.CacheEntryImplComparator COMPARATOR
-
-
Method Detail
-
get
public V get(K key)
Description copied from class:LRUCache
Returns the value associated with the given key. If there is no value, returns null
-
put
public CacheEntry put(K key, V value)
Description copied from class:LRUCache
Adds the given key and value pair into the cache
-
releaseCache
public void releaseCache()
Description copied from class:LRUCache
Clears all entries in the cache, for use when a known event makes the cache incorrect- Specified by:
releaseCache
in classLRUCache<K,V>
-
getMaxCacheSize
public int getMaxCacheSize()
Description copied from class:LRUCache
Returns the maximum number of entries that will be stored in this cache- Specified by:
getMaxCacheSize
in classLRUCache<K,V>
- Returns:
- The maximum number of entries that will be stored in this cache
-
releaseMatching
public void releaseMatching(CacheKeyFilter<K> filter)
Description copied from class:LRUCache
This method will remove all cache entries for which this filter matches- Specified by:
releaseMatching
in classLRUCache<K,V>
- Parameters:
filter
- Entries in the cache that match this filter will be removed from the cache. If filter is null nothing will be removed from the cache
-
removeLRUItem
private void removeLRUItem()
Remove least recently used item form the cache. No checks are done here. The method just tries to remove the least recently used cache item. An exception will be thrown if the cache is empty.
-
-