Class AbstractCache<K,V>
- java.lang.Object
-
- com.sodius.oslc.core.cache.AbstractCache<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Implemented Interfaces:
Cache<K,V>
public abstract class AbstractCache<K,V> extends Object implements Cache<K,V>
A skeletal implementation ofCache
. This class is to extend when creating builders of cluster compatible caches. Local caches are built withCacheBuilder.local()
, one should not try implementing a local cache by any other mean.- Since:
- 3.7.0
-
-
Constructor Summary
Constructors Constructor Description AbstractCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description V
get(K key, Function<? super K,? extends V> loader)
Returns the value associated with thekey
in this cache, obtaining that value from theloader
if necessary.protected void
scheduleCleanUp()
Schedules a call to theCache.cleanUp()
method, in a separate thread.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.sodius.oslc.core.cache.Cache
asMap, cleanUp, getIfPresent, invalidate, invalidateAll, put
-
-
-
-
Method Detail
-
get
public V get(K key, Function<? super K,? extends V> loader)
Returns the value associated with thekey
in this cache, obtaining that value from theloader
if necessary. This method provides a simple substitute for the conventional "if cached, return; otherwise create, cache and return" pattern.If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this cache. The entire method invocation is performed atomically, so the function is applied at most once per key. Some attempted update operations on this cache by other threads may be blocked while the computation is in progress, so the computation should be short and simple, and must not attempt to update any other mappings of this cache.
Warning:
loader
must not attempt to update any other mappings of this cache.Warning:
loader
must not returnnull
; it may either return a non-null value or throw an exception.Important Note: contrary to the contract of the overridden method, this implementation does not guarantee the loader is applied at most once per key.
-
scheduleCleanUp
protected void scheduleCleanUp()
Schedules a call to theCache.cleanUp()
method, in a separate thread. Threads are created using the factory provided byThreads.getThreadFactory()
.Subclasses shall call this method in the implementation of
Cache.put(Object, Object)
andCache.invalidate(Object)
, so that expired entries are removed.
-
-