@ThreadSafe @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 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. |
V |
load(K key)
Computes or retrieves the value corresponding to
key. |
default Map<K,V> |
loadAll(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. |
@Nullable V load(@Nonnull 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 set@Nonnull default 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 set@Nonnull default CompletableFuture<V> asyncLoad(@Nonnull 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@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.
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 values@Nullable default V reload(@Nonnull K key, @Nonnull 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 keykey, 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 set@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.
asyncReload in interface AsyncCacheLoader<K,V>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