Module org.glassfish.hk2.utilities
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>>
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 with
HybridCacheEntry
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.- Author:
- Jakub Podlesak (jakub.podlesak at oracle.com)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceShould 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. -
Constructor Summary
ConstructorsConstructorDescriptionLRUHybridCache(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
Modifier and TypeMethodDescriptionvoidclear()Empty the cache.Defines an expensive computation to retrieve value V from key K.booleancontainsKey(K key) Returns true if the key has already been cached.createCacheEntry(K k, V v, boolean dropMe) Create cache entry for given values.intvoidreleaseMatching(CacheKeyFilter<K> filter) This method will remove all cache entries for which this filter matchesvoidRemove given item from the cache.intsize()Return the size of the cache
-
Constructor Details
-
LRUHybridCache
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 Details
-
createCacheEntry
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
Description copied from interface:ComputableDefines an expensive computation to retrieve value V from key K.- Specified by:
computein 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
Returns true if the key has already been cached.- Parameters:
key-- Returns:
- true if given key is present in the cache.
-
remove
Remove given item from the cache.- Parameters:
key- item key.
-
releaseMatching
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
-