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
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 thread-safe and can be safely accessed by multiple concurrent threads.
- Since:
- 3.7.0
- See Also:
CacheBuilder.build(String)
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Map<K,V>
asMap()
Returns a view of the entries stored in this cache as an unmodifiable map.void
cleanUp()
Performs any pending maintenance operations needed by the cache.V
get(K key, Function<? super K,? extends V> loader)
Returns the value associated with thekey
in this cache, obtaining that value from theloader
if necessary.V
getIfPresent(K key)
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.void
invalidate(K key)
Discards any cached value for thekey
.void
invalidateAll()
Discards all entries in the cache.void
put(K key, V value)
Associates thevalue
with thekey
in this cache.
-
-
-
Method Detail
-
getIfPresent
V getIfPresent(K 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 cache does not contain a mapping for the key - Throws:
NullPointerException
- if the specified key is null
-
get
V get(K key, Function<? super K,? extends V> loader)
Returns the value associated with thekey
in this cache, obtaining that value from theloader
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. 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:
loader
must not attempt to update any other mappings of this cache.Warning:
loader
must 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
void put(K key, 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:
NullPointerException
- if the specified key or value is null
-
invalidate
void invalidate(K key)
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
Map<K,V> asMap()
Returns a view of the entries stored in this cache as an unmodifiable map.- Returns:
- an unmodifiable view of this cache
-
cleanUp
void cleanUp()
Performs any pending maintenance operations needed by the cache. Exactly which activities are performed, if any, is implementation-dependent.
-
-