K - the type of keys maintained by this cacheV - the type of mapped values@ThreadSafe @ParametersAreNonnullByDefault 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.
|
boolean |
contains(K key)
Returns
true if this map contains a mapping for the specified key. |
V |
get(K key)
Returns the value associated with the
key in this cache, or null if there is no cached value for
the key. |
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> |
getAll(Iterable<? extends K> keys)
Returns a map of the values associated with the
keys in this cache. |
void |
invalidate(K key)
Discards any cached value for the
key. |
void |
invalidateAll()
Discards all entries in the cache.
|
void |
invalidateAll(Iterable<? extends K> keys)
Discards any cached values for the
keys. |
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.
|
void |
putIfAbsent(K key,
V value)
Associates the
value with the key in this cache, only if the key does not already exist. |
void |
refresh(K key)
Loads a new value for the
key, asynchronously. |
long |
size()
Returns the approximate number of entries in this cache.
|
CacheStats |
stats()
Returns a current snapshot of this cache's cumulative statistics.
|
V get(K 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
keyV get(K key, Function<? super K,? extends V> mappingFunction)
key in this cache, obtaining that value from the mappingFunction if necessary.
If the specified key is not already associated with a value, attempts to compute its value using the
given mappingFunction 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.
key - the key with which the specified value is to be associatedmappingFunction - the function to compute a valuenull if the
computed value is nullIllegalStateException - if the computation detectably attempts a recursive update to this cache that would
otherwise never completeRuntimeException - if the mappingFunction does so, in which case the mapping is left unestablished@Nonnull Map<K,V> getAll(Iterable<? extends K> keys)
keys in this cache. The returned map will only contain
entries which are already present in the cache.keys - the keys whose associated values are to be returnedvoid put(K key, 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.key - the key with which the specified value is to be associatedvalue - value to be associated with the specified keyvoid putIfAbsent(K key, V value)
value with the key in this cache, only if the key does not already exist.
If the cache previously contained a value associated with the key, then the call does nothing.key - the key with which the specified value is to be associatedvalue - value to be associated with the specified keyvoid putAll(Map<? extends K,? extends V> map)
put(Object, Object) 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 cachevoid invalidate(K key)
key.key - the key whose mapping is to be removed from the cachevoid invalidateAll(Iterable<? extends K> keys)
keys.keys - the keys whose associated values are to be removedvoid invalidateAll()
boolean contains(K key)
true if this map contains a mapping for the specified key.key - key whose presence in this map is to be testedtrue if this map contains a mapping for the specified key@Nonnegative long size()
void 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.key - key with which a value may be associatedvoid cleanUp()
@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 operations@Nonnull CacheStats stats()
Due to the performance penalty of maintaining statistics, some implementations may not record the usage history immediately or at all.
Warning: this cache may not be recording statistical data. For example, a cache created using CacheBuilder
only does so if the CacheBuilder.recordStats() method was called. If statistics are not being recorded, a
CacheStats instance with zero for all values is returned.
Copyright © 2017–2019 Atlanmod. All rights reserved.