Interface Cache<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
- All Known Subinterfaces:
LoadingCache<K,V>
,LocalLoadingCache<C,K,V>
,LocalManualCache<C,K,V>
- All Known Implementing Classes:
BoundedLocalCache.BoundedLocalLoadingCache
,BoundedLocalCache.BoundedLocalManualCache
,LocalAsyncLoadingCache.LoadingCacheView
,UnboundedLocalCache.UnboundedLocalLoadingCache
,UnboundedLocalCache.UnboundedLocalManualCache
@ThreadSafe public interface Cache<K,V>
A semi-persistent mapping from keys to values. Cache entries are manually added usingget(Object, Function)
orput(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.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.util.concurrent.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, java.util.function.Function<? super K,? extends V> mappingFunction)
Returns the value associated with thekey
in this cache, obtaining that value from themappingFunction
if necessary.java.util.Map<K,V>
getAllPresent(java.lang.Iterable<?> keys)
Returns a map of the values associated with thekeys
in this cache.V
getIfPresent(java.lang.Object key)
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.void
invalidate(java.lang.Object key)
Discards any cached value for thekey
.void
invalidateAll()
Discards all entries in the cache.void
invalidateAll(java.lang.Iterable<?> keys)
Discards any cached values for thekeys
.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 thevalue
with thekey
in this cache.void
putAll(java.util.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.
-
-
-
Method Detail
-
getIfPresent
@CheckForNull V getIfPresent(@Nonnull java.lang.Object key)
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the value to which the specified key is mapped, or
null
if this map contains no mapping for the key - Throws:
java.lang.NullPointerException
- if the specified key is null
-
get
@CheckForNull V get(@Nonnull K key, @Nonnull java.util.function.Function<? super K,? extends V> mappingFunction)
Returns the value associated with thekey
in this cache, obtaining that value from themappingFunction
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.- Parameters:
key
- the key with which the specified value is to be associatedmappingFunction
- the function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key, or null if the computed value is null
- Throws:
java.lang.NullPointerException
- if the specified key or mappingFunction is nulljava.lang.IllegalStateException
- if the computation detectably attempts a recursive update to this cache that would otherwise never completejava.lang.RuntimeException
- or Error if the mappingFunction does so, in which case the mapping is left unestablished
-
getAllPresent
@Nonnull java.util.Map<K,V> getAllPresent(@Nonnull java.lang.Iterable<?> keys)
Returns a map of the values associated with thekeys
in this cache. The returned map will only contain entries which are already present in the cache.- Parameters:
keys
- the keys whose associated values are to be returned- Returns:
- the unmodifiable mapping of keys to values for the specified keys found in this cache
- Throws:
java.lang.NullPointerException
- if the specified collection is null or contains a null element
-
put
void put(@Nonnull K key, @Nonnull V value)
Associates thevalue
with thekey
in this cache. If the cache previously contained a value associated with thekey
, the old value is replaced by the newvalue
.Prefer
get(Object, Function)
when using the conventional "if cached, return; otherwise create, cache and return" pattern.- Parameters:
key
- the key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Throws:
java.lang.NullPointerException
- if the specified key or value is null
-
putAll
void putAll(@Nonnull java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to the cache. The effect of this call is equivalent to that of callingput(k, v)
on this map once for each mapping from keyk
to valuev
in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.- Parameters:
map
- the mappings to be stored in this cache- Throws:
java.lang.NullPointerException
- if the specified map is null or the specified map contains null keys or values
-
invalidate
void invalidate(@Nonnull java.lang.Object key)
Discards any cached value for thekey
. The behavior of this operation is undefined for an entry that is being loaded and is otherwise not present.- Parameters:
key
- the key whose mapping is to be removed from the cache- Throws:
java.lang.NullPointerException
- if the specified key is null
-
invalidateAll
void invalidateAll(@Nonnull java.lang.Iterable<?> keys)
Discards any cached values for thekeys
. The behavior of this operation is undefined for an entry that is being loaded and is otherwise not present.- Parameters:
keys
- the keys whose associated values are to be removed- Throws:
java.lang.NullPointerException
- if the specified collection is null or contains a null element
-
invalidateAll
void invalidateAll()
Discards all entries in the cache. The behavior of this operation is undefined for an entry that is being loaded and is otherwise not present.
-
estimatedSize
@Nonnegative long estimatedSize()
Returns the approximate number of entries in this cache. The value returned is an estimate; the actual count may differ if there are concurrent insertions or removals, or if some entries are pending removal due to expiration or weak/soft reference collection. In the case of stale entries this inaccuracy can be mitigated by performing acleanUp()
first.- Returns:
- the estimated number of mappings
-
stats
@Nonnull CacheStats stats()
Returns a current snapshot of this cache's cumulative statistics. All statistics are initialized to zero, and are monotonically increasing over the lifetime of the cache.Due to the performance penalty of maintaining statistics, some implementations may not record the usage history immediately or at all.
- Returns:
- the current snapshot of the statistics of this cache
-
asMap
@Nonnull java.util.concurrent.ConcurrentMap<K,V> asMap()
Returns a view of the entries stored in this cache as a thread-safe map. Modifications made to the map directly affect the cache.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.
- Returns:
- a thread-safe view of this cache supporting all of the optional
Map
operations
-
cleanUp
void cleanUp()
Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.
-
policy
@Nonnull Policy<K,V> policy()
Returns access to inspect and perform low-level operations on this cache based on its runtime characteristics. These operations are optional and dependent on how the cache was constructed and what abilities the implementation exposes.- Returns:
- access to inspect and perform advanced operations based on the cache's characteristics
-
-