![JAR search and dependency download from the Maven repository](/logo.png)
javadoc.com.google.common.cache.CacheLoader.html Maven / Gradle / Ivy
CacheLoader (Guava: Google Core Libraries for Java 11.0.1 API)
Overview
Package
Class
Use
Tree
Deprecated
Index
Help
PREV CLASS
NEXT CLASS
FRAMES
NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD
DETAIL: FIELD | CONSTR | METHOD
com.google.common.cache
Class CacheLoader<K,V>
java.lang.Object
com.google.common.cache.CacheLoader<K,V>
@Beta
@GwtCompatible(emulated=true)
public abstract class CacheLoader<K,V>
- extends Object
Computes or retrieves values, based on a key, for use in populating a Cache
.
Most implementations will only need to implement load(K)
. Other methods may be
overridden as desired.
- Since:
- 10.0
- Author:
- Charles Fry
Nested Class Summary | |
---|---|
static class |
CacheLoader.InvalidCacheLoadException
Thrown to indicate that an invalid response was returned from a call to CacheLoader . |
Constructor Summary | |
---|---|
protected |
CacheLoader()
Constructor for use by subclasses. |
Method Summary | ||
---|---|---|
static
|
from(Function<K,V> function)
Returns a CacheLoader which creates values by applying a Function to the key. |
|
static
|
from(Supplier<V> supplier)
Returns a CacheLoader which obtains values from a Supplier (independent of the
key). |
|
abstract V |
load(K key)
Computes or retrieves the value corresponding to key . |
|
Map<K,V> |
loadAll(Iterable<? extends K> keys)
Computes or retrieves the values corresponding to keys . |
|
ListenableFuture<V> |
reload(K key,
V oldValue)
Computes or retrieves a replacement value corresponding to an already-cached key . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
CacheLoader
protected CacheLoader()
- Constructor for use by subclasses.
Method Detail |
---|
load
public abstract V load(K key) throws Exception
- Computes or retrieves the value corresponding to
key
.- Parameters:
key
- the non-null key whose value should be loaded- Returns:
- the value associated with
key
; must not be null - Throws:
Exception
reload
@GwtIncompatible(value="Futures") 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 byCacheBuilder.refreshAfterWrite(long, java.util.concurrent.TimeUnit)
, or through a call toCache#refresh
.This implementation synchronously delegates to
load(K)
. It is recommended that it be overridden with an asynchronous implementation when usingCacheBuilder.refreshAfterWrite(long, java.util.concurrent.TimeUnit)
.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
- Since:
- 11.0
loadAll
public Map<K,V> loadAll(Iterable<? extends K> keys) throws Exception
- Computes or retrieves the values corresponding to
keys
. This method is called byCache#getAll
.If the returned map doesn't contain all requested
keys
then the entries it does contain will be cached, butgetAll
will throw an exception. If the returned map contains extra keys not present inkeys
then all returned entries will be cached, but only the entries forkeys
will be returned fromgetAll
.This method should be overriden when bulk retrieval is significantly more efficient than many individual lookups. Note that
Cache#getAll
will defer to individual calls toCache.get(K, java.util.concurrent.Callable extends V>)
if this method is not overriden.- Parameters:
keys
- the unique, non-null keys whose values should be loaded- Returns:
- a map from each key in
keys
to the value associated with that key; may not contain null values - Throws:
Exception
- Since:
- 11.0
from
public static <K,V> CacheLoader<K,V> from(Function<K,V> function)
- Returns a
CacheLoader
which creates values by applying aFunction
to the key.
from
public static <V> CacheLoader<Object,V> from(Supplier<V> supplier)
- Returns a
CacheLoader
which obtains values from aSupplier
(independent of the key).
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 2010-2012. All Rights Reserved.