Class StampedCommonCache<K,V>
- java.lang.Object
-
- org.codehaus.groovy.runtime.memoize.StampedCommonCache<K,V>
-
- Type Parameters:
K
- type of the keysV
- type of the values
- All Implemented Interfaces:
Serializable
,Map<K,V>
,EvictableCache<K,V>
,MemoizeCache<K,V>
,ValueConvertable<V,Object>
@ThreadSafe public class StampedCommonCache<K,V> extends Object implements EvictableCache<K,V>, ValueConvertable<V,Object>, Serializable
Represents a simple key-value cache, which is thread safe and backed by aMap
instance. StampedCommonCache has better performance thanConcurrentCommonCache
, but it is not reentrant, in other words, it may cause deadlock ifgetAndPut(Object, MemoizeCache.ValueProvider)
orgetAndPut(Object, MemoizeCache.ValueProvider, boolean)
is called recursively: readlock -> upgrade to writelock -> readlock (fails to get and waits forever)- Since:
- 3.0.0
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.codehaus.groovy.runtime.memoize.EvictableCache
EvictableCache.Action<K,V,R>, EvictableCache.EvictionStrategy
-
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K extends Object,V extends Object>
-
Nested classes/interfaces inherited from interface org.codehaus.groovy.runtime.memoize.MemoizeCache
MemoizeCache.ValueProvider<K,V>
-
-
Constructor Summary
Constructors Constructor Description StampedCommonCache()
Constructs a cache with unlimited sizeStampedCommonCache(int maxSize)
Constructs a LRU cache with the default initial capacity(16)StampedCommonCache(int initialCapacity, int maxSize)
Constructs a LRU cache with the specified initial capacity and max size.StampedCommonCache(int initialCapacity, int maxSize, EvictableCache.EvictionStrategy evictionStrategy)
Constructs a cache with limited sizeStampedCommonCache(Map<K,V> map)
Constructs a cache backed by the specifiedMap
instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cleanUpNullReferences()
Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache.Map<K,V>
clearAll()
Clear the cacheboolean
containsKey(Object key)
Determines if the cache contains an entry for the specified key.boolean
containsValue(Object value)
Object
convertValue(V value)
convert the original value to the target valueSet<Map.Entry<K,V>>
entrySet()
V
get(Object key)
Gets a value from the cacheV
getAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider)
Try to get the value from cache.V
getAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider, boolean shouldCache)
boolean
isEmpty()
Set<K>
keys()
Get all keys associated to cached valuesSet<K>
keySet()
V
put(K key, V value)
Associates the specified value with the specified key in the cache.void
putAll(Map<? extends K,? extends V> m)
V
remove(Object key)
Remove the cached value by the keyint
size()
Get the size of the cacheCollection<V>
values()
Get all cached values-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.codehaus.groovy.runtime.memoize.EvictableCache
clear
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Constructor Detail
-
StampedCommonCache
public StampedCommonCache()
Constructs a cache with unlimited size
-
StampedCommonCache
public StampedCommonCache(int initialCapacity, int maxSize, EvictableCache.EvictionStrategy evictionStrategy)
Constructs a cache with limited size- Parameters:
initialCapacity
- initial capacity of the cachemaxSize
- max size of the cacheevictionStrategy
- LRU or FIFO, seeEvictableCache.EvictionStrategy
-
StampedCommonCache
public StampedCommonCache(int initialCapacity, int maxSize)
Constructs a LRU cache with the specified initial capacity and max size. The LRU cache is slower thanLRUCache
- Parameters:
initialCapacity
- initial capacity of the LRU cachemaxSize
- max size of the LRU cache
-
StampedCommonCache
public StampedCommonCache(int maxSize)
Constructs a LRU cache with the default initial capacity(16)- Parameters:
maxSize
- max size of the LRU cache- See Also:
StampedCommonCache(int, int)
-
-
Method Detail
-
put
public V put(K key, V value)
Associates the specified value with the specified key in the cache.
-
getAndPut
public V getAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider)
Try to get the value from cache. If not found, create the value byMemoizeCache.ValueProvider
and put it into the cache, at last return the value.- Specified by:
getAndPut
in interfaceMemoizeCache<K,V>
valueProvider
- provide the value if the associated value not found- Returns:
- the cached value
-
getAndPut
public V getAndPut(K key, MemoizeCache.ValueProvider<? super K,? extends V> valueProvider, boolean shouldCache)
-
values
public Collection<V> values()
Get all cached values
-
keys
public Set<K> keys()
Get all keys associated to cached values- Specified by:
keys
in interfaceEvictableCache<K,V>
- Returns:
- all keys
-
containsKey
public boolean containsKey(Object key)
Determines if the cache contains an entry for the specified key.- Specified by:
containsKey
in interfaceEvictableCache<K,V>
- Specified by:
containsKey
in interfaceMap<K,V>
- Parameters:
key
- key whose presence in this cache is to be tested.- Returns:
- true if the cache contains a mapping for the specified key
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
-
size
public int size()
Get the size of the cache
-
clearAll
public Map<K,V> clearAll()
Clear the cache- Specified by:
clearAll
in interfaceEvictableCache<K,V>
- Returns:
- returns the content of the cleared map
-
cleanUpNullReferences
public void cleanUpNullReferences()
Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache. The implementation must ensure that concurrent invocations of all methods on the cache may occur from other threads and thus should protect any shared resources.- Specified by:
cleanUpNullReferences
in interfaceMemoizeCache<K,V>
-
convertValue
public Object convertValue(V value)
convert the original value to the target value- Specified by:
convertValue
in interfaceValueConvertable<K,V>
- Parameters:
value
- the original value- Returns:
- the converted value
-
-