Class Memoization<K,V>
java.lang.Object
com.kingsrook.qqq.backend.core.utils.memoization.Memoization<K,V>
Basic memoization functionality - with result timeouts (only when doing a get -
there's no cleanup thread), and max-size.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructorMemoization
(Integer maxSize) ConstructorMemoization
(Duration timeout) ConstructorMemoization
(Duration timeout, Integer maxSize) Constructor -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
Getter for maxSizeboolean
Getter for mayStoreNullValuesgetMemoizedResult
(K key) Get a memoized result, optionally containing a Value, for a given input Key.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).getResultThrowing
(K key, UnsafeFunction<K, V, E> lookupFunction) Get the memoized Value for a given input Key - computing it if it wasn't previously memoized (or expired).Getter for timeoutvoid
setMaxSize
(Integer maxSize) Setter for maxSizevoid
setMayStoreNullValues
(boolean mayStoreNullValues) Setter for mayStoreNullValuesvoid
setTimeout
(Duration timeout) Setter for timeoutSecondsvoid
storeResult
(K key, V value) withMaxSize
(Integer maxSize) Fluent setter for maxSizewithMayStoreNullValues
(boolean mayStoreNullValues) Fluent setter for mayStoreNullValueswithTimeout
(Duration timeout) Fluent setter for timeout
-
Constructor Details
-
Memoization
public Memoization()Constructor -
Memoization
Constructor -
Memoization
Constructor -
Memoization
-
-
Method Details
-
getResultThrowing
public <E extends Exception> Optional<V> getResultThrowing(K key, UnsafeFunction<K, V, throws EE> lookupFunction) 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
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
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
-
clear
public void clear() -
clearKey
-
setTimeout
Setter for timeoutSeconds -
setMaxSize
Setter for maxSize -
getTimeout
Getter for timeout -
withTimeout
Fluent setter for timeout -
getMaxSize
Getter for maxSize -
withMaxSize
Fluent setter for maxSize -
getMayStoreNullValues
public boolean getMayStoreNullValues()Getter for mayStoreNullValues -
setMayStoreNullValues
public void setMayStoreNullValues(boolean mayStoreNullValues) Setter for mayStoreNullValues -
withMayStoreNullValues
Fluent setter for mayStoreNullValues
-