K
- the type of keys maintained by this cacheV
- the type of mapped valuespublic interface LoadingCache<K,V> extends Cache<K,V>
Implementations of this interface are expected to be thread-safe, and can be safely accessed by multiple concurrent threads.
Modifier and Type | Method and Description |
---|---|
V |
get(K key)
Returns the value associated with the
key in this cache, obtaining that value from
CacheLoader.load(Object) if necessary. |
@NonNull Map<K,V> |
getAll(@NonNull Iterable<? extends K> keys)
Returns a map of the values associated with the
keys , creating or retrieving those
values if necessary. |
void |
refresh(K key)
Loads a new value for the
key , asynchronously. |
asMap, cleanUp, estimatedSize, get, getAll, getAllPresent, getIfPresent, invalidate, invalidateAll, invalidateAll, policy, put, putAll, stats
V get(K key)
key
in this cache, obtaining that value from
CacheLoader.load(Object)
if necessary.
If another call to get(K)
is currently loading the value for the key
, this thread
simply waits for that thread to finish and returns its loaded value. Note that multiple threads
can concurrently load values for distinct keys.
If the specified key is not already associated with a value, attempts to compute its value and
enters it into this cache unless null
. The entire method invocation is performed
atomically, so the function is applied at most once per key. Some attempted update operations
on this cache by other threads may be blocked while the computation is in progress, so the
computation should be short and simple, and must not attempt to update any other mappings of
this cache.
key
- key with which the specified value is to be associatedNullPointerException
- if the specified key is nullIllegalStateException
- if the computation detectably attempts a recursive update to this
cache that would otherwise never completeCompletionException
- if a checked exception was thrown while loading the valueRuntimeException
- or Error if the CacheLoader
does so, in which case the mapping
is left unestablished@NonNull Map<K,V> getAll(@NonNull Iterable<? extends K> keys)
keys
, creating or retrieving those
values if necessary. The returned map contains entries that were already cached, combined with
the 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. If another call to get(K)
tries to load the value for a key in
keys
, implementations may either have that thread load the entry or simply wait for
this thread to finish and returns the loaded value. In the case of overlapping non-blocking
loads, the last load to complete will replace the existing entry. Note that multiple threads
can concurrently load values for distinct keys.
Note that duplicate elements in keys
, as determined by Object.equals(java.lang.Object)
, will be
ignored.
keys
- the keys whose associated values are to be returnedNullPointerException
- if the specified collection is null or contains a null elementCompletionException
- if a checked exception was thrown while loading the valueRuntimeException
- or Error if the CacheLoader
does so, 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. In all cases, the mapping
is left unestablishedvoid refresh(K key)
key
, 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 the key
, and CacheLoader.load(K)
otherwise. Loading
is asynchronous by delegating to the default executor.
key
- key with which a value may be associatedNullPointerException
- if the specified key is null