Class DefaultMemoization

java.lang.Object
io.fluxzero.sdk.DefaultMemoization
All Implemented Interfaces:
Memoization

public final class DefaultMemoization extends Object implements Memoization
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • 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.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DefaultMemoization

      public DefaultMemoization()
  • Method Details

    • put

      public void put(Object key, Object value, Duration lifespan)
      Description copied from interface: Memoization
      Stores the given value for the given key, optionally with a lifespan after which it expires.
      Specified by:
      put in interface Memoization
      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

      public <K,V> V compute(Object key, K suppliedKey, BiFunction<K,V,V> supplier, Duration lifespan)
      Description copied from interface: Memoization
      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.

      Specified by:
      compute in interface Memoization
      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

      public <K,V> V computeIfAbsent(Object key, K suppliedKey, Function<K,V> supplier, Duration lifespan)
      Description copied from interface: Memoization
      Returns the cached value for the given key if present and not expired, otherwise computes, stores, and returns a new value.
      Specified by:
      computeIfAbsent in interface Memoization
      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

      public <V> V get(Object key)
      Description copied from interface: Memoization
      Returns the currently cached value for the given key, or null if none exists or the value has expired.
      Specified by:
      get in interface Memoization
      Type Parameters:
      V - the type of the memoized value
      Parameters:
      key - the internal cache key
      Returns:
      the cached value, or null
    • remove

      public <V> V remove(Object key)
      Description copied from interface: Memoization
      Removes the cached value for the given key and returns it.
      Specified by:
      remove in interface Memoization
      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

      public void clear()
      Description copied from interface: Memoization
      Removes all cached values from this memoization store.
      Specified by:
      clear in interface Memoization