Class CacheLoader<K,V> 
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);
Since this example doesn't support reloading or bulk loading, if you're able to use lambda expressions it can be specified even more easily:
CacheLoader<Key, Graph> loader = CacheLoader.from(key -> createExpensiveGraph(key));
- Since:
- 10.0
- Author:
- Charles Fry
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic final classThrown to indicate that an invalid response was returned from a call toCacheLoader.static final classException thrown byloadAll()to indicate that it is not supported.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic <K,V> CacheLoader <K, V> asyncReloading(CacheLoader<K, V> loader, Executor executor) static <K,V> CacheLoader <K, V> Returns a cache loader that usesfunctionto load keys, and without supporting either reloading or bulk loading.static <V> CacheLoader<Object, V> Returns a cache loader based on an existing supplier instance.abstract VComputes or retrieves the value corresponding tokey.Computes or retrieves the values corresponding tokeys.Computes or retrieves a replacement value corresponding to an already-cachedkey.
- 
Constructor Details- 
CacheLoaderprotected CacheLoader()Constructor for use by subclasses.
 
- 
- 
Method Details- 
loadComputes 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 result
- InterruptedException- if this method is interrupted.- InterruptedExceptionis treated like any other- Exceptionin all respects except that, when it is caught, the thread's interrupt status is set
 
- 
reloadComputes or retrieves a replacement value corresponding to an already-cachedkey. This method is called when an existing cache entry is refreshed byCacheBuilder.refreshAfterWrite(java.time.Duration), or through a call toLoadingCache.refresh(K).This implementation synchronously delegates to load(K). It is recommended that it be overridden with an asynchronous implementation when usingCacheBuilder.refreshAfterWrite(java.time.Duration).Note: all exceptions thrown by this method will be logged and then swallowed. - Parameters:
- key- the non-null key whose value should be loaded
- oldValue- the non-null old value corresponding to- key
- Returns:
- the future new value associated with key; must not be null, must not return null
- Throws:
- Exception- if unable to reload the result
- InterruptedException- if this method is interrupted.- InterruptedExceptionis treated like any other- Exceptionin all respects except that, when it is caught, the thread's interrupt status is set
- Since:
- 11.0
 
- 
loadAllComputes or retrieves the values corresponding tokeys. This method is called byLoadingCache.getAll(java.lang.Iterable<? extends K>).If the returned map doesn't contain all requested keysthen the entries it does contain will be cached, butgetAllwill throw an exception. 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(java.lang.Iterable<? extends K>)will defer to individual calls toLoadingCache.get(K)if this method is not overridden.- 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- if unable to load the result
- InterruptedException- if this method is interrupted.- InterruptedExceptionis treated like any other- Exceptionin all respects except that, when it is caught, the thread's interrupt status is set
- Since:
- 11.0
 
- 
fromReturns a cache loader that usesfunctionto load keys, and without supporting either reloading or bulk loading. This is most useful when you can pass a lambda expression. Otherwise it is useful mostly when you already have an existing function instance.The returned object is serializable if functionis serializable.- Parameters:
- function- the function to be used for loading values; must never return- null
- Returns:
- a cache loader that loads values by passing each key to function
 
- 
fromReturns a cache loader based on an existing supplier instance. Note that there's no need to create a new supplier just to pass it in here; just subclassCacheLoaderand implementloadinstead.The returned object is serializable if supplieris serializable.- Parameters:
- supplier- the supplier to be used for loading values; must never return- null
- Returns:
- a cache loader that loads values by calling Supplier.get(), irrespective of the key
 
- 
asyncReloading@GwtIncompatible public static <K,V> CacheLoader<K,V> asyncReloading(CacheLoader<K, V> loader, Executor executor) Returns aCacheLoaderwhich wrapsloader, executing calls toreload(K, V)usingexecutor.This method is useful only when loader.reloadhas a synchronous implementation, such as the default implementation.- Since:
- 17.0
 
 
-