Interface LocalManualCache<C extends LocalCache<K,V>,K,V>
-
- All Superinterfaces:
Cache<K,V>
- All Known Subinterfaces:
LocalLoadingCache<C,K,V>
- All Known Implementing Classes:
BoundedLocalCache.BoundedLocalLoadingCache
,BoundedLocalCache.BoundedLocalManualCache
,UnboundedLocalCache.UnboundedLocalLoadingCache
,UnboundedLocalCache.UnboundedLocalManualCache
interface LocalManualCache<C extends LocalCache<K,V>,K,V> extends Cache<K,V>
This class provides a skeletal implementation of theCache
interface to minimize the effort required to implement aLocalCache
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default java.util.concurrent.ConcurrentMap<K,V>
asMap()
Returns a view of the entries stored in this cache as a thread-safe map.C
cache()
Returns the backingLocalCache
data store.default void
cleanUp()
Performs any pending maintenance operations needed by the cache.default long
estimatedSize()
Returns the approximate number of entries in this cache.default 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.default java.util.Map<K,V>
getAllPresent(java.lang.Iterable<?> keys)
Returns a map of the values associated with thekeys
in this cache.default V
getIfPresent(java.lang.Object key)
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.default void
invalidate(java.lang.Object key)
Discards any cached value for thekey
.default void
invalidateAll()
Discards all entries in the cache.default void
invalidateAll(java.lang.Iterable<?> keys)
Discards any cached values for thekeys
.default void
put(K key, V value)
Associates thevalue
with thekey
in this cache.default void
putAll(java.util.Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to the cache.default CacheStats
stats()
Returns a current snapshot of this cache's cumulative statistics.
-
-
-
Method Detail
-
cache
C cache()
Returns the backingLocalCache
data store.
-
estimatedSize
default long estimatedSize()
Description copied from interface:Cache
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 aCache.cleanUp()
first.- Specified by:
estimatedSize
in interfaceCache<C extends LocalCache<K,V>,K>
- Returns:
- the estimated number of mappings
-
cleanUp
default void cleanUp()
Description copied from interface:Cache
Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.
-
getIfPresent
@Nullable default V getIfPresent(java.lang.Object key)
Description copied from interface:Cache
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.- Specified by:
getIfPresent
in interfaceCache<C extends LocalCache<K,V>,K>
- 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
-
get
default V get(K key, java.util.function.Function<? super K,? extends V> mappingFunction)
Description copied from interface:Cache
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.- Specified by:
get
in interfaceCache<C extends LocalCache<K,V>,K>
- 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
-
getAllPresent
default java.util.Map<K,V> getAllPresent(java.lang.Iterable<?> keys)
Description copied from interface:Cache
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.- Specified by:
getAllPresent
in interfaceCache<C extends LocalCache<K,V>,K>
- 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
-
put
default void put(K key, V value)
Description copied from interface:Cache
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
Cache.get(Object, Function)
when using the conventional "if cached, return; otherwise create, cache and return" pattern.
-
putAll
default void putAll(java.util.Map<? extends K,? extends V> map)
Description copied from interface:Cache
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.
-
invalidate
default void invalidate(java.lang.Object key)
Description copied from interface:Cache
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.- Specified by:
invalidate
in interfaceCache<C extends LocalCache<K,V>,K>
- Parameters:
key
- the key whose mapping is to be removed from the cache
-
invalidateAll
default void invalidateAll(java.lang.Iterable<?> keys)
Description copied from interface:Cache
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.- Specified by:
invalidateAll
in interfaceCache<C extends LocalCache<K,V>,K>
- Parameters:
keys
- the keys whose associated values are to be removed
-
invalidateAll
default void invalidateAll()
Description copied from interface:Cache
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.- Specified by:
invalidateAll
in interfaceCache<C extends LocalCache<K,V>,K>
-
stats
default CacheStats stats()
Description copied from interface:Cache
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.
-
asMap
default java.util.concurrent.ConcurrentMap<K,V> asMap()
Description copied from interface:Cache
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.
-
-