Class StampedCommonCache<K,V> 
java.lang.Object
org.codehaus.groovy.runtime.memoize.StampedCommonCache<K,V> 
- Type Parameters:
- K- type of the keys
- V- 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 a 
Map instance.
 StampedCommonCache has better performance than ConcurrentCommonCache,
 but it is not reentrant, in other words, it may cause deadlock if getAndPut(Object, MemoizeCache.ValueProvider)
 or getAndPut(Object, MemoizeCache.ValueProvider, boolean) is called recursively:
 readlock -> upgrade to writelock -> readlock (fails to get and waits forever)- Since:
- 3.0.0
- See Also:
- 
Nested Class SummaryNested classes/interfaces inherited from interface org.codehaus.groovy.runtime.memoize.EvictableCacheEvictableCache.Action<K,V, R>, EvictableCache.EvictionStrategy Nested classes/interfaces inherited from interface java.util.MapMap.Entry<K extends Object,V extends Object> Nested classes/interfaces inherited from interface org.codehaus.groovy.runtime.memoize.MemoizeCacheMemoizeCache.ValueProvider<K,V> 
- 
Constructor SummaryConstructorsConstructorDescriptionConstructs a cache with unlimited sizeStampedCommonCache(int maxSize) Constructs an LRU cache with the default initial capacity(16)StampedCommonCache(int initialCapacity, int maxSize) Constructs an 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 specifiedMapinstance
- 
Method SummaryModifier and TypeMethodDescriptionvoidInvoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache.clearAll()Clear the cachebooleancontainsKey(Object key) Determines if the cache contains an entry for the specified key.booleancontainsValue(Object value) convertValue(V value) convert the original value to the target valueentrySet()Gets a value from the cachegetAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider) Try to get the value from cache.getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache) booleanisEmpty()keys()Get all keys associated to cached valueskeySet()Associates the specified value with the specified key in the cache.voidRemove the cached value by the keyintsize()Get the size of the cachevalues()Get all cached valuesMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.runtime.memoize.EvictableCacheclearMethods inherited from interface java.util.Mapcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
- 
Constructor Details- 
StampedCommonCachepublic StampedCommonCache()Constructs a cache with unlimited size
- 
StampedCommonCachepublic StampedCommonCache(int initialCapacity, int maxSize, EvictableCache.EvictionStrategy evictionStrategy) Constructs a cache with limited size- Parameters:
- initialCapacity- initial capacity of the cache
- maxSize- max size of the cache
- evictionStrategy- LRU or FIFO, see- EvictableCache.EvictionStrategy
 
- 
StampedCommonCachepublic StampedCommonCache(int initialCapacity, int maxSize) Constructs an LRU cache with the specified initial capacity and max size. The LRU cache is slower thanLRUCache- Parameters:
- initialCapacity- initial capacity of the LRU cache
- maxSize- max size of the LRU cache
 
- 
StampedCommonCachepublic StampedCommonCache(int maxSize) Constructs an LRU cache with the default initial capacity(16)- Parameters:
- maxSize- max size of the LRU cache
- See Also:
 
- 
StampedCommonCacheConstructs a cache backed by the specifiedMapinstance- Parameters:
- map- the- Mapinstance
 
 
- 
- 
Method Details- 
getGets a value from the cache
- 
putAssociates the specified value with the specified key in the cache.
- 
getAndPutTry to get the value from cache. If not found, create the value byMemoizeCache.ValueProviderand put it into the cache, at last return the value.- Specified by:
- getAndPutin interface- MemoizeCache<K,- V> 
- Parameters:
- key- the key to look up
- valueProvider- provide the value if the associated value not found
- Returns:
- the cached value
 
- 
getAndPutpublic V getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache) 
- 
valuesGet all cached values
- 
entrySet
- 
keysGet all keys associated to cached values- Specified by:
- keysin interface- EvictableCache<K,- V> 
- Returns:
- all keys
 
- 
containsKeyDetermines if the cache contains an entry for the specified key.- Specified by:
- containsKeyin interface- EvictableCache<K,- V> 
- Specified by:
- containsKeyin interface- Map<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- Specified by:
- containsValuein interface- Map<K,- V> 
 
- 
sizepublic int size()Get the size of the cache
- 
isEmptypublic boolean isEmpty()
- 
removeRemove the cached value by the key
- 
putAll
- 
keySet
- 
clearAllClear the cache- Specified by:
- clearAllin interface- EvictableCache<K,- V> 
- Returns:
- returns the content of the cleared map
 
- 
cleanUpNullReferencespublic 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:
- cleanUpNullReferencesin interface- MemoizeCache<K,- V> 
 
- 
convertValueconvert the original value to the target value- Specified by:
- convertValuein interface- ValueConvertable<K,- V> 
- Parameters:
- value- the original value
- Returns:
- the converted value
 
 
-