Interface MemoizingFunction<K,V>

Type Parameters:
K - the type of input
V - the type of output
All Superinterfaces:
Function<K,V>
All Known Implementing Classes:
DefaultMemoizingFunction

public interface MemoizingFunction<K,V> extends Function<K,V>
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 Type
    Method
    Description
    void
    Removes all cached values from this function.
    default <K1> @NonNull MemoizingFunction<K1,V>
    compose(@NonNull Function<? super K1,? extends K> before)
    Composes this memoizing function with another function.
    void
    forEach(Consumer<? super V> consumer)
    Applies the given consumer to all currently cached values.
    boolean
    isCached(K key)
    Checks if the given key has a cached value.
    remove(K key)
    Removes and returns the cached result for the given key, if present.

    Methods inherited from interface java.util.function.Function

    andThen, apply
  • Method Details

    • clear

      void clear()
      Removes all cached values from this function.
    • remove

      V remove(K key)
      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

      boolean isCached(K key)
      Checks if the given key has a cached value.
      Parameters:
      key - the key to test
      Returns:
      true if the value is cached, otherwise false
    • forEach

      void forEach(Consumer<? super V> consumer)
      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 given before function to the input and then applies this memoizing function to the result.

      The composed function retains memoization behavior for the transformed keys.

      Specified by:
      compose in interface Function<K,V>
      Type Parameters:
      K1 - the input type of the composed function
      Parameters:
      before - a function to transform input before applying this function
      Returns:
      a composed memoizing function