Class AbstractLoadingCache<K,V>
- All Implemented Interfaces:
Function<K,V>, Cache<K, V>, LoadingCache<K, V>, Function<K, V>
Cache interface to minimize the
effort required to implement this interface.
To implement a cache, the programmer needs only to extend this class and provide an
implementation for the LoadingCache.get(Object) and Cache.getIfPresent(Object) methods. getUnchecked(K), AbstractCache.get(Object, Callable), and getAll(Iterable) are implemented in terms of
get; AbstractCache.getAllPresent(Iterable) is implemented in terms of getIfPresent; AbstractCache.putAll(Map) is implemented in terms of AbstractCache.put(K, V), AbstractCache.invalidateAll(Iterable) is implemented
in terms of AbstractCache.invalidate(Object). The method AbstractCache.cleanUp() is a no-op. All other methods throw
an UnsupportedOperationException.
- Since:
- 11.0
- Author:
- Charles Fry
-
Nested Class Summary
Nested classes/interfaces inherited from class AbstractCache
AbstractCache.SimpleStatsCounter, AbstractCache.StatsCounter -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor for use by subclasses. -
Method Summary
Modifier and TypeMethodDescriptionfinal VReturns a map of the values associated withkeys, creating or retrieving those values if necessary.getUnchecked(K key) Returns the value associated withkeyin this cache, first loading that value if necessary.voidLoads a new value forkey, possibly asynchronously.Methods inherited from class AbstractCache
asMap, cleanUp, get, getAllPresent, invalidate, invalidateAll, invalidateAll, put, putAll, size, statsMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Cache
cleanUp, get, getAllPresent, getIfPresent, invalidate, invalidateAll, invalidateAll, put, putAll, size, statsMethods inherited from interface LoadingCache
asMap, get
-
Constructor Details
-
AbstractLoadingCache
protected AbstractLoadingCache()Constructor for use by subclasses.
-
-
Method Details
-
getUnchecked
Description copied from interface:LoadingCacheReturns the value associated withkeyin this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes. UnlikeLoadingCache.get(K), this method does not throw a checked exception, and thus should only be used in situations where checked exceptions are not thrown by the cache loader.If another call to
LoadingCache.get(K)orLoadingCache.getUnchecked(K)is currently loading the value forkey, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.Caches loaded by a
CacheLoaderwill callCacheLoader.load(K)to load new values into the cache. Newly loaded values are added to the cache usingCache.asMap().putIfAbsentafter loading has completed; if another value was associated withkeywhile the new value was loading then a removal notification will be sent for the new value.Warning: this method silently converts checked exceptions to unchecked exceptions, and should not be used with cache loaders which throw checked exceptions. In such cases use
LoadingCache.get(K)instead.- Specified by:
getUncheckedin interfaceLoadingCache<K,V>
-
getAll
Description copied from interface:LoadingCacheReturns a map of the values associated withkeys, creating or retrieving those values if necessary. The returned map contains entries that were already cached, combined with newly loaded entries; it will never contain null keys or values.Caches loaded by a
CacheLoaderwill issue a single request toCacheLoader.loadAll(Iterable)for all keys which are not already present in the cache. All entries returned byCacheLoader.loadAll(Iterable)will be stored in the cache, over-writing any previously cached values. This method will throw an exception ifCacheLoader.loadAll(Iterable)returnsnull, returns a map containing null keys or values, or fails to return an entry for each requested key.Note that duplicate elements in
keys, as determined byObject.equals(Object), will be ignored.- Specified by:
getAllin interfaceLoadingCache<K,V> - Throws:
ExecutionException- if a checked exception was thrown while loading the value. (ExecutionExceptionis thrown even if computation was interrupted by anInterruptedException.)
-
apply
-
refresh
Description copied from interface:LoadingCacheLoads a new value forkey, possibly asynchronously. While the new value is loading the previous value (if any) will continue to be returned byget(key)unless it is evicted. If the new value is loaded successfully it will replace the previous value in the cache; if an exception is thrown while refreshing the previous value will remain, and the exception will be logged (usingLogger) and swallowed.Caches loaded by a
CacheLoaderwill callCacheLoader.reload(K, V)if the cache currently contains a value forkey, andCacheLoader.load(K)otherwise. Loading is asynchronous only ifCacheLoader.reload(K, V)was overridden with an asynchronous implementation.Returns without doing anything if another thread is currently loading the value for
key. If the cache loader associated with this cache performs refresh asynchronously then this method may return before refresh completes.- Specified by:
refreshin interfaceLoadingCache<K,V>
-