Class PerItemEvictionStrategyCache<K,V>

java.lang.Object
org.forgerock.util.PerItemEvictionStrategyCache<K,V>
Type Parameters:
K - Type of the key
V - Type of the value

public class PerItemEvictionStrategyCache<K,V> extends Object
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 Details

  • Method Details

    • perItemEvictionStrategyCache

      public static <K, V> PerItemEvictionStrategyCache<K,V> perItemEvictionStrategyCache(ScheduledExecutorService executorService, AsyncFunction<V,Duration,Exception> defaultTimeoutFunction)
      Build a new PerItemEvictionStrategyCache using the given scheduled executor.
      Type Parameters:
      K - the type of the key
      V - 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

      public V getValue(K key, Callable<V> callable) throws InterruptedException, ExecutionException
      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 key
      callable - cached value factory
      Returns:
      the cached value
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - 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,E> expire) throws InterruptedException, ExecutionException
      Deprecated, for removal: This API element is subject to removal in a future version.
      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 key
      callable - cached value factory
      expire - function to override the global cache's timeout
      Returns:
      the cached value
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - if the cached value computation threw an exception
    • getValue2

      public <E extends Exception> V getValue2(K key, Callable<V> callable, AsyncFunction<V,Duration,E> expire) throws InterruptedException, ExecutionException
      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 key
      callable - cached value factory
      expire - function to override the global cache's timeout
      Returns:
      the cached value
      Throws:
      InterruptedException - if the current thread was interrupted while waiting
      ExecutionException - 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

      public void evict(K key)
      Evict a cached value from the cache.
      Parameters:
      key - the entry key
    • getMaxTimeoutDuration

      public Duration getMaxTimeoutDuration()
      Gets the maximum timeout as a Duration (can be null).
      Returns:
      the maximum timeout
    • getMaxTimeout

      @Deprecated(since="27.0.0", forRemoval=true) public Duration getMaxTimeout()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Gets the maximum timeout as a Duration (can be null).
      Returns:
      the maximum timeout
    • setMaxTimeoutDuration

      public void setMaxTimeoutDuration(Duration maxTimeout)
      Sets the maximum timeout as a Duration. 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(since="27.0.0", forRemoval=true) public void setMaxTimeout(Duration maxTimeout)
      Deprecated, for removal: This API element is subject to removal in a future version.
      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.