Package org.forgerock.util
Class PerItemEvictionStrategyCache<K,V>
java.lang.Object
org.forgerock.util.PerItemEvictionStrategyCache<K,V>
- Type Parameters:
K
- Type of the keyV
- Type of the value
PerItemEvictionStrategyCache is a thread-safe write-through cache.
Instead of storing directly the value in the backing Map, it requires the consumer to provide a value factory (a Callable). A new FutureTask encapsulates the callable, is executed and is placed inside a ConcurrentHashMap if absent.
The final behavior is that, even if two concurrent Threads are borrowing an object from the cache, given that they provide an equivalent value factory, the first one will compute the value while the other will get the result from the Future (and will wait until the result is computed or a timeout occurs).
-
Constructor Summary
ConstructorsConstructorDescriptionPerItemEvictionStrategyCache
(ScheduledExecutorService executorService, Duration defaultTimeout) Build a newPerItemEvictionStrategyCache
using the given scheduled executor.PerItemEvictionStrategyCache
(ScheduledExecutorService executorService, AsyncFunction<V, Duration, Exception> defaultTimeoutFunction) Deprecated, for removal: This API element is subject to removal in a future version.PerItemEvictionStrategyCache
(ScheduledExecutorService executorService, Duration defaultTimeout) Deprecated, for removal: This API element is subject to removal in a future version. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clean-up the cache entries.void
Evict a cached value from the cache.Deprecated, for removal: This API element is subject to removal in a future version.UsegetMaxTimeoutDuration()
instead.Gets the maximum timeout as aDuration
(can be null).Borrow (and create beforehand if absent) a cache entry.Deprecated, for removal: This API element is subject to removal in a future version.UsegetValue2(Object, Callable, AsyncFunction)
instead.Borrow (and create beforehand if absent) a cache entry.boolean
isEmpty()
Returns whether this cache is empty or not.static <K,
V> PerItemEvictionStrategyCache<K, V> perItemEvictionStrategyCache
(ScheduledExecutorService executorService, AsyncFunction<V, Duration, Exception> defaultTimeoutFunction) Build a newPerItemEvictionStrategyCache
using the given scheduled executor.void
setMaxTimeout
(Duration maxTimeout) Deprecated, for removal: This API element is subject to removal in a future version.UsesetMaxTimeoutDuration(Duration)
instead.void
setMaxTimeoutDuration
(Duration maxTimeout) Sets the maximum timeout as aDuration
.int
size()
Returns the number of cached values.
-
Constructor Details
-
PerItemEvictionStrategyCache
public PerItemEvictionStrategyCache(ScheduledExecutorService executorService, Duration defaultTimeout) Build a newPerItemEvictionStrategyCache
using the given scheduled executor.- Parameters:
executorService
- scheduled executor for registering expiration callbacks.defaultTimeout
- the default cache entry timeout
-
PerItemEvictionStrategyCache
@Deprecated(since="27.0.0", forRemoval=true) public PerItemEvictionStrategyCache(ScheduledExecutorService executorService, Duration defaultTimeout) Deprecated, for removal: This API element is subject to removal in a future version.Build a newPerItemEvictionStrategyCache
using the given scheduled executor.- Parameters:
executorService
- scheduled executor for registering expiration callbacksdefaultTimeout
- the default cache entry timeout
-
PerItemEvictionStrategyCache
@Deprecated(since="27.0.0", forRemoval=true) public PerItemEvictionStrategyCache(ScheduledExecutorService executorService, AsyncFunction<V, Duration, Exception> defaultTimeoutFunction) Deprecated, for removal: This API element is subject to removal in a future version.Build a newPerItemEvictionStrategyCache
using the given scheduled executor.- Parameters:
executorService
- scheduled executor for registering expiration callbacks.defaultTimeoutFunction
- the function that will compute the cache entry timeout (must not be null) the default timeout to cache the entries
-
-
Method Details
-
perItemEvictionStrategyCache
public static <K,V> PerItemEvictionStrategyCache<K,V> perItemEvictionStrategyCache(ScheduledExecutorService executorService, AsyncFunction<V, Duration, Exception> defaultTimeoutFunction) Build a newPerItemEvictionStrategyCache
using the given scheduled executor.- Type Parameters:
K
- the type of the keyV
- the type of the value- Parameters:
executorService
- scheduled executor for registering expiration callbacks.defaultTimeoutFunction
- the function that will compute the cache entry timeout (must not be null) the default timeout to cache the entries- Returns:
- a new
PerItemEvictionStrategyCache
-
getValue
Borrow (and create beforehand if absent) a cache entry. If another thread has created (or the creation is undergoing) the value, this method waits indefinitely for the value to be available.- Parameters:
key
- entry keycallable
- cached value factory- Returns:
- the cached value
- Throws:
InterruptedException
- if the current thread was interrupted while waitingExecutionException
- if the cached value computation threw an exception
-
getValue
@Deprecated(since="27.0.0", forRemoval=true) public <E extends Exception> V getValue(K key, Callable<V> callable, AsyncFunction<V, Duration, throws InterruptedException, ExecutionExceptionE> expire) Deprecated, for removal: This API element is subject to removal in a future version.UsegetValue2(Object, Callable, AsyncFunction)
instead.Borrow (and create beforehand if absent) a cache entry. If another thread has created (or the creation is undergoing) the value, this method waits indefinitely for the value to be available.- Type Parameters:
E
- type of exception- Parameters:
key
- entry keycallable
- cached value factoryexpire
- function to override the global cache's timeout- Returns:
- the cached value
- Throws:
InterruptedException
- if the current thread was interrupted while waitingExecutionException
- if the cached value computation threw an exception
-
getValue2
public <E extends Exception> V getValue2(K key, Callable<V> callable, AsyncFunction<V, Duration, throws InterruptedException, ExecutionExceptionE> expire) Borrow (and create beforehand if absent) a cache entry. If another thread has created (or the creation is undergoing) the value, this method waits indefinitely for the value to be available.- Type Parameters:
E
- type of exception- Parameters:
key
- entry keycallable
- cached value factoryexpire
- function to override the global cache's timeout- Returns:
- the cached value
- Throws:
InterruptedException
- if the current thread was interrupted while waitingExecutionException
- if the cached value computation threw an exception
-
clear
public void clear()Clean-up the cache entries. -
size
public int size()Returns the number of cached values.- Returns:
- the number of cached values
-
isEmpty
public boolean isEmpty()Returns whether this cache is empty or not.- Returns:
- true if the cache does not contain any values, false otherwise.
-
evict
Evict a cached value from the cache.- Parameters:
key
- the entry key
-
getMaxTimeoutDuration
Gets the maximum timeout as aDuration
(can be null).- Returns:
- the maximum timeout
-
getMaxTimeout
Deprecated, for removal: This API element is subject to removal in a future version.UsegetMaxTimeoutDuration()
instead.Gets the maximum timeout as aDuration
(can be null).- Returns:
- the maximum timeout
-
setMaxTimeoutDuration
Sets the maximum timeout as aDuration
. If the timeout returned by the timeoutFunction is greater than this specified maximum timeout, then the maximum timeout is used instead of the returned one to cache the entry.- Parameters:
maxTimeout
- the maximum timeout to use.
-
setMaxTimeout
Deprecated, for removal: This API element is subject to removal in a future version.UsesetMaxTimeoutDuration(Duration)
instead.Sets the maximum timeout. If the timeout returned by the timeoutFunction is greater than this specified maximum timeout, then the maximum timeout is used instead of the returned one to cache the entry.- Parameters:
maxTimeout
- the maximum timeout to use.
-
perItemEvictionStrategyCache(ScheduledExecutorService, AsyncFunction)
instead.