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 Type
    Method
    Description
    void
    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> V
    get(Object key)
    Returns the currently cached value for the given key, or null if none exists or the value has expired.
    void
    put(Object key, Object value, Duration lifespan)
    Stores the given value for the given key, optionally with a lifespan after which it expires.
    <V> V
    Removes the cached value for the given key and returns it.
  • Method Details

    • put

      void put(Object key, Object value, Duration lifespan)
      Stores the given value for the given key, optionally with a lifespan after which it expires.
      Parameters:
      key - the cache key
      value - the value to store, which may be null
      lifespan - the optional lifespan of the value, or null to keep it until overwritten or cleared
    • compute

      <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.

      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 key
      V - the type of the memoized value
      Parameters:
      key - the internal cache key
      suppliedKey - the original key value to pass to the supplier
      supplier - computes the next value from the key and current cached value
      lifespan - the optional lifespan of the computed value, or null for no expiry
      Returns:
      the stored value
    • computeIfAbsent

      <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.
      Type Parameters:
      K - the type of the supplied key
      V - the type of the memoized value
      Parameters:
      key - the internal cache key
      suppliedKey - the original key value to pass to the supplier
      supplier - computes the value when no valid cached value exists
      lifespan - the optional lifespan of the computed value, or null for no expiry
      Returns:
      the cached or newly computed value
    • get

      <V> V get(Object key)
      Returns the currently cached value for the given key, or null if 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

      <V> V remove(Object key)
      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 null if none exists or the value has expired
    • clear

      void clear()
      Removes all cached values from this memoization store.