Class Cache<K,V>
- Object
-
- AbstractMap<K,V>
-
- Cache<K,V>
-
- Type Parameters:
K- the type of key objects.V- the type of value objects.
- All Implemented Interfaces:
ConcurrentMap<K,V>,Map<K,V>
public class Cache<K,V> extends AbstractMap<K,V> implements ConcurrentMap<K,V>
A concurrent map capable to locks entries for which the value is in process of being computed. This map is intended for use as a cache, with a goal of avoiding to compute the same values twice. This implementation is thread-safe and supports concurrency.Cacheis based onConcurrentHashMapwith the addition of three main capabilities:- Lock an entry when its value is under computation in a thread.
- Block other threads requesting the value of that particular entry until computation is completed.
- Retain oldest values by soft or weak references instead of strong references.
computeIfAbsent(…)orgetOrCreate(…)with lambda functions as below:
Alternatively, one can handle explicitly the locks. This alternative sometime provides more flexibility, for example in exception handling. The steps are as below:private final Cache<String,MyObject> cache = new Cache<String,MyObject>(); public MyObject getMyObject(String key) { return cache.computeIfAbsent(key, (k) -> createMyObject(k)); }- Check if the value is already available in the map. If it is, return it immediately and we are done.
- Otherwise, get a lock and check again if the value is already available in the map (because the value could have been computed by an other thread between step 1 and the obtention of the lock). If it is, release the lock and we are done.
- Otherwise compute the value, store the result and release the lock.
putAndUnlock(…)must be inside thefinallyblock of atryblock beginning immediately after the call tolock(…), no matter what the result of the computation is (includingnull).private final Cache<String,MyObject> cache = new Cache<String,MyObject>(); public MyObject getMyObject(final String key) throws MyCheckedException { MyObject value = cache.peek(key); if (value == null) { final Cache.Handler<MyObject> handler = cache.lock(key); try { value = handler.peek(); if (value == null) { value = createMyObject(key); } } finally { handler.putAndUnlock(value); } } return value; }Eviction of eldest values- The cost of a value is the value returned by
cost(V). The default implementation returns 1 in all cases, but subclasses can override this method for more elaborated cost computation. - The total cost is the sum of the cost of all values held by strong reference in this cache. The total cost does not include the cost of values held by weak or soft reference.
- The cost limit is the maximal value allowed for the total cost. If the total cost exceed this value, then strong references to the eldest values are replaced by weak or soft references until the total cost become equals or lower than the cost limit.
cost(V)method has not been overridden, then the total cost is the maximal amount of values to keep by strong references.Circular dependenciesThis implementation assumes that there is no circular dependencies (or cyclic graph) between the values in the cache. For example if creating A implies creating B, then creating B is not allowed to implies (directly or indirectly) the creation of A. If this condition is not met, deadlock may occur randomly.- Since:
- 0.3
Defined in the
sis-utilitymodule
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceCache.Handler<V>The handler returned bylock(K), to be used for unlocking and storing the result.-
Nested classes/interfaces inherited from class AbstractMap
AbstractMap.SimpleEntry<K extends Object,V extends Object>, AbstractMap.SimpleImmutableEntry<K extends Object,V extends Object>
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears the content of this cache.Vcompute(K key, BiFunction<? super K,? super V,? extends V> remapping)Replaces the value mapped to the given key by a new value computed from the old value.VcomputeIfAbsent(K key, Function<? super K,? extends V> creator)Returns the value for the given key if it exists, or computes it otherwise.VcomputeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remapping)Replaces the value mapped to the given key by a new value computed from the old value.booleancontainsKey(Object key)Returnstrueif this map contains the specified key.protected intcost(V value)Computes an estimation of the cost of the given value.Set<Map.Entry<K,V>>entrySet()Returns the set of entries in this cache.Vget(Object key)Returns the value mapped to the given key in the cache, potentially waiting for computation to complete.VgetOrCreate(K key, Callable<? extends V> creator)Returns the value for the given key if it exists, or computes it otherwise.booleanisEmpty()Returnstrueif this cache is empty.booleanisKeyCollisionAllowed()Returnstrueif different values may be assigned to the same key.Set<K>keySet()Returns the set of keys in this cache.Cache.Handler<V>lock(K key)Gets a lock for the entry at the given key and returns a handler to be used by the caller for unlocking and storing the result.Vmerge(K key, V value, BiFunction<? super V,? super V,? extends V> remapping)Maps the given value to the given key if no mapping existed before this method call, or computes a new value otherwise.Vpeek(K key)If a value is already cached for the given key, returns it.Vput(K key, V value)Puts the given value in cache and immediately returns the old value.VputIfAbsent(K key, V value)If no value is already mapped and no value is under computation for the given key, puts the given value in the cache.Vremove(Object key)Removes the value mapped to the given key in the cache.booleanremove(Object key, Object oldValue)If the given key is mapped to the given old value, removes that value.Vreplace(K key, V value)If the given key is mapped to any value, replaces that value with the given new value.booleanreplace(K key, V oldValue, V newValue)If the given key is mapped to the given old value, replaces that value with the given new value.voidreplaceAll(BiFunction<? super K,? super V,? extends V> remapping)Iterates over all entries in the cache and replaces their value with the one provided by the given function.voidsetKeyCollisionAllowed(boolean allowed)If set totrue, different values may be assigned to the same key.intsize()Returns the number of elements in this cache.-
Methods inherited from class AbstractMap
clone, containsValue, equals, hashCode, putAll, toString, values
-
Methods inherited from interface ConcurrentMap
forEach, getOrDefault
-
-
-
-
Constructor Detail
-
Cache
public Cache()
Creates a new cache with a default initial capacity and cost limit of 100. The oldest objects will be hold by weak references.
-
Cache
public Cache(int initialCapacity, long costLimit, boolean soft)Creates a new cache using the given initial capacity and cost limit. The initial capacity is the expected number of values to be stored in this cache. More values are allowed, but a little bit of CPU time may be saved if the expected capacity is known before the cache is created.The cost limit is the maximal value of the total cost (the sum of the cost of all values) before to replace eldest strong references by weak or soft references.
- Parameters:
initialCapacity- the initial capacity.costLimit- the maximum cost of objects to keep by strong reference.soft- iftrue, useSoftReferenceinstead ofWeakReference.
-
-
Method Detail
-
clear
public void clear()
Clears the content of this cache.
-
isEmpty
public boolean isEmpty()
Returnstrueif this cache is empty.
-
size
public int size()
-
get
public V get(Object key)
Returns the value mapped to the given key in the cache, potentially waiting for computation to complete. This method is similar topeek(Object)except that it blocks if the value is currently under computation in another thread.- Specified by:
getin interfaceMap<K,V>- Overrides:
getin classAbstractMap<K,V>- Parameters:
key- the key of the value to get.- Returns:
- the value mapped to the given key, or
nullif none. - See Also:
peek(Object),containsKey(Object),computeIfAbsent(Object, Function)
-
getOrCreate
public V getOrCreate(K key, Callable<? extends V> creator) throws Exception
Returns the value for the given key if it exists, or computes it otherwise. If a value already exists in the cache, then it is returned immediately. Otherwise thecreator.call()method is invoked and its result is saved in this cache for future reuse.Example: the following example shows how this method can be used. In particular, it shows how to propagateThis method is similar toMyCheckedException:private final Cache<String,MyObject> cache = new Cache<String,MyObject>(); public MyObject getMyObject(final String key) throws MyCheckedException { try { return cache.getOrCreate(key, new Callable<MyObject>() { public MyObject call() throws MyCheckedException { return createMyObject(key); } }); } catch (MyCheckedException | RuntimeException e) { throw e; } catch (Exception e) { throw new UndeclaredThrowableException(e); } }computeIfAbsent(Object, Function)except that it can propagate checked exceptions. If thecreatorfunction does not throw any checked exception, then invokingcomputeIfAbsent(…)is simpler.- Parameters:
key- the key for which to get the cached or created value.creator- a method for creating a value, to be invoked only if no value are cached for the given key.- Returns:
- the value for the given key, which may have been created as a result of this method call.
- Throws:
Exception- if an exception occurred during the execution ofcreator.call().- See Also:
get(Object),peek(Object),computeIfAbsent(Object, Function)
-
computeIfAbsent
public V computeIfAbsent(K key, Function<? super K,? extends V> creator)
Returns the value for the given key if it exists, or computes it otherwise. If a value already exists in the cache, then it is returned immediately. Otherwise thecreator.apply(Object)method is invoked and its result is saved in this cache for future reuse.Example: below is the same code thanThis method is similar togetOrCreate(Object, Callable)example, but without the need for any checked exception handling:private final Cache<String,MyObject> cache = new Cache<String,MyObject>(); public MyObject getMyObject(final String key) { return cache.computeIfAbsent(key, (k) -> createMyObject(k)); }getOrCreate(Object, Callable), but without checked exceptions.- Specified by:
computeIfAbsentin interfaceConcurrentMap<K,V>- Specified by:
computeIfAbsentin interfaceMap<K,V>- Parameters:
key- the key for which to get the cached or created value.creator- a method for creating a value, to be invoked only if no value are cached for the given key.- Returns:
- the value already mapped to the key, or the newly computed value.
- Since:
- 1.0
- See Also:
peek(Object),containsKey(Object),getOrCreate(Object, Callable),computeIfPresent(Object, BiFunction)
-
putIfAbsent
public V putIfAbsent(K key, V value)
If no value is already mapped and no value is under computation for the given key, puts the given value in the cache. Otherwise returns the current value (potentially blocking until the computation finishes). A nullvalueargument is equivalent to a no-op. Otherwise anullreturn value means that the givenvaluehas been stored in theCache.- Specified by:
putIfAbsentin interfaceConcurrentMap<K,V>- Specified by:
putIfAbsentin interfaceMap<K,V>- Parameters:
key- the key to associate with a value.value- the value to associate with the given key if no value already exists, ornull.- Returns:
- the existing value mapped to the given key, or
nullif none existed before this method call. - Since:
- 1.0
- See Also:
get(Object),computeIfAbsent(Object, Function)
-
put
public V put(K key, V value)
Puts the given value in cache and immediately returns the old value. A nullvalueargument removes the entry. If a different value is under computation in another thread, then the other thread may fail with anIllegalStateExceptionunlessisKeyCollisionAllowed()returnstrue. For more safety, consider usingputIfAbsent(…)instead.- Specified by:
putin interfaceMap<K,V>- Overrides:
putin classAbstractMap<K,V>- Parameters:
key- the key to associate with a value.value- the value to associate with the given key, ornullfor removing the mapping.- Returns:
- the value previously mapped to the given key, or
nullif no value existed before this method call or if the value was under computation in another thread. - See Also:
get(Object),putIfAbsent(Object, Object)
-
replace
public V replace(K key, V value)
If the given key is mapped to any value, replaces that value with the given new value. Otherwise does nothing. A nullvalueargument removes the entry. If a different value is under computation in another thread, then the other thread may fail with anIllegalStateExceptionunlessisKeyCollisionAllowed()returnstrue.- Specified by:
replacein interfaceConcurrentMap<K,V>- Specified by:
replacein interfaceMap<K,V>- Parameters:
key- key of the value to replace.value- the new value to use in replacement of the previous one, ornullfor removing the mapping.- Returns:
- the value previously mapped to the given key, or
nullif no value existed before this method call or if the value was under computation in another thread. - Since:
- 1.0
- See Also:
replace(Object, Object, Object)
-
replace
public boolean replace(K key, V oldValue, V newValue)
If the given key is mapped to the given old value, replaces that value with the given new value. Otherwise does nothing. A nullvalueargument removes the entry if the condition matches. If a value is under computation in another thread, then this method unconditionally returnsfalse.- Specified by:
replacein interfaceConcurrentMap<K,V>- Specified by:
replacein interfaceMap<K,V>- Parameters:
key- key of the value to replace.oldValue- previous value expected to be mapped to the given key.newValue- the new value to put if the condition matches, ornullfor removing the mapping.- Returns:
trueif the value has been replaced,falseotherwise.- Since:
- 1.0
-
replaceAll
public void replaceAll(BiFunction<? super K,? super V,? extends V> remapping)
Iterates over all entries in the cache and replaces their value with the one provided by the given function. If the function throws an exception, the iteration is stopped and the exception is propagated. If any value is under computation in other threads, then the iteration will block on that entry until its computation is completed.- Specified by:
replaceAllin interfaceConcurrentMap<K,V>- Specified by:
replaceAllin interfaceMap<K,V>- Parameters:
remapping- the function computing new values from the old ones.- Since:
- 1.0
-
computeIfPresent
public V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remapping)
Replaces the value mapped to the given key by a new value computed from the old value. If a value for the given key is under computation in another thread, then this method blocks until that computation is completed. This is equivalent to the work performed byreplaceAll(…)but on a single entry.- Specified by:
computeIfPresentin interfaceConcurrentMap<K,V>- Specified by:
computeIfPresentin interfaceMap<K,V>- Parameters:
key- key of the value to replace.remapping- the function computing new values from the old ones.- Returns:
- the new value associated with the given key.
- Since:
- 1.0
- See Also:
computeIfAbsent(Object, Function)
-
compute
public V compute(K key, BiFunction<? super K,? super V,? extends V> remapping)
Replaces the value mapped to the given key by a new value computed from the old value. If there is no value for the given key, then the "old value" is taken asnull. If a value for the given key is under computation in another thread, then this method blocks until that computation is completed. This method is equivalent tocomputeIfPresent(…)except that a new value will be computed even if no value existed for the key before this method call.- Specified by:
computein interfaceConcurrentMap<K,V>- Specified by:
computein interfaceMap<K,V>- Parameters:
key- key of the value to replace.remapping- the function computing new values from the old ones, or from anullvalue.- Returns:
- the new value associated with the given key.
- Since:
- 1.0
- See Also:
computeIfAbsent(Object, Function)
-
merge
public V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remapping)
Maps the given value to the given key if no mapping existed before this method call, or computes a new value otherwise. If a value for the given key is under computation in another thread, then this method blocks until that computation is completed.- Specified by:
mergein interfaceConcurrentMap<K,V>- Specified by:
mergein interfaceMap<K,V>- Parameters:
key- key of the value to replace.value- the value to associate with the given key if no value already exists, ornull.remapping- the function computing a new value by merging the exiting value with thevalueargument given to this method.- Returns:
- the new value associated with the given key.
- Since:
- 1.0
-
remove
public V remove(Object key)
Removes the value mapped to the given key in the cache. If a value is under computation in another thread, then the other thread may fail with anIllegalStateExceptionunlessisKeyCollisionAllowed()returnstrue. For more safety, consider usingremove(Object, Object)instead.- Specified by:
removein interfaceMap<K,V>- Overrides:
removein classAbstractMap<K,V>- Parameters:
key- the key of the value to removed.- Returns:
- the value previously mapped to the given key, or
nullif no value existed before this method call or if the value was under computation in another thread. - See Also:
get(Object),remove(Object, Object)
-
remove
public boolean remove(Object key, Object oldValue)
If the given key is mapped to the given old value, removes that value. Otherwise does nothing. If a value is under computation in another thread, then this method unconditionally returnsfalse.- Specified by:
removein interfaceConcurrentMap<K,V>- Specified by:
removein interfaceMap<K,V>- Parameters:
key- key of the value to remove.oldValue- previous value expected to be mapped to the given key.- Returns:
trueif the value has been removed,falseotherwise.- Since:
- 1.0
- See Also:
get(Object)
-
containsKey
public boolean containsKey(Object key)
Returnstrueif this map contains the specified key. If the value is under computation in another thread, this method returnstruewithout waiting for the computation result. This behavior is consistent with otherMapmethods in the following ways:get(Object)blocks until the computation is completed.put(Object, Object)returnsnullfor values under computation, i.e. behaves as if keys are temporarily mapped to thenullvalue until the computation is completed.
- Specified by:
containsKeyin interfaceMap<K,V>- Overrides:
containsKeyin classAbstractMap<K,V>- Parameters:
key- the key to check for existence.- Returns:
trueif the given key is mapped to an existing value or a value under computation.- See Also:
get(Object),peek(Object)
-
peek
public V peek(K key)
If a value is already cached for the given key, returns it. Otherwise returnsnull. This method is similar toget(Object)except that it doesn't block if the value is in process of being computed in an other thread; it returnsnullin such case.- Parameters:
key- the key for which to get the cached value.- Returns:
- the cached value for the given key, or
nullif there is none. - See Also:
get(Object),lock(Object)
-
lock
public Cache.Handler<V> lock(K key)
Gets a lock for the entry at the given key and returns a handler to be used by the caller for unlocking and storing the result. This method must be used together with aputAndUnlockcall intry…catchblocks as in the example below:Cache.Handler handler = cache.lock(); try { // Compute the result... } finally { handler.putAndUnlock(result); }- Parameters:
key- the key for the entry to lock.- Returns:
- a handler to use for unlocking and storing the result.
-
keySet
public Set<K> keySet()
Returns the set of keys in this cache. The returned set is subjects to the same caution than the ones documented in theConcurrentHashMap.keySet()method.
-
entrySet
public Set<Map.Entry<K,V>> entrySet()
Returns the set of entries in this cache. The returned set is subjects to the same caution than the ones documented in theConcurrentHashMap.entrySet()method, except that it doesn't support removal of elements (including through theIterator.remove()method call).
-
isKeyCollisionAllowed
public boolean isKeyCollisionAllowed()
Returnstrueif different values may be assigned to the same key. The default value isfalse.- Returns:
trueif key collisions are allowed.
-
setKeyCollisionAllowed
public void setKeyCollisionAllowed(boolean allowed)
If set totrue, different values may be assigned to the same key. This is usually an error, so the defaultCachebehavior is to thrown anIllegalStateExceptionin such cases, typically whenCache.Handler.putAndUnlock(Object)is invoked. However in some cases we may want to relax this check. For example the EPSG database sometime assigns the same key to different kinds of objects.If key collisions are allowed and two threads invoke
lock(Object)concurrently for the same key, then the value to be stored in the map will be the one computed by the first thread who got the lock. The value computed by any other concurrent thread will be ignored by thisCacheclass. However those threads still return their computed values to their callers.This property can also be set in order to allow some recursivity. If during the creation of an object, the program asks to this
Cachefor the same object (using the same key), then the defaultCacheimplementation will consider this situation as a key collision unless this property has been set totrue.- Parameters:
allowed-trueif key collisions should be allowed.
-
cost
protected int cost(V value)
Computes an estimation of the cost of the given value. The default implementation returns 1 in all cases. Subclasses should override this method if they have some easy way to measure the relative cost of value objects.- Parameters:
value- the object for which to get an estimation of its cost.- Returns:
- the estimated cost of the given object.
- See Also:
Instrumentation.getObjectSize(Object)
-
-