Package io.fluxcapacitor.common
Interface MemoizingFunction<K,V>
- Type Parameters:
K
- the type of inputV
- the type of output
- All Superinterfaces:
Function<K,
V>
- All Known Implementing Classes:
DefaultMemoizingFunction
A
Function
that memoizes (caches) its results by key. Once a result is computed for a specific key,
it is stored and returned for all subsequent invocations with the same key until it is explicitly cleared or evicted.
Instances of this interface are typically created using static utility methods in
io.fluxcapacitor.javaclient.common.ClientUtils
, e.g.:
MemoizingFunction<String, Integer> lengthCache =
ClientUtils.memoize(String::length);
int len = lengthCache.apply("hello"); // computes and caches
boolean cached = lengthCache.isCached("hello"); // true
lengthCache.remove("hello"); // evicts the cached value
Memoization with a time-based expiration can also be created using an overload:
MemoizingFunction<String, Integer> expiringCache =
ClientUtils.memoize(String::length, Duration.ofMinutes(5));
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Removes all cached values from this function.default <K1> @NonNull MemoizingFunction
<K1, V> Composes this memoizing function with another function.void
Applies the given consumer to all currently cached values.boolean
Checks if the given key has a cached value.Removes and returns the cached result for the given key, if present.
-
Method Details
-
clear
void clear()Removes all cached values from this function. -
remove
Removes and returns the cached result for the given key, if present.- Parameters:
key
- the key whose cached result should be removed- Returns:
- the previously cached value, or
null
if not present
-
isCached
Checks if the given key has a cached value.- Parameters:
key
- the key to test- Returns:
true
if the value is cached, otherwisefalse
-
forEach
Applies the given consumer to all currently cached values.- Parameters:
consumer
- a consumer to operate on cached values
-
compose
@NonNull default <K1> @NonNull MemoizingFunction<K1,V> compose(@NonNull @NonNull Function<? super K1, ? extends K> before) Composes this memoizing function with another function. The resulting function first applies the givenbefore
function to the input and then applies this memoizing function to the result.The composed function retains memoization behavior for the transformed keys.
-