Interface LocalLoadingCache<K,V>
-
- All Superinterfaces:
Cache<K,V>
,LoadingCache<K,V>
,LocalManualCache<K,V>
- All Known Implementing Classes:
BoundedLocalCache.BoundedLocalLoadingCache
,UnboundedLocalCache.UnboundedLocalLoadingCache
interface LocalLoadingCache<K,V> extends LocalManualCache<K,V>, LoadingCache<K,V>
This class provides a skeletal implementation of theLoadingCache
interface to minimize the effort required to implement aLocalCache
.
-
-
Field Summary
Fields Modifier and Type Field Description static java.util.logging.Logger
logger
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description @Nullable java.util.function.Function<java.lang.Iterable<? extends K>,java.util.Map<K,V>>
bulkMappingFunction()
Returns theCacheLoader.loadAll(java.lang.Iterable<? extends K>)
as a mapping function, if implemented.CacheLoader<? super K,V>
cacheLoader()
Returns theCacheLoader
used by this cache.default @Nullable V
get(K key)
Returns the value associated with thekey
in this cache, obtaining that value fromCacheLoader.load(Object)
if necessary.default java.util.Map<K,V>
getAll(java.lang.Iterable<? extends K> keys)
Returns a map of the values associated with thekeys
, creating or retrieving those values if necessary.static boolean
hasLoadAll(CacheLoader<?,?> loader)
Returns whether the supplied cache loader has bulk load functionality.default java.util.Map<K,V>
loadSequentially(java.lang.Iterable<? extends K> keys)
Sequentially loads each missing entry.java.util.function.Function<K,V>
mappingFunction()
Returns theCacheLoader.load(K)
as a mapping function.static <K,V>
@Nullable java.util.function.Function<java.lang.Iterable<? extends K>,java.util.Map<K,V>>newBulkMappingFunction(CacheLoader<? super K,V> cacheLoader)
Returns a mapping function that adapts toCacheLoader.loadAll(java.lang.Iterable<? extends K>)
, if implemented.static <K,V>
java.util.function.Function<K,V>newMappingFunction(CacheLoader<? super K,V> cacheLoader)
Returns a mapping function that adapts toCacheLoader.load(K)
.default void
refresh(K key)
Loads a new value for thekey
, asynchronously.-
Methods inherited from interface com.github.benmanes.caffeine.cache.LocalManualCache
asMap, bulkLoad, cache, cleanUp, estimatedSize, get, getAll, getAllPresent, getIfPresent, invalidate, invalidateAll, invalidateAll, put, putAll, stats
-
-
-
-
Method Detail
-
cacheLoader
CacheLoader<? super K,V> cacheLoader()
Returns theCacheLoader
used by this cache.
-
mappingFunction
java.util.function.Function<K,V> mappingFunction()
Returns theCacheLoader.load(K)
as a mapping function.
-
bulkMappingFunction
@Nullable java.util.function.Function<java.lang.Iterable<? extends K>,java.util.Map<K,V>> bulkMappingFunction()
Returns theCacheLoader.loadAll(java.lang.Iterable<? extends K>)
as a mapping function, if implemented.
-
get
default @Nullable V get(K key)
Description copied from interface:LoadingCache
Returns the value associated with thekey
in this cache, obtaining that value fromCacheLoader.load(Object)
if necessary.If another call to
LoadingCache.get(K)
is currently loading the value for thekey
, this thread simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.If the specified key is not already associated with a value, attempts to compute its value and enters it into this cache unless
null
. 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.- Specified by:
get
in interfaceLoadingCache<K,V>
- Parameters:
key
- key with which the specified value is to be associated- Returns:
- the current (existing or computed) value associated with the specified key, or null if the computed value is null
-
getAll
default java.util.Map<K,V> getAll(java.lang.Iterable<? extends K> keys)
Description copied from interface:LoadingCache
Returns a map of the values associated with thekeys
, creating or retrieving those values if necessary. The returned map contains entries that were already cached, combined with the newly loaded entries; it will never contain null keys or values.Caches loaded by a
CacheLoader
will issue a single request toCacheLoader.loadAll(java.lang.Iterable<? extends K>)
for all keys which are not already present in the cache. All entries returned byCacheLoader.loadAll(java.lang.Iterable<? extends K>)
will be stored in the cache, over-writing any previously cached values. If another call toLoadingCache.get(K)
tries to load the value for a key inkeys
, implementations may either have that thread load the entry or simply wait for this thread to finish and returns the loaded value. In the case of overlapping non-blocking loads, the last load to complete will replace the existing entry. Note that multiple threads can concurrently load values for distinct keys.Note that duplicate elements in
keys
, as determined byObject.equals(java.lang.Object)
, will be ignored.- Specified by:
getAll
in interfaceLoadingCache<K,V>
- Parameters:
keys
- the keys whose associated values are to be returned- Returns:
- the unmodifiable mapping of keys to values for the specified keys in this cache
-
loadSequentially
default java.util.Map<K,V> loadSequentially(java.lang.Iterable<? extends K> keys)
Sequentially loads each missing entry.
-
refresh
default void refresh(K key)
Description copied from interface:LoadingCache
Loads a new value for thekey
, 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
CacheLoader
will callCacheLoader.reload(K, V)
if the cache currently contains a value for thekey
, andCacheLoader.load(K)
otherwise. Loading is asynchronous by delegating to the default executor.- Specified by:
refresh
in interfaceLoadingCache<K,V>
- Parameters:
key
- key with which a value may be associated
-
newMappingFunction
static <K,V> java.util.function.Function<K,V> newMappingFunction(CacheLoader<? super K,V> cacheLoader)
Returns a mapping function that adapts toCacheLoader.load(K)
.
-
newBulkMappingFunction
static <K,V> @Nullable java.util.function.Function<java.lang.Iterable<? extends K>,java.util.Map<K,V>> newBulkMappingFunction(CacheLoader<? super K,V> cacheLoader)
Returns a mapping function that adapts toCacheLoader.loadAll(java.lang.Iterable<? extends K>)
, if implemented.
-
hasLoadAll
static boolean hasLoadAll(CacheLoader<?,?> loader)
Returns whether the supplied cache loader has bulk load functionality.
-
-