Class 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 Detail

      • PerItemEvictionStrategyCache

        public PerItemEvictionStrategyCache​(ScheduledExecutorService executorService,
                                            Duration defaultTimeout)
        Build a new PerItemEvictionStrategyCache 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 new PerItemEvictionStrategyCache 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 Detail

      • getValue

        public V getValue​(K key,
                          Callable<V> callable)
                   throws InterruptedException,
                          ExecutionException
        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 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

        public <E extends ExceptionV getValue​(K key,
                                                Callable<V> callable,
                                                AsyncFunction<V,​Duration,​E> expire)
                                         throws InterruptedException,
                                                ExecutionException
        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 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
      • getMaxTimeout

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

        public void setMaxTimeout​(Duration maxTimeout)
        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.