@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 |
---|---|
@NonNull CompletableFuture<V> |
asyncLoad(K key,
@NonNull Executor executor)
Asynchronously computes or retrieves the value corresponding to
key . |
default @NonNull CompletableFuture<Map<K,V>> |
asyncLoadAll(@NonNull Iterable<? extends K> keys,
@NonNull Executor executor)
Asynchronously computes or retrieves the values corresponding to
keys . |
default @NonNull CompletableFuture<V> |
asyncReload(K key,
V oldValue,
@NonNull Executor executor)
Asynchronously computes or retrieves a replacement value corresponding to an already-cached
key . |
@NonNull CompletableFuture<V> asyncLoad(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
default @NonNull 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)
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 valuesdefault @NonNull CompletableFuture<V> asyncReload(K key, 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 key
executor
- the executor with which the entry is asynchronously loadedkey
, or containing
null
if the mapping is to be removed