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
ConstructorDescriptionPerItemEvictionStrategyCache
(ScheduledExecutorService executorService, AsyncFunction<V, Duration, Exception> defaultTimeoutFunction) Build a newPerItemEvictionStrategyCache
using the given scheduled executor.PerItemEvictionStrategyCache
(ScheduledExecutorService executorService, Duration defaultTimeout) Build a newPerItemEvictionStrategyCache
using the given scheduled executor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clean-up the cache entries.void
Evict a cached value from the cache.Gets the maximum timeout (can be null).Borrow (and create before hand if absent) a cache entry.Borrow (and create before hand if absent) a cache entry.boolean
isEmpty()
Returns whether this cache is empty or not.void
setMaxTimeout
(Duration maxTimeout) Sets the maximum timeout.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
public PerItemEvictionStrategyCache(ScheduledExecutorService executorService, AsyncFunction<V, Duration, Exception> defaultTimeoutFunction) 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
-
getValue
Borrow (and create before hand 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
public <E extends Exception> V getValue(K key, Callable<V> callable, AsyncFunction<V, Duration, throws InterruptedException, ExecutionExceptionE> expire) Borrow (and create before hand 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
-
getMaxTimeout
Gets the maximum timeout (can be null).- Returns:
- the maximum timeout
-
setMaxTimeout
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.
-