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:
<p>
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.InvalidCacheLoadException
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 |
---|---|
abstract V |
load(K key)
Computes or retrieves the value corresponding to
key . |
ListenableFuture<V> |
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 loadedkey
; must not be nullException
- if unable to load the resultInterruptedException
- 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 setpublic ListenableFuture<V> reload(K key, V oldValue) throws Exception
key
. This
method is called when an existing cache entry is refreshed by
CacheBuilder#refreshAfterWrite
, or through a call to LoadingCache#refresh
.
This implementation synchronously delegates to load(K)
. It is recommended that it be
overridden with an asynchronous implementation when using
CacheBuilder#refreshAfterWrite
.
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
;
must not be null, must not return nullException
- if unable to reload the resultInterruptedException
- 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 setCopyright © 2007-2024, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms.