K
- The type for the keys in the cacheV
- The type for the values in the cachepublic class LRUHybridCache<K,V> extends Object implements Computable<K,HybridCacheEntry<V>>
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.Modifier and Type | Class and Description |
---|---|
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.
|
Constructor and 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.
|
Modifier and Type | Method and 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
matches
|
void |
remove(K key)
Remove given item from the cache.
|
int |
size()
Return the size of the cache
|
public LRUHybridCache(int maxCacheSize, Computable<K,HybridCacheEntry<V>> computable)
maxCacheSize
- The maximum number of entries in the cachecomputable
- The thing that can create the entrypublic LRUHybridCache(int maxCacheSize, Computable<K,HybridCacheEntry<V>> computable, LRUHybridCache.CycleHandler<K> cycleHandler)
maxCacheSize
- The maximum number of entries in the cachecomputable
- The thing that can create the entrycycleHandler
- What to do if a cycle is detectedpublic HybridCacheEntry<V> createCacheEntry(K k, V v, boolean dropMe)
k
- cache entry key.v
- cache entry value.dropMe
- should this entry be kept in the cache this must be set to false.public HybridCacheEntry<V> compute(K key)
Computable
compute
in interface Computable<K,HybridCacheEntry<V>>
key
- input data.public void clear()
public int size()
public int getMaximumCacheSize()
public boolean containsKey(K key)
key
- public void remove(K key)
key
- item key.public void releaseMatching(CacheKeyFilter<K> filter)
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 cacheCopyright © 2009-2017, Oracle and/or its affiliates. All Rights Reserved.