Interface AsyncLoadingCache<K,V extends @Nullable Object>
- Type Parameters:
K- the type of keys maintained by this cacheV- the type of mapped values
- All Superinterfaces:
AsyncCache<K,V>
Implementations of this interface are expected to be thread-safe and can be safely accessed by multiple concurrent threads.
-
Method Summary
Modifier and TypeMethodDescriptionReturns the future associated with thekeyin this cache, obtaining that value fromAsyncCacheLoader.asyncLoad(K, java.util.concurrent.Executor)if necessary.Returns the future of a map of the values associated withkeys, creating or retrieving those values if necessary.Returns a view of the entries stored in this cache as a synchronousLoadingCache.Methods inherited from interface com.github.benmanes.caffeine.cache.AsyncCache
asMap, get, get, getAll, getAll, getIfPresent, put
-
Method Details
-
get
Returns the future associated with thekeyin 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.- Parameters:
key- the key with which the specified value is to be associated- Returns:
- the current (existing or computed) future value associated with the specified key
- Throws:
NullPointerException- if the specified key is null or if the future returned by theAsyncCacheLoaderis nullRuntimeException- or Error if theAsyncCacheLoaderdoes when constructing the future, in which case the mapping is left unestablished
-
getAll
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 any of the asynchronous computations fail, those entries will be automatically removed from this cache.Caches loaded by a
AsyncCacheLoadersupporting bulk loading will issue a single request toAsyncCacheLoader.asyncLoadAll(java.util.Set<? extends K>, java.util.concurrent.Executor)for all keys that are not already present in the cache. If another call toget(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 aAsyncCacheLoaderwith 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.- Parameters:
keys- the keys whose associated values are to be returned- Returns:
- a future containing an unmodifiable mapping of keys to values for the specified keys in this cache
- Throws:
NullPointerException- if the specified collection is null or contains a null element, or if the future returned by theAsyncCacheLoaderis nullRuntimeException- or Error if theAsyncCacheLoaderdoes so, ifAsyncCacheLoader.asyncLoadAll(java.util.Set<? extends K>, java.util.concurrent.Executor)returnsnull, or fails when constructing the future, in which case the mapping is left unestablished
-
synchronous
LoadingCache<K,V> synchronous()Returns 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 interfaceAsyncCache<K,V extends @Nullable Object> - Returns:
- a thread-safe synchronous view of this cache
-