Class LRUHybridCache<K,V>
- java.lang.Object
-
- org.glassfish.hk2.utilities.cache.LRUHybridCache<K,V>
-
- Type Parameters:
K
- The type for the keys in the cacheV
- The type for the values in the cache
- All Implemented Interfaces:
Computable<K,HybridCacheEntry<V>>
public class LRUHybridCache<K,V> extends java.lang.Object implements Computable<K,HybridCacheEntry<V>>
Hybrid cache that allows explicit removals of included entries as well as implicit removal of entries that have been least recently accessed. The implicit removal happens only in case the internal cache runs out of space. Maximum number of items that can be kept in the cache is invariant for a single cache instance, and can be set in the cache constructor. The cache deals withHybridCacheEntry
items than could even instruct the cache to drop them as they get computed. The cache will still make sure such items get computed just once in given time (based on computation length) for given key. Desired value will only be computed once and computed value stored in the cache. The implementation is based on an example from the "Java Concurrency in Practice" book authored by Brian Goetz and company.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
LRUHybridCache.CacheEntryImplComparator<K,V>
static interface
LRUHybridCache.CycleHandler<K>
Should a cycle be detected during computation of a value for given key, this interface allows client code to register a callback that would get invoked in such a case.private class
LRUHybridCache.HybridCacheEntryImpl<V1>
private class
LRUHybridCache.OriginThreadAwareFuture
Helper class, that remembers the future task origin thread, so that cycles could be detected.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ConcurrentHashMap<K,LRUHybridCache.OriginThreadAwareFuture>
cache
private static java.util.Comparator<LRUHybridCache.OriginThreadAwareFuture>
COMPARATOR
private Computable<K,HybridCacheEntry<V>>
computable
private LRUHybridCache.CycleHandler<K>
cycleHandler
private static LRUHybridCache.CycleHandler<java.lang.Object>
EMPTY_CYCLE_HANDLER
private int
maxCacheSize
private java.lang.Object
prunningLock
-
Constructor Summary
Constructors Constructor Description LRUHybridCache(int maxCacheSize, Computable<K,HybridCacheEntry<V>> computable)
Create new cache with given computable to compute values.LRUHybridCache(int maxCacheSize, Computable<K,HybridCacheEntry<V>> computable, LRUHybridCache.CycleHandler<K> cycleHandler)
Create new cache with given computable and cycleHandler.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Empty the cache.HybridCacheEntry<V>
compute(K key)
Defines an expensive computation to retrieve value V from key K.boolean
containsKey(K key)
Returns true if the key has already been cached.HybridCacheEntry<V>
createCacheEntry(K k, V v, boolean dropMe)
Create cache entry for given values.int
getMaximumCacheSize()
void
releaseMatching(CacheKeyFilter<K> filter)
This method will remove all cache entries for which this filter matchesvoid
remove(K key)
Remove given item from the cache.private void
removeLRUItem()
Remove least recently used item form the cache.int
size()
Return the size of the cache
-
-
-
Field Detail
-
cycleHandler
private final LRUHybridCache.CycleHandler<K> cycleHandler
-
EMPTY_CYCLE_HANDLER
private static final LRUHybridCache.CycleHandler<java.lang.Object> EMPTY_CYCLE_HANDLER
-
cache
private final java.util.concurrent.ConcurrentHashMap<K,LRUHybridCache.OriginThreadAwareFuture> cache
-
computable
private final Computable<K,HybridCacheEntry<V>> computable
-
prunningLock
private final java.lang.Object prunningLock
-
maxCacheSize
private final int maxCacheSize
-
COMPARATOR
private static final java.util.Comparator<LRUHybridCache.OriginThreadAwareFuture> COMPARATOR
-
-
Constructor Detail
-
LRUHybridCache
public LRUHybridCache(int maxCacheSize, Computable<K,HybridCacheEntry<V>> computable)
Create new cache with given computable to compute values.- Parameters:
maxCacheSize
- The maximum number of entries in the cachecomputable
- The thing that can create the entry
-
LRUHybridCache
public LRUHybridCache(int maxCacheSize, Computable<K,HybridCacheEntry<V>> computable, LRUHybridCache.CycleHandler<K> cycleHandler)
Create new cache with given computable and cycleHandler.- Parameters:
maxCacheSize
- The maximum number of entries in the cachecomputable
- The thing that can create the entrycycleHandler
- What to do if a cycle is detected
-
-
Method Detail
-
createCacheEntry
public HybridCacheEntry<V> createCacheEntry(K k, V v, boolean dropMe)
Create cache entry for given values.- Parameters:
k
- cache entry key.v
- cache entry value.dropMe
- should this entry be kept in the cache this must be set to false.- Returns:
- an instance of cache entry.
-
compute
public HybridCacheEntry<V> compute(K key)
Description copied from interface:Computable
Defines an expensive computation to retrieve value V from key K.- Specified by:
compute
in interfaceComputable<K,V>
- Parameters:
key
- input data.- Returns:
- output from the computation.
-
clear
public void clear()
Empty the cache.
-
size
public int size()
Return the size of the cache- Returns:
- The current size of the cache
-
getMaximumCacheSize
public int getMaximumCacheSize()
-
containsKey
public boolean containsKey(K key)
Returns true if the key has already been cached.- Parameters:
key
-- Returns:
- true if given key is present in the cache.
-
remove
public void remove(K key)
Remove given item from the cache.- Parameters:
key
- item key.
-
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.
-
releaseMatching
public void releaseMatching(CacheKeyFilter<K> filter)
This method will remove all cache entries for which this filter matches- 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
-
-