@ThreadSafe @FunctionalInterface public interface AsyncCacheLoader<K,V>
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);
| Modifier and Type | Method and Description |
|---|---|
CompletableFuture<V> |
asyncLoad(K key,
Executor executor)
Asynchronously computes or retrieves the value corresponding to
key. |
default CompletableFuture<Map<K,V>> |
asyncLoadAll(Iterable<? extends K> keys,
Executor executor)
Asynchronously computes or retrieves the values corresponding to
keys. |
default CompletableFuture<V> |
asyncReload(K key,
V oldValue,
Executor executor)
Asynchronously computes or retrieves a replacement value corresponding to an already-cached
key. |
@Nonnull CompletableFuture<V> asyncLoad(@Nonnull K key, @Nonnull Executor executor)
key.key - the non-null key whose value should be loadedexecutor - the executor with which the entry is asynchronously loadedkey@Nonnull default CompletableFuture<Map<K,V>> asyncLoadAll(@Nonnull Iterable<? extends K> keys, @Nonnull Executor executor)
keys. This method is
called by AsyncLoadingCache.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 and getAll will return the partial results. If the returned map contains
extra keys not present in keys then all returned entries will be cached, but only the
entries for keys will be returned from getAll.
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
to AsyncLoadingCache.get(K, java.util.function.Function<? super K, ? extends V>) if this method is not overridden.
keys - the unique, non-null keys whose values should be loadedexecutor - the executor with which the entries are asynchronously loadedkeys to the value associated with
that key; may not contain null values@Nonnull default CompletableFuture<V> asyncReload(@Nonnull K key, @Nonnull V oldValue, @Nonnull Executor executor)
key. If the replacement value is not found then the mapping will be removed if
null is computed. This method is called when an existing cache entry is refreshed by
Caffeine.refreshAfterWrite(java.time.Duration), or through a call to LoadingCache.refresh(K).
Note: all exceptions thrown by this method will be logged and then swallowed.
key - the non-null key whose value should be loadedoldValue - the non-null old value corresponding to keyexecutor - the executor with which the entry is asynchronously loadedkey, or containing
null if the mapping is to be removed