@ThreadSafe
public class StampedCommonCache<K, V>
extends Object
implements FlexibleCache, ValueConvertable, 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)
K - type of the keysV - type of the values| Constructor and description |
|---|
StampedCommonCache()Constructs a cache with unlimited size |
StampedCommonCache(int initialCapacity, int maxSize, EvictionStrategy evictionStrategy)Constructs a cache with limited size |
StampedCommonCache(int initialCapacity, int maxSize)Constructs an LRU cache with the specified initial capacity and max size. |
StampedCommonCache(int maxSize)Constructs an LRU cache with the default initial capacity(16) |
StampedCommonCache(Map<K, V> map)Constructs a cache backed by the specified Map instance |
| Type Params | Return Type | Name and description |
|---|---|---|
|
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. |
|
public Map<K, V> |
clearAll()* Clear the cache *
|
|
public boolean |
containsKey(Object key)* Determines if the cache contains an entry for the specified key. *
|
|
public boolean |
containsValue(Object value)Determines whether the cache contains the specified stored value. |
|
public Object |
convertValue(V value)Returns the stored value unchanged. |
|
public Set<Entry<K, V>> |
entrySet()Returns a live view of the cache entries. |
|
public V |
get(Object key)* Gets a value from the cache *
|
|
public V |
getAndPut(K key, ValueProvider<? super K, ? extends V> valueProvider)* Try to get the value from cache. * If not found, create the value by ValueProvider and put it into the cache, at last return the value. * *
|
|
public V |
getAndPut(K key, ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)* Returns the value associated with key, creating it with the
* supplied provider when necessary and optionally caching the result.
*
*
|
|
public boolean |
isEmpty()Returns whether the cache currently holds no entries. |
|
public Set<K> |
keySet()Returns a live view of the keys in this cache. |
|
public Set<K> |
keys()* Get all keys associated to cached values *
|
|
public V |
put(K key, V value)* Associates the specified value with the specified key in the cache. *
|
|
public void |
putAll(Map<? extends K, ? extends V> m)Copies all mappings from the supplied map into this cache. |
|
public V |
remove(Object key)* Remove the cached value by the key *
|
|
public int |
size()* Get the size of the cache *
|
|
public Collection<V> |
values()* Get all cached values *
|
Constructs a cache with unlimited size
Constructs a cache with limited size
initialCapacity - initial capacity of the cachemaxSize - max size of the cacheevictionStrategy - LRU or FIFO, see EvictableCache.EvictionStrategyConstructs an LRU cache with the specified initial capacity and max size. The LRU cache is slower than LRUCache
initialCapacity - initial capacity of the LRU cachemaxSize - max size of the LRU cacheConstructs an LRU cache with the default initial capacity(16)
maxSize - max size of the LRU cache* 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.
* Clear the cache *
* Determines if the cache contains an entry for the specified key. *
key - key whose presence in this cache is to be tested.
*Determines whether the cache contains the specified stored value.
value - the value whose presence should be testedtrue if the cache contains the valueReturns the stored value unchanged.
value - the stored valuevalueReturns a live view of the cache entries.
* Gets a value from the cache *
key - the key whose associated value is to be returned
** Try to get the value from cache. * If not found, create the value by ValueProvider and put it into the cache, at last return the value. * *
key - the key to look up
*valueProvider - provide the value if the associated value not found
*
* Returns the value associated with key, creating it with the
* supplied provider when necessary and optionally caching the result.
*
*
key - the key to look up
*valueProvider - supplies a value when the key is not cached
*shouldCache - whether a newly created value should be stored
*Returns whether the cache currently holds no entries.
true if the cache is emptyReturns a live view of the keys in this cache.
* Get all keys associated to cached values *
* Associates the specified value with the specified key in the cache. *
key - key with which the specified value is to be associated
*value - value to be associated with the specified key
*Copies all mappings from the supplied map into this cache.
m - the mappings to copy* Remove the cached value by the key *
key - of the cached value
** Get the size of the cache *
* Get all cached values *
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.