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.fluxzero.sdk.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 TypeMethodDescriptionvoidclear()Removes all cached values from this function.default <K1> @NonNull MemoizingFunction<K1, V> Composes this memoizing function with another function.voidApplies the given consumer to all currently cached values.booleanChecks 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
-
isCached
Checks if the given key has a cached value.- Parameters:
key- the key to test- Returns:
trueif the value is cached, otherwisefalse
-
forEach
-
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 givenbeforefunction to the input and then applies this memoizing function to the result.The composed function retains memoization behavior for the transformed keys.
-