Interface AsyncCacheLoader<K,V>
- All Known Subinterfaces:
CacheLoader<K,
V>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
AsyncLoadingCache
.
Most implementations will only need to implement asyncLoad(K, java.util.concurrent.Executor)
. Other methods may be
overridden as desired.
Usage example:
AsyncCacheLoader<Key, Graph> loader = (key, executor) ->
createExpensiveGraphAsync(key, executor);
AsyncLoadingCache<Key, Graph> cache = Caffeine.newBuilder().buildAsync(loader);
-
Method Summary
Modifier and TypeMethodDescriptionAsynchronously computes or retrieves the value corresponding tokey
.default CompletableFuture<Map<K,
V>> asyncLoadAll
(Iterable<? extends K> keys, Executor executor) Asynchronously computes or retrieves the values corresponding tokeys
.default CompletableFuture<V>
asyncReload
(K key, V oldValue, Executor executor) Asynchronously computes or retrieves a replacement value corresponding to an already-cachedkey
.
-
Method Details
-
asyncLoad
Asynchronously computes or retrieves the value corresponding tokey
.- Parameters:
key
- the non-null key whose value should be loadedexecutor
- the executor with which the entry is asynchronously loaded- Returns:
- the future value associated with
key
-
asyncLoadAll
@Nonnull default CompletableFuture<Map<K,V>> asyncLoadAll(@Nonnull Iterable<? extends K> keys, @Nonnull Executor executor) Asynchronously computes or retrieves the values corresponding tokeys
. This method is called byAsyncLoadingCache.getAll(java.lang.Iterable<? extends K>)
.If the returned map doesn't contain all requested
keys
then the entries it does contain will be cached andgetAll
will return the partial results. If the returned map contains extra keys not present inkeys
then all returned entries will be cached, but only the entries forkeys
will be returned fromgetAll
.This method should be overridden when bulk retrieval is significantly more efficient than many individual lookups. Note that
AsyncLoadingCache.getAll(java.lang.Iterable<? extends K>)
will defer to individual calls toAsyncLoadingCache.get(K, java.util.function.Function<? super K, ? extends V>)
if this method is not overridden.- Parameters:
keys
- the unique, non-null keys whose values should be loadedexecutor
- the executor with which the entries are asynchronously loaded- Returns:
- a future containing the map from each key in
keys
to the value associated with that key; may not contain null values
-
asyncReload
@Nonnull default CompletableFuture<V> asyncReload(@Nonnull K key, @Nonnull V oldValue, @Nonnull Executor executor) Asynchronously computes or retrieves a replacement value corresponding to an already-cachedkey
. If the replacement value is not found then the mapping will be removed ifnull
is computed. This method is called when an existing cache entry is refreshed byCaffeine.refreshAfterWrite(long, java.util.concurrent.TimeUnit)
, or through a call toLoadingCache.refresh(K)
.Note: all exceptions thrown by this method will be logged and then swallowed.
- Parameters:
key
- the non-null key whose value should be loadedoldValue
- the non-null old value corresponding tokey
executor
- the executor with which the entry is asynchronously loaded- Returns:
- a future containing the new value associated with
key
, or containingnull
if the mapping is to be removed
-