K
- Type of the keyV
- Type of the valuepublic class ThreadSafeCache<K,V> extends Object
Instead of storing directly the value in the backing Map, it requires the consumer to provide a value factory (a Callable). A new FutureTask encapsulate 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 and Description |
---|
ThreadSafeCache(ScheduledExecutorService executorService)
Build a new
ThreadSafeCache using the given scheduled executor. |
public ThreadSafeCache(ScheduledExecutorService executorService)
ThreadSafeCache
using the given scheduled executor.executorService
- scheduled executor for registering expiration callbacks.public void setTimeout(Duration timeout)
timeout
- new cache entry timeoutpublic V getValue(K key, Callable<V> callable) throws InterruptedException, ExecutionException
key
- entry keycallable
- cached value factoryInterruptedException
- if the current thread was interrupted while waitingExecutionException
- if the cached value computation threw an exceptionpublic void clear()
Copyright © 2014 ForgeRock AS. All rights reserved.