Top Description Fields Constructors Methods
org.codehaus.groovy.runtime.memoize

public Class StampedCommonCache<K, V>

extends Object
implements EvictableCache<K, V>, ValueConvertable<V, Object>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.codehaus.groovy.runtime.memoize.ValueConvertable, org.codehaus.groovy.runtime.memoize.EvictableCache, java.util.Map, org.codehaus.groovy.runtime.memoize.MemoizeCache
Annotations
@ThreadSafe
Type Parameters
<K>
type of the keys
<V>
type of the values
Imports
javax.annotation.concurrent.ThreadSafe, java.io.Serializable, java.util.Collection, .Map, .Set, java.util.concurrent.locks.StampedLock

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

Field Summary

Modifier and TypeField and Description
private final CommonCache<K, V>
private static final long
private final StampedLock

Constructor Summary

AccessConstructor and Description
public
StampedCommonCache()

Constructs a cache with unlimited size

public
StampedCommonCache(int
initial capacity of the cache
initialCapacity
,
int
max size of the cache
maxSize
,
EvictableCache.EvictionStrategy evictionStrategy)

Constructs a cache with limited size

public
StampedCommonCache(int
initial capacity of the LRU cache
initialCapacity
,
int
max size of the LRU cache
maxSize
)

Constructs a LRU cache with the specified initial capacity and max size.

public
StampedCommonCache(int
max size of the LRU cache
maxSize
)

Constructs a LRU cache with the default initial capacity(16)

public
StampedCommonCache(Map<K, V>
the Map instance
map
)

Constructs a cache backed by the specified Map instance

Method Summary

Modifier and TypeMethod and Description
public void
cleanUpNullReferences()

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.cleanUpNullReferences.

Invoked when some of the held SoftReferences have been evicted by the garbage collector and so should be removed from the cache.
public Map<K, V>
private V
compute(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)

public boolean
containsKey(final Object
key whose presence in this cache is to be tested.
key
)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.containsKey.

Determines if the cache contains an entry for the specified key.
public boolean
containsValue(Object
value whose presence in this map is to be tested
value
)

Implements java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value.
public Object
convertValue(V
the original value
value
)

Implements org.codehaus.groovy.runtime.memoize.ValueConvertable.convertValue.

convert the original value to the target value
private <R> R
doWithReadLock(EvictableCache.Action<K, V, R>
the content to complete
action
)

deal with the backed cache guarded by read lock

private <R> R
doWithWriteLock(EvictableCache.Action<K, V, R>
the content to complete
action
)

deal with the backed cache guarded by write lock

public Set<Map.Entry<K, V>>
entrySet()

Implements java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map.
public V
get(final Object
the key whose associated value is to be returned
key
)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.get, java.util.Map.get.

Gets a value from the cache
public V
getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V>
provide the value if the associated value not found
valueProvider
)

Overrides default org.codehaus.groovy.runtime.memoize.MemoizeCache.getAndPut.

Try to get the value from cache.
public V
getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)

public boolean
isEmpty()

Implements java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.
public Set<K>
keys()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.keys.

Get all keys associated to cached values
public Set<K>
keySet()

Implements java.util.Map.keySet.

Returns a Set view of the keys contained in this map.
public V
put(final K
key with which the specified value is to be associated
key
,
final V
value to be associated with the specified key
value
)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.put, java.util.Map.put.

Associates the specified value with the specified key in the cache.
public void
putAll(Map<? extends K, ? extends V>
mappings to be stored in this map
m
)

Implements java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation).
public V
remove(final Object
of the cached value
key
)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.remove.

Remove the cached value by the key
public int
size()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.size.

Get the size of the cache
public Collection<V>
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

commonCacheback to summary
private final CommonCache<K, V> commonCache
serialVersionUIDback to summary
private static final long serialVersionUID
slback to summary
private final StampedLock sl

Constructor Detail

StampedCommonCacheback to summary
public StampedCommonCache()

Constructs a cache with unlimited size

StampedCommonCacheback to summary
public StampedCommonCache(int initialCapacity, int maxSize, EvictableCache.EvictionStrategy evictionStrategy)

Constructs a cache with limited size

Parameters
initialCapacity:int

initial capacity of the cache

maxSize:int

max size of the cache

evictionStrategy:EvictableCache.EvictionStrategy

LRU or FIFO, see EvictableCache.EvictionStrategy

StampedCommonCacheback to summary
public StampedCommonCache(int initialCapacity, int maxSize)

Constructs a LRU cache with the specified initial capacity and max size. The LRU cache is slower than LRUCache

Parameters
initialCapacity:int

initial capacity of the LRU cache

maxSize:int

max size of the LRU cache

StampedCommonCacheback to summary
public StampedCommonCache(int maxSize)

Constructs a LRU cache with the default initial capacity(16)

Parameters
maxSize:int

max size of the LRU cache

See Also
StampedCommonCache(int, int)
StampedCommonCacheback to summary
public StampedCommonCache(Map<K, V> map)

Constructs a cache backed by the specified Map instance

Parameters
map:Map<K, V>

the Map instance

Method Detail

cleanUpNullReferencesback to summary
public void cleanUpNullReferences()

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.cleanUpNullReferences.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.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.

Annotations
@Override
clearAllback to summary
public Map<K, V> clearAll()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.clearAll.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.clearAll.

Clear the cache

Returns:Map<K, V>

returns the content of the cleared map

Annotations
@Override
computeback to summary
private V compute(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)
containsKeyback to summary
public boolean containsKey(final Object key)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.containsKey.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.containsKey.

Determines if the cache contains an entry for the specified key.

Parameters
key:Object

key whose presence in this cache is to be tested.

Returns:boolean

true if the cache contains a mapping for the specified key

Annotations
@Override
containsValueback to summary
public boolean containsValue(Object value)

Implements java.util.Map.containsValue.

Doc from java.util.Map.containsValue.

Returns true if this map maps one or more keys to the specified value. More formally, returns true if and only if this map contains at least one mapping to a value v such that Objects.equals(value, v). This operation will probably require time linear in the map size for most implementations of the Map interface.

Parameters
value:Object

value whose presence in this map is to be tested

Returns:boolean

true if this map maps one or more keys to the specified value

Annotations
@Override
convertValueback to summary
public Object convertValue(V value)

Implements org.codehaus.groovy.runtime.memoize.ValueConvertable.convertValue.

Doc from org.codehaus.groovy.runtime.memoize.ValueConvertable.convertValue.

convert the original value to the target value

Parameters
value:V

the original value

Returns:Object

the converted value

Annotations
@Override
doWithReadLockback to summary
private <R> R doWithReadLock(EvictableCache.Action<K, V, R> action)

deal with the backed cache guarded by read lock

Parameters
action:EvictableCache.Action<K, V, R>

the content to complete

doWithWriteLockback to summary
private <R> R doWithWriteLock(EvictableCache.Action<K, V, R> action)

deal with the backed cache guarded by write lock

Parameters
action:EvictableCache.Action<K, V, R>

the content to complete

entrySetback to summary
public Set<Map.Entry<K, V>> entrySet()

Implements java.util.Map.entrySet.

Doc from java.util.Map.entrySet.

Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation, or through the setValue operation on a map entry returned by the iterator) the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll and clear operations. It does not support the add or addAll operations.

Returns:Set<Map.Entry<K, V>>

a set view of the mappings contained in this map

Annotations
@Override
getback to summary
public V get(final Object key)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.get, java.util.Map.get.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.get.

Gets a value from the cache

Parameters
key:Object

the key whose associated value is to be returned

Returns:V

the value, or null, if it does not exist.

Annotations
@Override
getAndPutback to summary
public V getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider)

Overrides default org.codehaus.groovy.runtime.memoize.MemoizeCache.getAndPut.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.getAndPut.

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.

Parameters
valueProvider:MemoizeCache.ValueProvider<? super K, ? extends V>

provide the value if the associated value not found

Returns:V

the cached value

Annotations
@Override
getAndPutback to summary
public V getAndPut(K key, MemoizeCache.ValueProvider<? super K, ? extends V> valueProvider, boolean shouldCache)
isEmptyback to summary
public boolean isEmpty()

Implements java.util.Map.isEmpty.

Doc from java.util.Map.isEmpty.

Returns true if this map contains no key-value mappings.

Returns:boolean

true if this map contains no key-value mappings

Annotations
@Override
keysback to summary
public Set<K> keys()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.keys.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.keys.

Get all keys associated to cached values

Returns:Set<K>

all keys

Annotations
@Override
keySetback to summary
public Set<K> keySet()

Implements java.util.Map.keySet.

Doc from java.util.Map.keySet.

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Returns:Set<K>

a set view of the keys contained in this map

Annotations
@Override
putback to summary
public V put(final K key, final V value)

Implements org.codehaus.groovy.runtime.memoize.MemoizeCache.put, java.util.Map.put.

Doc from org.codehaus.groovy.runtime.memoize.MemoizeCache.put.

Associates the specified value with the specified key in the cache.

Parameters
key:K

key with which the specified value is to be associated

value:V

value to be associated with the specified key

Returns:V

null, or the old value if the key associated with the specified key.

Annotations
@Override
putAllback to summary
public void putAll(Map<? extends K, ? extends V> m)

Implements java.util.Map.putAll.

Doc from java.util.Map.putAll.

Copies all of the mappings from the specified map to this map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress. If the specified map has a defined encounter order, processing of its mappings generally occurs in that order.

Parameters
m:Map<? extends K, ? extends V>

mappings to be stored in this map

Annotations
@Override
removeback to summary
public V remove(final Object key)

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.remove.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.remove.

Remove the cached value by the key

Parameters
key:Object

of the cached value

Returns:V

returns the removed value

Annotations
@Override
sizeback to summary
public int size()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.size.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.size.

Get the size of the cache

Returns:int

the size of the cache

Annotations
@Override
valuesback to summary
public Collection<V> values()

Implements org.codehaus.groovy.runtime.memoize.EvictableCache.values.

Doc from org.codehaus.groovy.runtime.memoize.EvictableCache.values.

Get all cached values

Returns:Collection<V>

all cached values

Annotations
@Override