Interface CacheLoader<K,V>
- All Superinterfaces:
AsyncCacheLoader<K,V>
- All Known Implementing Classes:
BoundedLocalCache.BoundedLocalAsyncLoadingCache.AsyncLoader, CaffeinatedGuavaLoadingCache.BulkLoader, CaffeinatedGuavaLoadingCache.SingleLoader, JCacheLoaderAdapter
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
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);
-
Method Summary
Modifier and TypeMethodDescriptiondefault @NonNull CompletableFuture<V> Asynchronously computes or retrieves the value corresponding tokey.default @NonNull CompletableFuture<Map<@NonNull K, @NonNull V>> asyncLoadAll(@NonNull Iterable<? extends K> keys, @NonNull Executor executor) Asynchronously computes or retrieves the values corresponding tokeys.default @NonNull CompletableFuture<V> asyncReload(@NonNull K key, @NonNull V oldValue, @NonNull Executor executor) Asynchronously computes or retrieves a replacement value corresponding to an already-cachedkey.@Nullable VComputes or retrieves the value corresponding tokey.Computes or retrieves the values corresponding tokeys.default @Nullable VComputes or retrieves a replacement value corresponding to an already-cachedkey.
-
Method Details
-
load
Computes or retrieves the value corresponding tokey.Warning: loading must not attempt to update any mappings of this cache directly.
- Parameters:
key- the non-null key whose value should be loaded- Returns:
- the value associated with
keyornullif not found - Throws:
Exception- or Error, in which case the mapping is unchangedInterruptedException- if this method is interrupted.InterruptedExceptionis treated like any otherExceptionin all respects except that, when it is caught, the thread's interrupt status is set
-
loadAll
default @NonNull Map<@NonNull K, @NonNull V> loadAll(@NonNull Iterable<? extends @NonNull K> keys) throws Exception Computes or retrieves the values corresponding tokeys. This method is called byLoadingCache.getAll(Iterable).If the returned map doesn't contain all requested
keysthen the entries it does contain will be cached andgetAllwill return the partial results. If the returned map contains extra keys not present inkeysthen all returned entries will be cached, but only the entries forkeyswill be returned fromgetAll.This method should be overridden when bulk retrieval is significantly more efficient than many individual lookups. Note that
LoadingCache.getAll(Iterable)will defer to individual calls toLoadingCache.get(K)if this method is not overridden.Warning: loading must not attempt to update any mappings of this cache directly.
- Parameters:
keys- the unique, non-null keys whose values should be loaded- Returns:
- a map from each key in
keysto the value associated with that key; may not contain null values - Throws:
Exception- or Error, in which case the mappings are unchangedInterruptedException- if this method is interrupted.InterruptedExceptionis treated like any otherExceptionin all respects except that, when it is caught, the thread's interrupt status is set
-
asyncLoad
Asynchronously computes or retrieves the value corresponding tokey.- Specified by:
asyncLoadin interfaceAsyncCacheLoader<K,V> - Parameters:
key- the non-null key whose value should be loadedexecutor- the executor that asynchronously loads the entry- Returns:
- the future value associated with
key
-
asyncLoadAll
default @NonNull CompletableFuture<Map<@NonNull K, @NonNull V>> asyncLoadAll(@NonNull Iterable<? extends K> keys, @NonNull Executor executor) Asynchronously computes or retrieves the values corresponding tokeys. This method is called byAsyncLoadingCache.getAll(Iterable).If the returned map doesn't contain all requested
keysthen the entries it does contain will be cached andgetAllwill return the partial results. If the returned map contains extra keys not present inkeysthen all returned entries will be cached, but only the entries forkeyswill be returned fromgetAll.This method should be overridden when bulk retrieval is significantly more efficient than many individual lookups. Note that
AsyncLoadingCache.getAll(Iterable)will defer to individual calls toAsyncLoadingCache.get(K)if this method is not overridden.- Specified by:
asyncLoadAllin interfaceAsyncCacheLoader<K,V> - Parameters:
keys- the unique, non-null keys whose values should be loadedexecutor- the executor that with asynchronously loads the entries- Returns:
- a future containing the map from each key in
keysto the value associated with that key; may not contain null values
-
reload
Computes or retrieves a replacement value corresponding to an already-cachedkey. If the replacement value is not found then the mapping will be removed ifnullis returned. This method is called when an existing cache entry is refreshed byCaffeine.refreshAfterWrite(Duration), 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- Returns:
- the new value associated with
key, ornullif the mapping is to be removed - Throws:
Exception- or Error, in which case the mapping is unchangedInterruptedException- if this method is interrupted.InterruptedExceptionis treated like any otherExceptionin all respects except that, when it is caught, the thread's interrupt status is set
-
asyncReload
default @NonNull CompletableFuture<V> asyncReload(@NonNull K key, @NonNull V oldValue, @NonNull 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 ifnullis computed. This method is called when an existing cache entry is refreshed byCaffeine.refreshAfterWrite(Duration), or through a call toLoadingCache.refresh(K).Note: all exceptions thrown by this method will be logged and then swallowed.
- Specified by:
asyncReloadin interfaceAsyncCacheLoader<K,V> - Parameters:
key- the non-null key whose value should be loadedoldValue- the non-null old value corresponding tokeyexecutor- the executor with which the entry is asynchronously loaded- Returns:
- a future containing the new value associated with
key, or containingnullif the mapping is to be removed
-