Top Description Inners Constructors Methods
com.google.common.cache

public abstract Class CacheLoader<K, V>

extends Object
Class Inheritance
Known Direct Subclasses
com.google.common.cache.CacheLoader.FunctionToCacheLoader, com.google.common.cache.CacheLoader.SupplierToCacheLoader
Annotations
@GwtCompatible
emulated:true
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull

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

Most implementations will only need to implement load. 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, it can also be specified much more simply:

CacheLoader<Key, Graph> loader = CacheLoader.from(key -> createExpensiveGraph(key));
Author
Charles Fry
Since
10.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
public static class
CacheLoader.InvalidCacheLoadException

Thrown to indicate that an invalid response was returned from a call to CacheLoader.

private static class
public static class
CacheLoader.UnsupportedLoadingOperationException

Exception thrown by loadAll() to indicate that it is not supported.

Constructor Summary

AccessConstructor and Description
protected
CacheLoader()

Constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public static <K, V> CacheLoader<K, V>
asyncReloading(final CacheLoader<K, V> loader, final Executor executor)

Returns a CacheLoader which wraps loader, executing calls to CacheLoader#reload using executor.

public static <K, V> CacheLoader<K, V>

Returns:

a cache loader that loads values by passing each key to function
from
(Function<K, V>
the function to be used for loading values; must never return null
function
)

Returns a cache loader that uses function to load keys, without supporting either reloading or bulk loading.

public static <V> CacheLoader<Object, V>

Returns:

a cache loader that loads values by calling Supplier#get, irrespective of the key
from
(Supplier<V>
the supplier to be used for loading values; must never return null
supplier
)

Returns a cache loader based on an existing supplier instance.

public abstract V

Returns:

the value associated with key; must not be null
load
(K
the non-null key whose value should be loaded
key
)

Computes or retrieves the value corresponding to key.

public Map<K, V>

Returns:

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

Computes or retrieves the values corresponding to keys.

public ListenableFuture<V>

Returns:

the future new value associated with key; must not be null, must not return null
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 java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

CacheLoaderback to summary
protected CacheLoader()

Constructor for use by subclasses.

Method Detail

asyncReloadingback to summary
public static <K, V> CacheLoader<K, V> asyncReloading(final CacheLoader<K, V> loader, final Executor executor)

Returns a CacheLoader which wraps loader, executing calls to CacheLoader#reload using executor.

This method is useful only when loader.reload has a synchronous implementation, such as the default implementation.

Annotations
@GwtIncompatible
Since
17.0
fromback to summary
public static <K, V> CacheLoader<K, V> from(Function<K, V> function)

Returns a cache loader that uses function to load keys, without supporting either reloading or bulk loading. This allows creating a cache loader using a lambda expression.

The returned object is serializable if function is serializable.

Parameters
function:Function<K, V>

the function to be used for loading values; must never return null

Returns:CacheLoader<K, V>

a cache loader that loads values by passing each key to function

fromback to summary
public static <V> CacheLoader<Object, V> from(Supplier<V> supplier)

Returns 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 subclass CacheLoader and implement load instead.

The returned object is serializable if supplier is serializable.

Parameters
supplier:Supplier<V>

the supplier to be used for loading values; must never return null

Returns:CacheLoader<Object, V>

a cache loader that loads values by calling Supplier#get, irrespective of the key

loadback to summary
public abstract V load(K key) throws Exception

Computes or retrieves the value corresponding to key.

Parameters
key:K

the non-null key whose value should be loaded

Returns:V

the value associated with key; must not be null

Exceptions
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
loadAllback to summary
public Map<K, V> loadAll(Iterable<? 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, 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 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.

Parameters
keys:Iterable<? extends K>

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

Returns:Map<K, V>

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

Exceptions
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
Since
11.0
reloadback to summary
public ListenableFuture<V> reload(K key, V oldValue) throws Exception

Computes or retrieves a replacement value corresponding to an already-cached 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. 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.

Parameters
key:K

the non-null key whose value should be loaded

oldValue:V

the non-null old value corresponding to key

Returns:ListenableFuture<V>

the future new value associated with key; must not be null, must not return null

Annotations
@GwtIncompatible
Exceptions
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
Since
11.0
com.google.common.cache back to summary

private final Class CacheLoader.FunctionToCacheLoader<K, V>

extends CacheLoader<K, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Field Summary

Modifier and TypeField and Description
private final Function<K, V>
private static final long

Constructor Summary

AccessConstructor and Description
public
FunctionToCacheLoader(Function<K, V> computingFunction)

Method Summary

Modifier and TypeMethod and Description
public V
load(K
the non-null key whose value should be loaded
key
)

Implements abstract com.google.common.cache.CacheLoader.load.

Computes or retrieves the value corresponding to key.
Inherited from com.google.common.cache.CacheLoader:
asyncReloadingfromfromloadAllreload

Field Detail

computingFunctionback to summary
private final Function<K, V> computingFunction
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

FunctionToCacheLoaderback to summary
public FunctionToCacheLoader(Function<K, V> computingFunction)

Method Detail

loadback to summary
public V load(K key)

Implements abstract com.google.common.cache.CacheLoader.load.

Doc from com.google.common.cache.CacheLoader.load.

Computes or retrieves the value corresponding to key.

Parameters
key:K

the non-null key whose value should be loaded

Returns:V

the value associated with key; must not be null

Annotations
@Override
com.google.common.cache back to summary

public final Class CacheLoader.InvalidCacheLoadException

extends RuntimeException
Class Inheritance

Thrown to indicate that an invalid response was returned from a call to CacheLoader.
Since
11.0

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

InvalidCacheLoadExceptionback to summary
public InvalidCacheLoadException(String message)
com.google.common.cache back to summary

private final Class CacheLoader.SupplierToCacheLoader<V>

extends CacheLoader<Object, V>
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Field Summary

Modifier and TypeField and Description
private final Supplier<V>
private static final long

Constructor Summary

AccessConstructor and Description
public
SupplierToCacheLoader(Supplier<V> computingSupplier)

Method Summary

Modifier and TypeMethod and Description
public V
load(Object
the non-null key whose value should be loaded
key
)

Implements abstract com.google.common.cache.CacheLoader.load.

Computes or retrieves the value corresponding to key.
Inherited from com.google.common.cache.CacheLoader:
asyncReloadingfromfromloadAllreload

Field Detail

computingSupplierback to summary
private final Supplier<V> computingSupplier
serialVersionUIDback to summary
private static final long serialVersionUID

Constructor Detail

SupplierToCacheLoaderback to summary
public SupplierToCacheLoader(Supplier<V> computingSupplier)

Method Detail

loadback to summary
public V load(Object key)

Implements abstract com.google.common.cache.CacheLoader.load.

Doc from com.google.common.cache.CacheLoader.load.

Computes or retrieves the value corresponding to key.

Parameters
key:Object

the non-null key whose value should be loaded

Returns:V

the value associated with key; must not be null

Annotations
@Override
com.google.common.cache back to summary

public final Class CacheLoader.UnsupportedLoadingOperationException

extends UnsupportedOperationException
Class Inheritance

Exception thrown by loadAll() to indicate that it is not supported.
Since
19.0

Constructor Summary

AccessConstructor and Description
pack-priv

Constructor Detail

UnsupportedLoadingOperationExceptionback to summary
pack-priv UnsupportedLoadingOperationException()