public abstract class ForwardingLoadingCache<K,V> extends ForwardingCache<K ,V> implements LoadingCache <K ,V>
Note that get(K)
, getUnchecked(K)
, and apply(K)
all expose the same underlying functionality, so should probably be overridden as a group.
Modifier and Type | Class and Description |
---|---|
static class |
ForwardingLoadingCache
A simplified version of
ForwardingLoadingCache where subclasses can pass in an already constructed
LoadingCache as the delegate.
|
ForwardingCache.SimpleForwardingCache <K,V>
Modifier | Constructor and Description |
---|---|
protected |
ForwardingLoadingCache()
Constructor for use by subclasses.
|
Modifier and Type | Method and Description |
---|---|
V |
apply(K key)
Returns the result of applying this function to
input .
|
protected abstract LoadingCache |
delegate()
Returns the backing delegate instance that methods are forwarded to.
|
V |
get(K key)
Returns the value associated with
key in this cache, first loading that value if necessary.
|
ImmutableMap |
getAll(Iterable
Returns a map of the values associated with
keys , creating or retrieving those values if necessary.
|
V |
getUnchecked(K key)
Returns the value associated with
key in this cache, first loading that value if necessary.
|
void |
refresh(K key)
Loads a new value for key
key , possibly asynchronously.
|
asMap, cleanUp, get, getAllPresent, getIfPresent, invalidate, invalidateAll, invalidateAll, put, putAll, size, stats
toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
asMap
cleanUp, get, getAllPresent, getIfPresent, invalidate, invalidateAll, invalidateAll, put, putAll, size, stats
protected ForwardingLoadingCache()
protected abstract LoadingCache<K ,V> delegate()
ForwardingObject
ForwardingSet.delegate()
. Concrete subclasses override this method to supply the instance being decorated.
public V get(K key) throws ExecutionException
LoadingCache
key
in this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes.
If another call to LoadingCache
or LoadingCache
is currently loading the value for key
, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.
Caches loaded by a CacheLoader
will call CacheLoader
to load new values into the cache. Newly loaded values are added to the cache using Cache.asMap().putIfAbsent
after loading has completed; if another value was associated with key
while the new value was loading then a removal notification will be sent for the new value.
If the cache loader associated with this cache is known not to throw checked exceptions, then prefer LoadingCache
over this method.
get
in interface
LoadingCache<K,V>
ExecutionException
- if a checked exception was thrown while loading the value. (
ExecutionException
is thrown
even if computation was interrupted by an InterruptedException
.)
public V getUnchecked(K key)
LoadingCache
key
in this cache, first loading that value if necessary. No observable state associated with this cache is modified until loading completes. Unlike
LoadingCache.get(K)
, this method does not throw a checked exception, and thus should only be used in situations where checked exceptions are not thrown by the cache loader.
If another call to LoadingCache
or LoadingCache
is currently loading the value for key
, simply waits for that thread to finish and returns its loaded value. Note that multiple threads can concurrently load values for distinct keys.
Caches loaded by a CacheLoader
will call CacheLoader
to load new values into the cache. Newly loaded values are added to the cache using Cache.asMap().putIfAbsent
after loading has completed; if another value was associated with key
while the new value was loading then a removal notification will be sent for the new value.
Warning: this method silently converts checked exceptions to unchecked exceptions, and should not be used with cache loaders which throw checked exceptions. In such cases use LoadingCache
instead.
public ImmutableMap<K ,V> getAll(Iterable <? extends K> keys) throws ExecutionException
LoadingCache
keys
, creating or retrieving those values if necessary. The returned map contains entries that were already cached, combined with newly loaded entries; it will never contain null keys or values.
Caches loaded by a CacheLoader
will issue a single request to CacheLoader
for all keys which are not already present in the cache. All entries returned by CacheLoader
will be stored in the cache, over-writing any previously cached values. This method will throw an exception if CacheLoader
returns null
, returns a map containing null keys or values, or fails to return an entry for each requested key.
Note that duplicate elements in keys
, as determined by Object
, will be ignored.
getAll
in interface
LoadingCache<K,V>
ExecutionException
- if a checked exception was thrown while loading the value. (
ExecutionException
is thrown
even if computation was interrupted by an InterruptedException
.)
public V apply(K key)
Function
input
. This method is
generally expected, but not absolutely required, to have the following properties:
Objects.equal
(a, b)
implies that Objects.equal(function.apply(a), function.apply(b))
. public void refresh(K key)
LoadingCache
key
, possibly asynchronously. While the new value is loading the previous value (if any) will continue to be returned by
get(key)
unless it is evicted. If the new value is loaded successfully it will replace the previous value in the cache; if an exception is thrown while refreshing the previous value will remain,
and the exception will be logged (using Logger
) and swallowed.
Caches loaded by a CacheLoader
will call CacheLoader
if the cache currently contains a value for key
, and CacheLoader
otherwise. Loading is asynchronous only if CacheLoader
was overridden with an asynchronous implementation.
Returns without doing anything if another thread is currently loading the value for key
. If the cache loader associated with this cache performs refresh asynchronously then this method may return before refresh completes.