public class CommonCache<K, V>
extends Object
implements FlexibleCache, ValueConvertable, Serializable
Represents a simple key-value cache, which is NOT thread safe and backed by a Map instance
K - type of the keysV - type of the values| Modifiers | Name | Description |
|---|---|---|
static int |
DEFAULT_INITIAL_CAPACITY |
The default initial capacity |
static float |
DEFAULT_LOAD_FACTOR |
The default load factor |
| Constructor and description |
|---|
CommonCache()Constructs a cache with unlimited size |
CommonCache(int initialCapacity, int maxSize, EvictionStrategy evictionStrategy)Constructs a cache with limited size |
CommonCache(int initialCapacity, int maxSize)Constructs an LRU cache with the specified initial capacity and max size. |
CommonCache(int maxSize)Constructs an LRU cache with the default initial capacity |
CommonCache(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) |
|
public Object |
convertValue(V value){@inheritDoc} |
|
public Set<Entry<K, V>> |
entrySet() |
|
public V |
get(Object key){@inheritDoc} |
|
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) |
|
public boolean |
isEmpty() |
|
public Set<K> |
keySet() |
|
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) |
|
public V |
remove(Object key)Remove the cached value by the key
|
|
protected boolean |
removeEldestEntry(Map.Entry<K, V> eldest) |
|
public int |
size()Get the size of the cache
|
|
public String |
toString() |
|
public Collection<V> |
values()Get all cached values
|
The default initial capacity
The default load factor
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 org.codehaus.groovy.runtime.memoize.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
maxSize - max size of the LRU cacheInvoked 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.{@inheritDoc}
{@inheritDoc}
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 upvalueProvider - provide the value if the associated value not foundGet 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 associatedvalue - value to be associated with the specified keyRemove the cached value by the key
key - of the cached valueGet the size of the cache
Get all cached values
Copyright © 2003-2026 The Apache Software Foundation. All rights reserved.