Package org.axonframework.common.caching
Class WeakReferenceCache
java.lang.Object
org.axonframework.common.caching.WeakReferenceCache
- All Implemented Interfaces:
Cache
Cache implementation that keeps values in the cache until the garbage collector has removed them. Unlike the
WeakHashMap, which uses weak references on the keys, this Cache uses weak references on the values.
Values are Weakly referenced, which means they are not eligible for removal as long as any other references to the
value exist.
Items expire once the garbage collector has removed them. Some time after they have been removed, the entry listeners
are being notified thereof. Note that notification are emitted when the cache is being accessed (either for reading
or writing). If the cache is not being accessed for a longer period of time, it may occur that listeners are not
notified.
- Since:
- 2.2.1
- Author:
- Allard Buijze, Henrique Sena
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.axonframework.common.caching.Cache
Cache.EntryListener, Cache.EntryListenerAdapter -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TcomputeIfAbsent(Object key, Supplier<T> valueSupplier) Returns the value under the givenkeyin the cache.<V> voidcomputeIfPresent(Object key, UnaryOperator<V> update) Perform theupdatein the value behind the givenkey.booleancontainsKey(Object key) Indicates whether there is an item stored under givenkey.<K,V> V get(K key) Returns an item from the cache, ornullif no item was stored under that keyvoidStores the givenvaluein the cache, under givenkey.booleanputIfAbsent(Object key, Object value) Stores the givenvaluein the cache, under givenkey, if no element is yet available under that key.registerCacheEntryListener(Cache.EntryListener entryListener) Registers the givencacheEntryListenerto listen for Cache changes.booleanRemoves the entry stored under givenkey.voidRemove all stored entries in this cache.
-
Constructor Details
-
WeakReferenceCache
public WeakReferenceCache()
-
-
Method Details
-
registerCacheEntryListener
Description copied from interface:CacheRegisters the givencacheEntryListenerto listen for Cache changes.- Specified by:
registerCacheEntryListenerin interfaceCache- Parameters:
entryListener- The listener to register- Returns:
- a handle to deregister the listener
-
get
public <K,V> V get(K key) Description copied from interface:CacheReturns an item from the cache, ornullif no item was stored under that key -
put
Description copied from interface:CacheStores the givenvaluein the cache, under givenkey. If an item already exists, it is updated with the new value. -
putIfAbsent
Description copied from interface:CacheStores the givenvaluein the cache, under givenkey, if no element is yet available under that key. This operation is performed atomically.- Specified by:
putIfAbsentin interfaceCache- Parameters:
key- The key under which to store the itemvalue- The item to cache- Returns:
trueif no value was previously assigned to the key,falseotherwise.
-
computeIfAbsent
Description copied from interface:CacheReturns the value under the givenkeyin the cache. If there is no value present, will invoke the givenvalueSupplier, put the value in the cache and return the produced value.- Specified by:
computeIfAbsentin interfaceCache- Parameters:
key- The key under which the item was cached. If not present, this key is used to cache the outcome of thevalueSupplier.valueSupplier- A supplier that lazily supplies the value if there's nokeypresent.- Returns:
- The value that is in the cache after the operation. This can be the original value or the one supplied by
the
valueSupplier.
-
remove
Description copied from interface:CacheRemoves the entry stored under givenkey. If no such entry exists, nothing happens. -
removeAll
public void removeAll()Description copied from interface:CacheRemove all stored entries in this cache. -
containsKey
Description copied from interface:CacheIndicates whether there is an item stored under givenkey.- Specified by:
containsKeyin interfaceCache- Parameters:
key- The key to check- Returns:
trueif an item is available under that key,falseotherwise.
-
computeIfPresent
Description copied from interface:CachePerform theupdatein the value behind the givenkey. Theupdateis only executed if there's an entry referencing thekey.- Specified by:
computeIfPresentin interfaceCache- Type Parameters:
V- The type of the value to execute theupdatefor.- Parameters:
key- The key to perform an update for, if not empty.update- The update to perform if thekeyis present.
-