Class Memoization<K,V>

java.lang.Object
com.kingsrook.qqq.backend.core.utils.memoization.Memoization<K,V>

public class Memoization<K,V> extends Object
Basic memoization functionality - with result timeouts (only when doing a get - there's no cleanup thread), and max-size.
  • Constructor Details

    • Memoization

      public Memoization()
      Constructor
    • Memoization

      public Memoization(Integer maxSize)
      Constructor
    • Memoization

      public Memoization(Duration timeout)
      Constructor
    • Memoization

      public Memoization(Duration timeout, Integer maxSize)
      Constructor
  • Method Details

    • getResultThrowing

      public <E extends Exception> Optional<V> getResultThrowing(K key, UnsafeFunction<K,V,E> lookupFunction) throws E
      Get the memoized Value for a given input Key - computing it if it wasn't previously memoized (or expired). If the returned Optional is empty, it means the value is null (whether that came form memoization, or from the lookupFunction, you don't care - the answer is null).
      Throws:
      E
    • getResult

      public Optional<V> getResult(K key, UnsafeFunction<K,V,?> lookupFunction)
      Get the memoized Value for a given input Key - computing it if it wasn't previously memoized (or expired). If a null value was memoized, the resulting optional here will be empty. If the lookup function throws, then a null value will be memoized and an empty Optional will be returned. In here, if the optional is empty, it means the value is null (whether that came form memoization, or from the lookupFunction, you don't care - the answer is null).
    • getMemoizedResult

      public Optional<MemoizedResult<V>> getMemoizedResult(K key)
      Get a memoized result, optionally containing a Value, for a given input Key. If the returned Optional is empty, it means that we haven't ever looked up or memoized the key (or it's expired). If the returned Optional is not empty, then it means we've memoized something (and it's not expired) - so if the Value from the MemoizedResult is null, then null is the proper memoized value.
    • storeResult

      public void storeResult(K key, V value)
    • clear

      public void clear()
    • clearKey

      public void clearKey(K key)
    • setTimeout

      public void setTimeout(Duration timeout)
      Setter for timeoutSeconds
    • setMaxSize

      public void setMaxSize(Integer maxSize)
      Setter for maxSize
    • getTimeout

      public Duration getTimeout()
      Getter for timeout
    • withTimeout

      public Memoization<K,V> withTimeout(Duration timeout)
      Fluent setter for timeout
    • getMaxSize

      public Integer getMaxSize()
      Getter for maxSize
    • withMaxSize

      public Memoization<K,V> withMaxSize(Integer maxSize)
      Fluent setter for maxSize
    • getMayStoreNullValues

      public boolean getMayStoreNullValues()
      Getter for mayStoreNullValues
    • setMayStoreNullValues

      public void setMayStoreNullValues(boolean mayStoreNullValues)
      Setter for mayStoreNullValues
    • withMayStoreNullValues

      public Memoization<K,V> withMayStoreNullValues(boolean mayStoreNullValues)
      Fluent setter for mayStoreNullValues