Top Description Methods
com.github.benmanes.caffeine.cache

public Interface CacheLoader<K, V>

extends AsyncCacheLoader<K, V>
Annotations
@FunctionalInterface
@SuppressWarnings:FunctionalInterfaceMethodChanged, PMD.SignatureDeclareThrowsException
Static Imports
java.util.Objects.requireNonNull

Computes or retrieves values, based on a key, for use in populating a LoadingCache or AsyncLoadingCache.

Most implementations will only need to implement load. Other methods may be overridden as desired.

Usage example:

CacheLoader<Key, Graph> loader = key -> createExpensiveGraph(key);
  LoadingCache<Key, Graph> cache = Caffeine.newBuilder().build(loader);
Author
[email protected] (Ben Manes)

Method Summary

Modifier and TypeMethod and Description
public default CompletableFuture<? extends V>

Returns:

the future value associated with key
asyncLoad
(K
the non-null key whose value should be loaded
key
,
Executor
the executor that asynchronously loads the entry
executor
)

Implements com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncLoad.

Asynchronously computes or retrieves the value corresponding to key.
public default CompletableFuture<? extends Map<? extends K, ? extends V>>

Returns:

a future containing the map from each key in keys to the value associated with that key; may not contain null values
asyncLoadAll
(Set<? extends K>
the unique, non-null keys whose values should be loaded
keys
,
Executor
the executor that with asynchronously loads the entries
executor
)

Overrides default com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncLoadAll.

Asynchronously computes or retrieves the values corresponding to keys.
public default CompletableFuture<? extends V>

Returns:

a future containing the new value associated with key, or containing null if the mapping is to be removed
asyncReload
(K
the non-null key whose value should be loaded
key
,
V
the non-null old value corresponding to key
oldValue
,
Executor
the executor with which the entry is asynchronously loaded
executor
)

Overrides default com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncReload.

Asynchronously computes or retrieves a replacement value corresponding to an already-cached key.
public static <
the key type
K
,
the value type
V
>
CacheLoader<K, V>

Returns:

a cache loader that delegates to the supplied mappingFunction
bulk
(Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>>
the function to compute the values
mappingFunction
)

Hides com.github.benmanes.caffeine.cache.AsyncCacheLoader.bulk.

Returns a cache loader that delegates to the supplied mapping function for retrieving the values.
public @Nullable V

Returns:

the value associated with key or null if not found
load
(K
the non-null key whose value should be loaded
key
)

Computes or retrieves the value corresponding to key.

public default Map<? extends K, ? extends V>

Returns:

a map from each key in keys to the value associated with that key; may not contain null values
loadAll
(Set<? extends K>
the unique, non-null keys whose values should be loaded
keys
)

Computes or retrieves the values corresponding to keys.

public default @Nullable V

Returns:

the new value associated with key, or null if the mapping is to be removed
reload
(K
the non-null key whose value should be loaded
key
,
V
the non-null old value corresponding to key
oldValue
)

Computes or retrieves a replacement value corresponding to an already-cached key.

Inherited from com.github.benmanes.caffeine.cache.AsyncCacheLoader:
bulk

Method Detail

asyncLoadback to summary
public default CompletableFuture<? extends V> asyncLoad(K key, Executor executor) throws Exception

Implements com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncLoad.

Asynchronously computes or retrieves the value corresponding to key.

Parameters
key:K

the non-null key whose value should be loaded

executor:Executor

the executor that asynchronously loads the entry

Returns:CompletableFuture<? extends V>

the future value associated with key

Annotations
@Override
Exceptions
Exception:

Doc from com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncLoad.

or Error, in which case the mapping is unchanged

asyncLoadAllback to summary
public default CompletableFuture<? extends Map<? extends K, ? extends V>> asyncLoadAll(Set<? extends K> keys, Executor executor) throws Exception

Overrides default com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncLoadAll.

Asynchronously computes or retrieves the values corresponding to keys. This method is called by AsyncLoadingCache#getAll.

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 will defer to individual calls to AsyncLoadingCache#get if this method is not overridden.

Warning

loading must not attempt to update any mappings of this cache directly.

Parameters
keys:Set<? extends K>

the unique, non-null keys whose values should be loaded

executor:Executor

the executor that with asynchronously loads the entries

Returns:CompletableFuture<? extends Map<? extends K, ? extends V>>

a future containing the map from each key in keys to the value associated with that key; may not contain null values

Annotations
@Override
Exceptions
Exception:

Doc from com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncLoadAll.

or Error, in which case the mappings are unchanged

asyncReloadback to summary
public default CompletableFuture<? extends V> asyncReload(K key, V oldValue, Executor executor) throws Exception

Overrides default com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncReload.

Asynchronously computes or retrieves a replacement value corresponding to an already-cached 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, or through a call to LoadingCache#refresh.

Warning

loading must not attempt to update any mappings of this cache directly or block waiting for other cache operations to complete.

Note

all exceptions thrown by this method will be logged and then swallowed.

Parameters
key:K

the non-null key whose value should be loaded

oldValue:V

the non-null old value corresponding to key

executor:Executor

the executor with which the entry is asynchronously loaded

Returns:CompletableFuture<? extends V>

a future containing the new value associated with key, or containing null if the mapping is to be removed

Annotations
@Override
Exceptions
Exception:

Doc from com.github.benmanes.caffeine.cache.AsyncCacheLoader.asyncReload.

or Error, in which case the mapping is unchanged

bulkback to summary
public static <K, V> CacheLoader<K, V> bulk(Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction)

Hides com.github.benmanes.caffeine.cache.AsyncCacheLoader.bulk.

Returns a cache loader that delegates to the supplied mapping function for retrieving the values. Note that load will silently discard any additional mappings loaded when retrieving the key prior to returning to the value to the cache.

Usage example:

CacheLoader<Key, Graph> loader = CacheLoader.bulk(keys -> createExpensiveGraphs(keys));
  LoadingCache<Key, Graph> cache = Caffeine.newBuilder().build(loader);
Parameters
<K>
the key type
<V>
the value type
mappingFunction:Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>>

the function to compute the values

Returns:CacheLoader<K, V>

a cache loader that delegates to the supplied mappingFunction

Annotations
@SuppressWarnings:FunctionalInterfaceClash
Exceptions
NullPointerException:
if the mappingFunction is null
loadback to summary
public @Nullable V load(K key) throws Exception

Computes or retrieves the value corresponding to key.

Warning

loading must not attempt to update any mappings of this cache directly.

Parameters
key:K

the non-null key whose value should be loaded

Returns:@Nullable V

the value associated with key or null if not found

Exceptions
Exception:
or Error, in which case the mapping is unchanged
InterruptedException:
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
loadAllback to summary
public default Map<? extends K, ? extends V> loadAll(Set<? extends K> keys) throws Exception

Computes or retrieves the values corresponding to keys. This method is called by LoadingCache#getAll.

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 will defer to individual calls to LoadingCache#get if this method is not overridden.

Warning

loading must not attempt to update any mappings of this cache directly.

Parameters
keys:Set<? extends K>

the unique, non-null keys whose values should be loaded

Returns:Map<? extends K, ? extends V>

a map from each key in keys to the value associated with that key; may not contain null values

Exceptions
Exception:
or Error, in which case the mappings are unchanged
InterruptedException:
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
reloadback to summary
public default @Nullable V reload(K key, V oldValue) throws Exception

Computes or retrieves a replacement value corresponding to an already-cached 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, or through a call to LoadingCache#refresh.

Warning

loading must not attempt to update any mappings of this cache directly or block waiting for other cache operations to complete.

Note

all exceptions thrown by this method will be logged and then swallowed.

Parameters
key:K

the non-null key whose value should be loaded

oldValue:V

the non-null old value corresponding to key

Returns:@Nullable V

the new value associated with key, or null if the mapping is to be removed

Exceptions
Exception:
or Error, in which case the mapping is unchanged
InterruptedException:
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