Class DefaultMemoization
java.lang.Object
io.fluxzero.sdk.DefaultMemoization
- All Implemented Interfaces:
Memoization
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all cached values from this memoization store.<K,V> V compute(Object key, K suppliedKey, BiFunction<K, V, V> supplier, Duration lifespan) Computes a new value using the current cached value, stores the result, and returns it.<K,V> V computeIfAbsent(Object key, K suppliedKey, Function<K, V> supplier, Duration lifespan) Returns the cached value for the given key if present and not expired, otherwise computes, stores, and returns a new value.<V> VReturns the currently cached value for the given key, ornullif none exists or the value has expired.voidStores the given value for the given key, optionally with a lifespan after which it expires.<V> VRemoves the cached value for the given key and returns it.
-
Constructor Details
-
DefaultMemoization
public DefaultMemoization()
-
-
Method Details
-
put
Description copied from interface:MemoizationStores the given value for the given key, optionally with a lifespan after which it expires.- Specified by:
putin interfaceMemoization- Parameters:
key- the cache keyvalue- the value to store, which may benulllifespan- the optional lifespan of the value, ornullto keep it until overwritten or cleared
-
compute
Description copied from interface:MemoizationComputes a new value using the current cached value, stores the result, and returns it.If no value is currently cached for the key, or if the cached value has expired, the second argument passed to the supplier will be
null.- Specified by:
computein interfaceMemoization- Type Parameters:
K- the type of the supplied keyV- the type of the memoized value- Parameters:
key- the internal cache keysuppliedKey- the original key value to pass to the suppliersupplier- computes the next value from the key and current cached valuelifespan- the optional lifespan of the computed value, ornullfor no expiry- Returns:
- the stored value
-
computeIfAbsent
public <K,V> V computeIfAbsent(Object key, K suppliedKey, Function<K, V> supplier, Duration lifespan) Description copied from interface:MemoizationReturns the cached value for the given key if present and not expired, otherwise computes, stores, and returns a new value.- Specified by:
computeIfAbsentin interfaceMemoization- Type Parameters:
K- the type of the supplied keyV- the type of the memoized value- Parameters:
key- the internal cache keysuppliedKey- the original key value to pass to the suppliersupplier- computes the value when no valid cached value existslifespan- the optional lifespan of the computed value, ornullfor no expiry- Returns:
- the cached or newly computed value
-
get
Description copied from interface:MemoizationReturns the currently cached value for the given key, ornullif none exists or the value has expired.- Specified by:
getin interfaceMemoization- Type Parameters:
V- the type of the memoized value- Parameters:
key- the internal cache key- Returns:
- the cached value, or
null
-
remove
Description copied from interface:MemoizationRemoves the cached value for the given key and returns it.- Specified by:
removein interfaceMemoization- Type Parameters:
V- the type of the memoized value- Parameters:
key- the internal cache key- Returns:
- the removed cached value, or
nullif none exists or the value has expired
-
clear
public void clear()Description copied from interface:MemoizationRemoves all cached values from this memoization store.- Specified by:
clearin interfaceMemoization
-