Class LocalAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K,V>
- java.lang.Object
-
- com.github.benmanes.caffeine.cache.LocalAsyncLoadingCache<C,K,V>
-
- All Implemented Interfaces:
AsyncLoadingCache<K,V>
- Direct Known Subclasses:
BoundedLocalCache.BoundedLocalAsyncLoadingCache,UnboundedLocalCache.UnboundedLocalAsyncLoadingCache
abstract class LocalAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K,V> extends java.lang.Object implements AsyncLoadingCache<K,V>
This class provides a skeletal implementation of theAsyncLoadingCacheinterface to minimize the effort required to implement aLocalCache.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classLocalAsyncLoadingCache.AsMapView<K,V>private classLocalAsyncLoadingCache.AsyncBulkCompleterA function executed asynchronously after a bulk load completes.(package private) classLocalAsyncLoadingCache.LoadingCacheView
-
Field Summary
Fields Modifier and Type Field Description (package private) Ccache(package private) booleancanBulkLoad(package private) AsyncCacheLoader<K,V>loader(package private) LocalAsyncLoadingCache.LoadingCacheViewlocalCacheView(package private) static java.util.logging.Loggerlogger
-
Constructor Summary
Constructors Constructor Description LocalAsyncLoadingCache(C cache, AsyncCacheLoader<? super K,V> loader)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description private static booleancanBulkLoad(AsyncCacheLoader<?,?> loader)Returns whether the supplied cache loader has bulk load functionality.private java.util.concurrent.CompletableFuture<java.util.Map<K,V>>composeResult(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futures)Returns a future that waits for all of the dependent futures to complete and returns the combined mapping if successful.java.util.concurrent.CompletableFuture<V>get(K key)Returns the future associated withkeyin this cache, obtaining that value fromCacheLoader.asyncLoad(K, java.util.concurrent.Executor)if necessary.java.util.concurrent.CompletableFuture<V>get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction)Returns the future associated withkeyin this cache, obtaining that value frommappingFunctionif necessary.(package private) java.util.concurrent.CompletableFuture<V>get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction, boolean recordStats)java.util.concurrent.CompletableFuture<V>get(K key, java.util.function.Function<? super K,? extends V> mappingFunction)Returns the future associated withkeyin this cache, obtaining that value frommappingFunctionif 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.private java.util.concurrent.CompletableFuture<java.util.Map<K,V>>getAllBulk(java.lang.Iterable<? extends K> keys)Computes all of the missing entries in a singleCacheLoader.asyncLoadAll(java.lang.Iterable<? extends K>, java.util.concurrent.Executor)call.java.util.concurrent.CompletableFuture<V>getIfPresent(java.lang.Object key)Returns the future associated withkeyin this cache, ornullif there is no cached future forkey.protected abstract Policy<K,V>policy()Returns the policy supported by this implementation and its configuration.voidput(K key, java.util.concurrent.CompletableFuture<V> valueFuture)Associatesvaluewithkeyin this cache.LoadingCache<K,V>synchronous()Returns a view of the entries stored in this cache as a synchronousLoadingCache.
-
-
-
Field Detail
-
logger
static final java.util.logging.Logger logger
-
cache
final C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>> cache
-
canBulkLoad
final boolean canBulkLoad
-
loader
final AsyncCacheLoader<K,V> loader
-
localCacheView
LocalAsyncLoadingCache.LoadingCacheView localCacheView
-
-
Constructor Detail
-
LocalAsyncLoadingCache
LocalAsyncLoadingCache(C cache, AsyncCacheLoader<? super K,V> loader)
-
-
Method Detail
-
policy
protected abstract Policy<K,V> policy()
Returns the policy supported by this implementation and its configuration.
-
canBulkLoad
private static boolean canBulkLoad(AsyncCacheLoader<?,?> loader)
Returns whether the supplied cache loader has bulk load functionality.
-
getIfPresent
public java.util.concurrent.CompletableFuture<V> getIfPresent(@Nonnull java.lang.Object key)
Description copied from interface:AsyncLoadingCacheReturns the future associated withkeyin this cache, ornullif there is no cached future forkey.- Specified by:
getIfPresentin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- Parameters:
key- key whose associated value is to be returned- Returns:
- the current (existing or computed) future value to which the specified key is mapped,
or
nullif this map contains no mapping for the key
-
get
public java.util.concurrent.CompletableFuture<V> get(@Nonnull K key, @Nonnull java.util.function.Function<? super K,? extends V> mappingFunction)
Description copied from interface:AsyncLoadingCacheReturns the future associated withkeyin this cache, obtaining that value frommappingFunctionif 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 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. If the asynchronous computation fails, the entry will be automatically removed.Warning: as with
CacheLoader.load(K),mappingFunctionmust not attempt to update any other mappings of this cache.- Specified by:
getin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- Parameters:
key- key with which the specified value is to be associatedmappingFunction- the function to asynchronously compute a value- Returns:
- the current (existing or computed) future value associated with the specified key
-
get
public java.util.concurrent.CompletableFuture<V> get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction)
Description copied from interface:AsyncLoadingCacheReturns the future associated withkeyin this cache, obtaining that value frommappingFunctionif 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 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. If the asynchronous computation fails, the entry will be automatically removed.Warning: as with
CacheLoader.load(K),mappingFunctionmust not attempt to update any other mappings of this cache.- Specified by:
getin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- Parameters:
key- key with which the specified value is to be associatedmappingFunction- the function to asynchronously compute a value- Returns:
- the current (existing or computed) future value associated with the specified key
-
get
java.util.concurrent.CompletableFuture<V> get(K key, java.util.function.BiFunction<? super K,java.util.concurrent.Executor,java.util.concurrent.CompletableFuture<V>> mappingFunction, boolean recordStats)
-
get
public java.util.concurrent.CompletableFuture<V> get(K key)
Description copied from interface:AsyncLoadingCacheReturns the future associated withkeyin this cache, obtaining that value fromCacheLoader.asyncLoad(K, java.util.concurrent.Executor)if necessary. If the asynchronous computation fails, the entry will be automatically removed.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:
getin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- 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:AsyncLoadingCacheReturns 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.Caches loaded by a
CacheLoadersupporting bulk loading will issue a single request toCacheLoader.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, java.util.function.Function<? super K, ? extends V>)tries to load the value for a key inkeys, that thread simply waits for this computation to finish and returns the loaded value. Caches that do not use aCacheLoaderwith an optimized bulk load implementation will sequentially load each key by making individualCacheLoader.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:
getAllin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- 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
-
getAllBulk
private java.util.concurrent.CompletableFuture<java.util.Map<K,V>> getAllBulk(java.lang.Iterable<? extends K> keys)
Computes all of the missing entries in a singleCacheLoader.asyncLoadAll(java.lang.Iterable<? extends K>, java.util.concurrent.Executor)call.
-
composeResult
private java.util.concurrent.CompletableFuture<java.util.Map<K,V>> composeResult(java.util.Map<K,java.util.concurrent.CompletableFuture<V>> futures)
Returns a future that waits for all of the dependent futures to complete and returns the combined mapping if successful. If any future fails then it is automatically removed from the cache if still present.
-
put
public void put(K key, java.util.concurrent.CompletableFuture<V> valueFuture)
Description copied from interface:AsyncLoadingCacheAssociatesvaluewithkeyin this cache. If the cache previously contained a value associated withkey, the old value is replaced byvalue. If the asynchronous computation fails, the entry will be automatically removed.Prefer
AsyncLoadingCache.get(Object, Function)when using the conventional "if cached, return; otherwise create, cache and return" pattern.- Specified by:
putin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- Parameters:
key- key with which the specified value is to be associatedvalueFuture- value to be associated with the specified key
-
synchronous
public LoadingCache<K,V> synchronous()
Description copied from interface:AsyncLoadingCacheReturns a view of the entries stored in this cache as a synchronousLoadingCache. 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:
synchronousin interfaceAsyncLoadingCache<C extends LocalCache<K,java.util.concurrent.CompletableFuture<V>>,K>- Returns:
- a thread-safe synchronous view of this cache
-
-