Interface Cache<K,V>
- Type Parameters:
K- the type of keys maintained by this cacheV- the type of mapped values
- All Known Implementing Classes:
AbstractCache
get(Object, Function) or put(Object, Object),
and are stored in the cache until either evicted or manually invalidated.
Implementations of this interface are thread-safe and can be safely accessed by multiple concurrent threads.
- Since:
- 3.7.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionasMap()Returns a view of the entries stored in this cache as an unmodifiable map.voidcleanUp()Performs any pending maintenance operations needed by the cache.Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).Returns the value associated with thekeyin this cache, obtaining that value from theloaderif necessary.getIfPresent(K key) Returns the value associated with thekeyin this cache, ornullif there is no cached value for thekey.voidinvalidate(K key) Discards any cached value for thekey.voidDiscards all entries in the cache.voidAssociates thevaluewith thekeyin this cache.
-
Method Details
-
getIfPresent
Returns the value associated with thekeyin this cache, ornullif 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
nullif this cache does not contain a mapping for the key - Throws:
NullPointerException- if the specified key is null
-
get
Returns the value associated with thekeyin this cache, obtaining that value from theloaderif 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. 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:
loadermust not attempt to update any other mappings of this cache.Warning:
loadermust not returnnull; it may either return a non-null value or throw an exception.- Parameters:
key- the key whose associated value is to be returnedloader- the function to compute a value- Returns:
- the current (existing or computed) value associated with the specified key
- Throws:
NullPointerException- if the specified key or loader is nullIllegalStateException- if the computation detectably attempts a recursive update to this cache that would otherwise never completeRuntimeException- or Error if the loader does so, in which case the mapping is left unestablished
-
put
Associates thevaluewith thekeyin 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:
NullPointerException- if the specified key or value is null
-
invalidate
Discards any cached value for thekey. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present.- Parameters:
key- the key whose mapping is to be removed from the cache- Throws:
NullPointerException- if the specified key is null
-
invalidateAll
void invalidateAll()Discards all entries in the cache. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present. -
asMap
Returns a view of the entries stored in this cache as an unmodifiable map.- Returns:
- an unmodifiable view of this cache
-
compute
Attempts to compute a mapping for the specified key and its current mapped value (ornullif there is no current mapping).If the remapping function returns
null, the mapping is removed (or remains absent if initially absent). If the remapping function returns a non-null value, it is stored in the cache.The entire operation is performed atomically with respect to other cache operations on the same key, making it safe for concurrent use and cluster environments.
This method is aligned with
Map.compute(Object, BiFunction).Warning:
remappingFunctionmust not attempt to update any other mappings of this cache.- Parameters:
key- the key whose associated value is to be computedremappingFunction- the function to compute a value. The first argument is the key, the second argument is the current value ornullif absent. The result is the new value, ornullto remove the mapping.- Returns:
- the new value associated with the specified key, or
nullif none - Throws:
NullPointerException- if the specified key or remappingFunction is null- Since:
- 3.16.0
-
cleanUp
void cleanUp()Performs any pending maintenance operations needed by the cache. Exactly which activities are performed, if any, is implementation-dependent.
-