Interface Memoization
- All Known Implementing Classes:
DefaultMemoization
public interface Memoization
Instance-bound memoization store used by
Fluxzero for lightweight caching of values.
A Memoization is scoped to a single Fluxzero instance. Higher-level concerns such as whether entries
are additionally scoped to the calling class or shared globally within that instance are handled by the
Fluxzero static helper methods that construct the effective cache key.
Implementations may support expiring entries by lifespan. Expired values are no longer returned, and may be removed eagerly or lazily depending on the implementation.
-
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.
-
Method Details
-
put
Stores the given value for the given key, optionally with a lifespan after which it expires.- 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
Computes 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.- 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
Returns the cached value for the given key if present and not expired, otherwise computes, stores, and returns a new value.- 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
Returns the currently cached value for the given key, ornullif none exists or the value has expired.- Type Parameters:
V- the type of the memoized value- Parameters:
key- the internal cache key- Returns:
- the cached value, or
null
-
remove
Removes the cached value for the given key and returns it.- 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
void clear()Removes all cached values from this memoization store.
-