Class CacheLoader<K,V>
- java.lang.Object
-
- org.glassfish.jersey.internal.guava.CacheLoader<K,V>
-
public abstract class CacheLoader<K,V> extends Object
Computes or retrieves values, based on a key, for use in populating aLoadingCache.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);- Since:
- 10.0
- Author:
- Charles Fry
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCacheLoader.InvalidCacheLoadExceptionThrown to indicate that an invalid response was returned from a call toCacheLoader.
-
Constructor Summary
Constructors Modifier Constructor Description protectedCacheLoader()Constructor for use by subclasses.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract Vload(K key)Computes or retrieves the value corresponding tokey.ListenableFuture<V>reload(K key, V oldValue)Computes or retrieves a replacement value corresponding to an already-cachedkey.
-
-
-
Method Detail
-
load
public abstract V load(K key) throws Exception
Computes or retrieves the value corresponding tokey.- Parameters:
key- the non-null key whose value should be loaded- Returns:
- the value associated with
key; must not be null - Throws:
Exception- if unable to load the resultInterruptedException- 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
-
reload
public ListenableFuture<V> reload(K key, V oldValue) throws Exception
Computes or retrieves a replacement value corresponding to an already-cachedkey. This method is called when an existing cache entry is refreshed byCacheBuilder#refreshAfterWrite, or through a call toLoadingCache#refresh.This implementation synchronously delegates to
load(K). It is recommended that it be overridden with an asynchronous implementation when usingCacheBuilder#refreshAfterWrite.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 future new value associated with
key; must not be null, must not return null - Throws:
Exception- if unable to reload the resultInterruptedException- 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- Since:
- 11.0
-
-