@Beta public abstract class AbstractLoadingCache<K,V> extends AbstractCache<K,V> implements LoadingCache<K,V>
Cache
interface to minimize the
effort required to implement this interface.
To implement a cache, the programmer needs only to extend this class and provide an
implementation for the AbstractCache.get(K, java.util.concurrent.Callable<? extends V>)
and Cache.getIfPresent(K)
methods. getUnchecked(K)
,
#get(K, Callable)
, and getAll(java.lang.Iterable<? extends K>)
are implemented in terms of get
;
AbstractCache.getAllPresent(java.lang.Iterable<? extends K>)
is implemented in terms of get
; AbstractCache.invalidateAll(Iterable)
is implemented in terms of AbstractCache.invalidate(java.lang.Object)
. The method AbstractCache.cleanUp()
is a no-op. All other
methods throw an UnsupportedOperationException
.
AbstractCache.SimpleStatsCounter, AbstractCache.StatsCounter
Modifier | Constructor and Description |
---|---|
protected |
AbstractLoadingCache()
Constructor for use by subclasses.
|
Modifier and Type | Method and Description |
---|---|
V |
apply(K key)
Discouraged.
|
ImmutableMap<K,V> |
getAll(java.lang.Iterable<? extends K> keys)
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, invalidate, invalidateAll, invalidateAll, put, size, stats
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
asMap, get
cleanUp, get, getAllPresent, getIfPresent, invalidate, invalidateAll, invalidateAll, put, size, stats
protected AbstractLoadingCache()
public V getUnchecked(K key)
Cache
key
in this cache, first loading that value if
necessary. No observable state associated with this cache is modified until computation
completes. Unlike Cache.get(K, java.util.concurrent.Callable<? extends V>)
, 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.
Warning: this method silently converts checked exceptions to unchecked exceptions, and should not be used with cache loaders which throw checked exceptions.
getUnchecked
in interface Cache<K,V>
getUnchecked
in interface LoadingCache<K,V>
getUnchecked
in class AbstractCache<K,V>
public ImmutableMap<K,V> getAll(java.lang.Iterable<? extends K> keys) throws java.util.concurrent.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.loadAll(java.lang.Iterable<? extends K>)
for all keys which are not already present in the cache. All
entries returned by CacheLoader.loadAll(java.lang.Iterable<? extends K>)
will be stored in the cache, over-writing
any previously cached values. This method will throw an exception if
CacheLoader.loadAll(java.lang.Iterable<? extends K>)
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.equals(java.lang.Object)
, will
be ignored.
getAll
in interface LoadingCache<K,V>
java.util.concurrent.ExecutionException
- if a checked exception was thrown while loading the valuespublic final V apply(K key)
Cache
Function
interface; use Cache.get(K, java.util.concurrent.Callable<? extends V>)
or
Cache.getUnchecked(K)
instead.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.reload(K, V)
if the
cache currently contains a value for key
, and CacheLoader.load(K)
otherwise.
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.
refresh
in interface LoadingCache<K,V>