Class LocalAsyncLoadingCache<K,V>
- java.lang.Object
-
- com.github.benmanes.caffeine.cache.LocalAsyncLoadingCache<K,V>
-
- All Implemented Interfaces:
AsyncCache<K,V>
,AsyncLoadingCache<K,V>
,LocalAsyncCache<K,V>
- Direct Known Subclasses:
BoundedLocalCache.BoundedLocalAsyncLoadingCache
,UnboundedLocalCache.UnboundedLocalAsyncLoadingCache
abstract class LocalAsyncLoadingCache<K,V> extends java.lang.Object implements LocalAsyncCache<K,V>, AsyncLoadingCache<K,V>
This class provides a skeletal implementation of theAsyncLoadingCache
interface to minimize the effort required to implement aLocalCache
.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
LocalAsyncLoadingCache.LoadingCacheView<K,V>
-
Nested classes/interfaces inherited from interface com.github.benmanes.caffeine.cache.LocalAsyncCache
LocalAsyncCache.AbstractCacheView<K,V>, LocalAsyncCache.AsMapView<K,V>, LocalAsyncCache.AsyncAsMapView<K,V>, LocalAsyncCache.AsyncBulkCompleter<K,V>, LocalAsyncCache.CacheView<K,V>
-
-
Field Summary
Fields Modifier and Type Field Description (package private) @Nullable LocalAsyncLoadingCache.LoadingCacheView<K,V>
cacheView
(package private) boolean
canBulkLoad
(package private) AsyncCacheLoader<K,V>
loader
(package private) static java.util.logging.Logger
logger
-
Constructor Summary
Constructors Constructor Description LocalAsyncLoadingCache(AsyncCacheLoader<? super K,V> loader)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
canBulkLoad(AsyncCacheLoader<?,?> loader)
Returns whether the supplied cache loader has bulk load functionality.java.util.concurrent.CompletableFuture<V>
get(K key)
Returns the future associated withkey
in this cache, obtaining that value fromAsyncCacheLoader.asyncLoad(K, java.util.concurrent.Executor)
if necessary.java.util.concurrent.CompletableFuture<java.util.Map<K,V>>
getAll(java.lang.Iterable<? extends K> keys)
Returns the future of a map of the values associated withkeys
, creating or retrieving those values if necessary.LoadingCache<K,V>
synchronous()
Returns a view of the entries stored in this cache as a synchronousCache
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.github.benmanes.caffeine.cache.AsyncLoadingCache
asMap
-
Methods inherited from interface com.github.benmanes.caffeine.cache.LocalAsyncCache
cache, composeResult, get, get, get, getAll, getAll, getIfPresent, handleCompletion, policy, put
-
-
-
-
Field Detail
-
logger
static final java.util.logging.Logger logger
-
canBulkLoad
final boolean canBulkLoad
-
loader
final AsyncCacheLoader<K,V> loader
-
cacheView
@Nullable LocalAsyncLoadingCache.LoadingCacheView<K,V> cacheView
-
-
Constructor Detail
-
LocalAsyncLoadingCache
LocalAsyncLoadingCache(AsyncCacheLoader<? super K,V> loader)
-
-
Method Detail
-
canBulkLoad
private static boolean canBulkLoad(AsyncCacheLoader<?,?> loader)
Returns whether the supplied cache loader has bulk load functionality.
-
get
public java.util.concurrent.CompletableFuture<V> get(K key)
Description copied from interface:AsyncLoadingCache
Returns the future associated withkey
in this cache, obtaining that value fromAsyncCacheLoader.asyncLoad(K, java.util.concurrent.Executor)
if necessary. If the asynchronous computation fails, the entry will be automatically removed from this cache.If the specified key is not already associated with a value, attempts to compute its value asynchronously 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.- Specified by:
get
in interfaceAsyncLoadingCache<K,V>
- Parameters:
key
- key with which the specified value is to be associated- Returns:
- the current (existing or computed) future value associated with the specified key
-
getAll
public java.util.concurrent.CompletableFuture<java.util.Map<K,V>> getAll(java.lang.Iterable<? extends K> keys)
Description copied from interface:AsyncLoadingCache
Returns the future of 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. If the any of the asynchronous computations fail, those entries will be automatically removed from this cache.Caches loaded by a
AsyncCacheLoader
supporting bulk loading will issue a single request toAsyncCacheLoader.asyncLoadAll(java.lang.Iterable<? extends K>, java.util.concurrent.Executor)
for all keys which are not already present in the cache. If another call toAsyncLoadingCache.get(K)
tries to load the value for a key inkeys
, that thread retrieves a future that is completed by this bulk computation. Caches that do not use aAsyncCacheLoader
with an optimized bulk load implementation will sequentially load each key by making individualAsyncCacheLoader.asyncLoad(K, java.util.concurrent.Executor)
calls. 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 interfaceAsyncLoadingCache<K,V>
- Parameters:
keys
- the keys whose associated values are to be returned- Returns:
- the future containing an unmodifiable mapping of keys to values for the specified keys in this cache
-
synchronous
public LoadingCache<K,V> synchronous()
Description copied from interface:AsyncCache
Returns a view of the entries stored in this cache as a synchronousCache
. A mapping is not present if the value is currently being loaded. Modifications made to the synchronous cache directly affect the asynchronous cache. If a modification is made to a mapping that is currently loading, the operation blocks until the computation completes.- Specified by:
synchronous
in interfaceAsyncCache<K,V>
- Specified by:
synchronous
in interfaceAsyncLoadingCache<K,V>
- Returns:
- a thread-safe synchronous view of this cache
-
-