@FunctionalInterface public interface CacheLoader<K,V> extends AsyncCacheLoader<K,V>
LoadingCache
or
AsyncLoadingCache
.
Most implementations will only need to implement load(K)
. Other methods may be
overridden as desired.
Usage example:
CacheLoader<Key, Graph> loader = key -> createExpensiveGraph(key);
LoadingCache<Key, Graph> cache = Caffeine.newBuilder().build(loader);
Modifier and Type | Method and Description |
---|---|
default @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 . |
V |
load(K key)
Computes or retrieves the value corresponding to
key . |
default @NonNull Map<K,V> |
loadAll(@NonNull Iterable<? extends K> keys)
Computes or retrieves the values corresponding to
keys . |
default V |
reload(K key,
V oldValue)
Computes or retrieves a replacement value corresponding to an already-cached
key . |
V load(K key) throws Exception
key
.
Warning: loading must not attempt to update any mappings of this cache directly.
key
- the non-null key whose value should be loadedkey
or null
if not foundException
- or Error, in which case the mapping is unchangedInterruptedException
- if this method is interrupted. InterruptedException
is
treated like any other Exception
in all respects except that, when it is
caught, the thread's interrupt status is setdefault @NonNull Map<K,V> loadAll(@NonNull Iterable<? extends K> keys) throws Exception
keys
. This method is called by
LoadingCache.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 LoadingCache.getAll(java.lang.Iterable<? extends K>)
will defer to individual calls to
LoadingCache.get(K)
if this method is not overridden.
Warning: loading must not attempt to update any mappings of this cache directly.
keys
- the unique, non-null keys whose values should be loadedkeys
to the value associated with that key; may not
contain null valuesException
- or Error, in which case the mappings are unchangedInterruptedException
- if this method is interrupted. InterruptedException
is
treated like any other Exception
in all respects except that, when it is
caught, the thread's interrupt status is setdefault @NonNull CompletableFuture<V> asyncLoad(K key, @NonNull Executor executor)
key
.asyncLoad
in interface AsyncCacheLoader<K,V>
key
- the non-null key whose value should be loadedexecutor
- the executor that asynchronously loads the entrykey
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.
asyncLoadAll
in interface AsyncCacheLoader<K,V>
keys
- the unique, non-null keys whose values should be loadedexecutor
- the executor that with asynchronously loads the entrieskeys
to the value associated with
that key; may not contain null valuesdefault V reload(K key, V oldValue) throws Exception
key
. If
the replacement value is not found then the mapping will be removed if null
is
returned. 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
key
, or null
if the mapping is to be
removedException
- or Error, in which case the mapping is unchangedInterruptedException
- if this method is interrupted. InterruptedException
is
treated like any other Exception
in all respects except that, when it is
caught, the thread's interrupt status is setdefault @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.
asyncReload
in interface AsyncCacheLoader<K,V>
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