K
- the type of keys maintained by this cacheV
- the type of mapped values@ThreadSafe public interface Cache<K,V>
get(Object, Function)
or put(Object, Object)
, and are stored in the cache until
either evicted or manually invalidated.
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 |
---|---|
ConcurrentMap<K,V> |
asMap()
Returns a view of the entries stored in this cache as a thread-safe map.
|
void |
cleanUp()
Performs any pending maintenance operations needed by the cache.
|
long |
estimatedSize()
Returns the approximate number of entries in this cache.
|
V |
get(K key,
Function<? super K,? extends V> mappingFunction)
Returns the value associated with the
key in this cache, obtaining that value from the
mappingFunction if necessary. |
Map<K,V> |
getAllPresent(Iterable<?> keys)
Returns a map of the values associated with the
keys in this cache. |
V |
getIfPresent(Object key)
Returns the value associated with the
key in this cache, or null if there is no
cached value for the key . |
void |
invalidate(Object key)
Discards any cached value for the
key . |
void |
invalidateAll()
Discards all entries in the cache.
|
void |
invalidateAll(Iterable<?> keys)
Discards any cached values for the
keys . |
Policy<K,V> |
policy()
Returns access to inspect and perform low-level operations on this cache based on its runtime
characteristics.
|
void |
put(K key,
V value)
Associates the
value with the key in this cache. |
void |
putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to the cache.
|
CacheStats |
stats()
Returns a current snapshot of this cache's cumulative statistics.
|
@CheckForNull V getIfPresent(@Nonnull Object key)
key
in this cache, or null
if there is no
cached value for the key
.key
- the key whose associated value is to be returnednull
if this map contains no
mapping for the keyNullPointerException
- if the specified key is null@CheckForNull V get(@Nonnull K key, @Nonnull Function<? super K,? extends V> mappingFunction)
key
in this cache, obtaining that value from the
mappingFunction
if necessary. This method provides a simple substitute for the
conventional "if cached, return; otherwise create, cache and return" pattern.
If the specified key is not already associated with a value, attempts to compute its value
using the given mapping function 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.
Warning: as with CacheLoader.load(K)
, mappingFunction
must not
attempt to update any other mappings of this cache.
key
- the key with which the specified value is to be associatedmappingFunction
- the function to compute a valueNullPointerException
- if the specified key or mappingFunction is nullIllegalStateException
- if the computation detectably attempts a recursive update to this
cache that would otherwise never completeRuntimeException
- or Error if the mappingFunction does so, in which case the mapping is
left unestablished@Nonnull Map<K,V> getAllPresent(@Nonnull Iterable<?> keys)
keys
in this cache. The returned map
will only contain entries which are already present in the cache.
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 elementvoid put(@Nonnull K key, @Nonnull V value)
value
with the key
in this cache. If the cache previously
contained a value associated with the key
, the old value is replaced by the new
value
.
Prefer get(Object, Function)
when using the conventional "if cached, return; otherwise
create, cache and return" pattern.
key
- the key with which the specified value is to be associatedvalue
- value to be associated with the specified keyNullPointerException
- if the specified key or value is nullvoid putAll(@Nonnull Map<? extends K,? extends V> map)
put(k, v)
on this map once for each mapping from key
k
to value v
in the specified map. The behavior of this operation is undefined
if the specified map is modified while the operation is in progress.map
- the mappings to be stored in this cacheNullPointerException
- if the specified map is null or the specified map contains null
keys or valuesvoid invalidate(@Nonnull Object key)
key
. The behavior of this operation is undefined for
an entry that is being loaded and is otherwise not present.key
- the key whose mapping is to be removed from the cacheNullPointerException
- if the specified key is nullvoid invalidateAll(@Nonnull Iterable<?> keys)
keys
. The behavior of this operation is undefined
for an entry that is being loaded and is otherwise not present.keys
- the keys whose associated values are to be removedNullPointerException
- if the specified collection is null or contains a null elementvoid invalidateAll()
@Nonnegative long estimatedSize()
cleanUp()
first.@Nonnull CacheStats stats()
Due to the performance penalty of maintaining statistics, some implementations may not record the usage history immediately or at all.
@Nonnull ConcurrentMap<K,V> asMap()
Iterators from the returned map are at least weakly consistent: they are safe for concurrent use, but if the cache is modified (including by eviction) after the iterator is created, it is undefined which of the changes (if any) will be reflected in that iterator.
Map
operationsvoid cleanUp()
@Nonnull Policy<K,V> policy()