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.
@ThreadSafe @FunctionalInterface public interface AsyncCacheLoader<K,V>
Computes or retrieves values asynchronously, based on a key, for use in populating aAsyncLoadingCache
.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
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description java.util.concurrent.CompletableFuture<V>
asyncLoad(K key, java.util.concurrent.Executor executor)
Asynchronously computes or retrieves the value corresponding tokey
.default java.util.concurrent.CompletableFuture<java.util.Map<K,V>>
asyncLoadAll(java.lang.Iterable<? extends K> keys, java.util.concurrent.Executor executor)
Asynchronously computes or retrieves the values corresponding tokeys
.default java.util.concurrent.CompletableFuture<V>
asyncReload(K key, V oldValue, java.util.concurrent.Executor executor)
Asynchronously computes or retrieves a replacement value corresponding to an already-cachedkey
.
-
-
-
Method Detail
-
asyncLoad
@Nonnull java.util.concurrent.CompletableFuture<V> asyncLoad(@Nonnull K key, @Nonnull java.util.concurrent.Executor executor)
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 java.util.concurrent.CompletableFuture<java.util.Map<K,V>> asyncLoadAll(@Nonnull java.lang.Iterable<? extends K> keys, @Nonnull java.util.concurrent.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 java.util.concurrent.CompletableFuture<V> asyncReload(@Nonnull K key, @Nonnull V oldValue, @Nonnull java.util.concurrent.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
-
-