@GwtCompatible(emulated=true) public abstract class CacheLoader<K,V> extends Object
LoadingCache.
Most implementations will only need to implement load(K). Other methods may be overridden as desired.
Usage example:
CacheLoader<Key, Graph> loader = new CacheLoader<Key, Graph>() { public Graph load(Key key) throws AnyException { return createExpensiveGraph(key); } }; LoadingCache<Key, Graph> cache = CacheBuilder.newBuilder().build(loader);
| Modifier and Type | Class and Description |
|---|---|
static class |
CacheLoader
Thrown to indicate that an invalid response was returned from a call to
CacheLoader.
|
| Modifier | Constructor and Description |
|---|---|
protected |
CacheLoader()
Constructor for use by subclasses.
|
| Modifier and Type | Method and Description |
|---|---|
static <K |
asyncReloading(CacheLoader
|
static <K |
from(Function
Returns a cache loader based on an
existing function instance.
|
static <V> CacheLoader |
from(Supplier
Returns a cache loader based on an
existing supplier instance.
|
abstract V |
load(K key)
Computes or retrieves the value corresponding to
key.
|
Map |
loadAll(Iterable
Computes or retrieves the values corresponding to
keys.
|
ListenableFuture |
reload(K key, V oldValue)
Computes or retrieves a replacement value corresponding to an already-cached
key.
|
public abstract V load(K key) throws Exception
key.
key - the non-null key whose value should be loaded
key;
must not be null
Exception - if unable to load the result
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
@GwtIncompatible(value="Futures") public ListenableFuture<V> reload(K key, V oldValue) throws Exception
key. This method is called when an existing cache entry is refreshed by
CacheBuilder.refreshAfterWrite(long, java.util.concurrent.TimeUnit) , or through a call to
LoadingCache.refresh(K) .
This implementation synchronously delegates to load(K). It is recommended that it be overridden with an asynchronous implementation when using CacheBuilder.
Note: all exceptions thrown by this method will be logged and then swallowed.
key - the non-null key whose value should be loaded
oldValue - the non-null old value corresponding to
key
key;
must not be null, must not return null
Exception - if unable to reload the result
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
public Map<K ,V> loadAll(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, but getAll will throw an exception. 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 overriden when bulk retrieval is significantly more efficient than many individual lookups. Note that LoadingCache will defer to individual calls to LoadingCache if this method is not overriden.
keys - the unique, non-null keys whose values should be loaded
keys to the value associated with that key;
may not contain null values
Exception - if unable to load the result
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
@Beta public static <K,V> CacheLoader <K ,V> from(Function <K ,V> function)
CacheLoader and implement
load instead.
function - the function to be used for loading values; must never return
null
function
@Beta public static <V> CacheLoader<Object ,V> from(Supplier <V> supplier)
CacheLoader and implement
load instead.
supplier - the supplier to be used for loading values; must never return
null
Supplier.get() , irrespective of the key
@Beta @GwtIncompatible(value="Executor + Futures") public static <K,V> CacheLoader <K ,V> asyncReloading(CacheLoader <K ,V> loader, Executor executor)
CacheLoader which wraps
loader, executing calls to
reload(K, V) using
executor.
This method is useful only when loader.reload has a synchronous implementation, such as the default implementation.