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.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static class
static interface
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 final class
private class
Helper class, that remembers the future task origin thread, so that cycles could be detected. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final ConcurrentHashMap
<K, LRUHybridCache<K, V>.OriginThreadAwareFuture> private static final Comparator
<LRUHybridCache.OriginThreadAwareFuture> private final Computable
<K, HybridCacheEntry<V>> private final LRUHybridCache.CycleHandler
<K> private static final LRUHybridCache.CycleHandler
<Object> private final int
private final Object
-
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 TypeMethodDescriptionvoid
clear()
Empty the cache.Defines an expensive computation to retrieve value V from key K.boolean
containsKey
(K key) Returns true if the key has already been cached.createCacheEntry
(K k, V v, boolean dropMe) Create cache entry for given values.int
void
releaseMatching
(CacheKeyFilter<K> filter) This method will remove all cache entries for which this filter matchesvoid
Remove given item from the cache.private void
Remove least recently used item form the cache.int
size()
Return the size of the cache
-
Field Details
-
cycleHandler
-
EMPTY_CYCLE_HANDLER
-
cache
-
computable
-
prunningLock
-
maxCacheSize
private final int maxCacheSize -
COMPARATOR
-
-
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: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
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.
-
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
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
-