Top Description Methods
com.github.benmanes.caffeine.cache

pack-priv Interface LocalManualCache<K, V>

extends Cache<K, V>
Known Direct Subinterfaces
com.github.benmanes.caffeine.cache.LocalLoadingCache
Known Direct Implementers
com.github.benmanes.caffeine.cache.UnboundedLocalCache.UnboundedLocalManualCache, com.github.benmanes.caffeine.cache.BoundedLocalCache.BoundedLocalManualCache
Static Imports
com.github.benmanes.caffeine.cache.Caffeine.calculateHashMapCapacity, java.util.Objects.requireNonNull

This class provides a skeletal implementation of the Cache interface to minimize the effort required to implement a LocalCache.
Author
[email protected] (Ben Manes)

Method Summary

Modifier and TypeMethod and Description
public default ConcurrentMap<K, V>
asMap()

Implements com.github.benmanes.caffeine.cache.Cache.asMap.

Returns a view of the entries stored in this cache as a thread-safe map.
public default void
bulkLoad(Set<K> keysToLoad, Map<K, V> result, Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction)

Performs a non-blocking bulk load of the missing keys.

public LocalCache<K, V>
cache()

Returns the backing LocalCache data store.

public default void
cleanUp()

Implements com.github.benmanes.caffeine.cache.Cache.cleanUp.

Performs any pending maintenance operations needed by the cache.
public default long
estimatedSize()

Implements com.github.benmanes.caffeine.cache.Cache.estimatedSize.

Returns the approximate number of entries in this cache.
public default @Nullable V
get(K
the key with which the specified value is to be associated
key
,
Function<? super K, ? extends V>
the function to compute a value
mappingFunction
)

Implements com.github.benmanes.caffeine.cache.Cache.get.

Returns the value associated with the key in this cache, obtaining that value from the mappingFunction if necessary.
public default Map<K, V>
getAll(Iterable<? extends K>
the keys whose associated values are to be returned
keys
,
Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>>
the function to compute the values
mappingFunction
)

Implements com.github.benmanes.caffeine.cache.Cache.getAll.

Returns a map of the values associated with the keys, creating or retrieving those values if necessary.
public default Map<K, V>
getAllPresent(Iterable<? extends K>
the keys whose associated values are to be returned
keys
)

Implements com.github.benmanes.caffeine.cache.Cache.getAllPresent.

Returns a map of the values associated with the keys in this cache.
public default @Nullable V
getIfPresent(K
the key whose associated value is to be returned
key
)

Implements com.github.benmanes.caffeine.cache.Cache.getIfPresent.

Returns the value associated with the key in this cache, or null if there is no cached value for the key.
public default void
invalidate(K
the key whose mapping is to be removed from the cache
key
)

Implements com.github.benmanes.caffeine.cache.Cache.invalidate.

Discards any cached value for the key.
public default void
invalidateAll(Iterable<? extends K>
the keys whose associated values are to be removed
keys
)

Implements com.github.benmanes.caffeine.cache.Cache.invalidateAll.

Discards any cached values for the keys.
public default void
invalidateAll()

Implements com.github.benmanes.caffeine.cache.Cache.invalidateAll.

Discards all entries in the cache.
public default void
put(K
the key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Implements com.github.benmanes.caffeine.cache.Cache.put.

Associates the value with the key in this cache.
public default void
putAll(Map<? extends K, ? extends V>
the mappings to be stored in this cache
map
)

Implements com.github.benmanes.caffeine.cache.Cache.putAll.

Copies all of the mappings from the specified map to the cache.
public default CacheStats
stats()

Implements com.github.benmanes.caffeine.cache.Cache.stats.

Returns a current snapshot of this cache's cumulative statistics.
Inherited from com.github.benmanes.caffeine.cache.Cache:
policy

Method Detail

asMapback to summary
public default ConcurrentMap<K, V> asMap()

Implements com.github.benmanes.caffeine.cache.Cache.asMap.

Doc from com.github.benmanes.caffeine.cache.Cache.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.

A computation operation, such as ConcurrentMap#compute, performs the entire method invocation atomically, so the function is applied at most once per key. Some attempted update operations by other threads may be blocked while computation is in progress. The computation must not attempt to update any other mappings of this 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:ConcurrentMap<K, V>

a thread-safe view of this cache supporting all of the optional Map operations

Annotations
@Override
bulkLoadback to summary
public default void bulkLoad(Set<K> keysToLoad, Map<K, V> result, Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction)

Performs a non-blocking bulk load of the missing keys. Any missing entry that materializes during the load are replaced when the loaded entries are inserted into the cache.

cacheback to summary
public LocalCache<K, V> cache()

Returns the backing LocalCache data store.

cleanUpback to summary
public default void cleanUp()

Implements com.github.benmanes.caffeine.cache.Cache.cleanUp.

Doc from com.github.benmanes.caffeine.cache.Cache.cleanUp.

Performs any pending maintenance operations needed by the cache. Exactly which activities are performed -- if any -- is implementation-dependent.

Annotations
@Override
estimatedSizeback to summary
public default long estimatedSize()

Implements com.github.benmanes.caffeine.cache.Cache.estimatedSize.

Doc from com.github.benmanes.caffeine.cache.Cache.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 a cleanUp() first.

Returns:long

the estimated number of mappings

Annotations
@Override
getback to summary
public default @Nullable V get(K key, Function<? super K, ? extends V> mappingFunction)

Implements com.github.benmanes.caffeine.cache.Cache.get.

Doc from com.github.benmanes.caffeine.cache.Cache.get.

Returns the value associated with the key in this cache, obtaining that value from the mappingFunction 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, mappingFunction must not attempt to update any other mappings of this cache.

Parameters
key:K

the key with which the specified value is to be associated

mappingFunction:Function<? super K, ? extends V>

the function to compute a value

Returns:@Nullable V

the current (existing or computed) value associated with the specified key, or null if the computed value is null

Annotations
@Override
@SuppressWarnings:NullAway
getAllback to summary
public default Map<K, V> getAll(Iterable<? extends K> keys, Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>> mappingFunction)

Implements com.github.benmanes.caffeine.cache.Cache.getAll.

Doc from com.github.benmanes.caffeine.cache.Cache.getAll.

Returns a map of the values associated with the keys, creating or retrieving those values if necessary. The returned map contains entries that were already cached, combined with the newly loaded entries; it will never contain null keys or values.

A single request to the mappingFunction is performed for all keys which are not already present in the cache. All entries returned by mappingFunction will be stored in the cache, over-writing any previously cached values. If another call to get tries to load the value for a key in keys, implementations may either have that thread load the entry or simply wait for this thread to finish and return the loaded value. In the case of overlapping non-blocking loads, the last load to complete will replace the existing entry. Note that multiple threads can concurrently load values for distinct keys. Any loaded values for keys that were not specifically requested will not be returned, but will be stored in the cache.

Note that duplicate elements in keys, as determined by Object#equals, will be ignored.

Parameters
keys:Iterable<? extends K>

the keys whose associated values are to be returned

mappingFunction:Function<? super Set<? extends K>, ? extends Map<? extends K, ? extends V>>

the function to compute the values

Returns:Map<K, V>

an unmodifiable mapping of keys to values for the specified keys in this cache

Annotations
@Override
getAllPresentback to summary
public default Map<K, V> getAllPresent(Iterable<? extends K> keys)

Implements com.github.benmanes.caffeine.cache.Cache.getAllPresent.

Doc from com.github.benmanes.caffeine.cache.Cache.getAllPresent.

Returns a map of the values associated with the keys in this cache. The returned map will only contain entries which are already present in the cache.

Note that duplicate elements in keys, as determined by Object#equals, will be ignored.

Parameters
keys:Iterable<? extends K>

the keys whose associated values are to be returned

Returns:Map<K, V>

an unmodifiable mapping of keys to values for the specified keys found in this cache

Annotations
@Override
getIfPresentback to summary
public default @Nullable V getIfPresent(K key)

Implements com.github.benmanes.caffeine.cache.Cache.getIfPresent.

Doc from com.github.benmanes.caffeine.cache.Cache.getIfPresent.

Returns the value associated with the key in this cache, or null if there is no cached value for the key.

Parameters
key:K

the key whose associated value is to be returned

Returns:@Nullable V

the value to which the specified key is mapped, or null if this cache does not contain a mapping for the key

Annotations
@Override
invalidateback to summary
public default void invalidate(K key)

Implements com.github.benmanes.caffeine.cache.Cache.invalidate.

Doc from com.github.benmanes.caffeine.cache.Cache.invalidate.

Discards any cached value for the key. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present.

Parameters
key:K

the key whose mapping is to be removed from the cache

Annotations
@Override
invalidateAllback to summary
public default void invalidateAll(Iterable<? extends K> keys)

Implements com.github.benmanes.caffeine.cache.Cache.invalidateAll.

Doc from com.github.benmanes.caffeine.cache.Cache.invalidateAll.

Discards any cached values for the keys. The behavior of this operation is undefined for an entry that is being loaded (or reloaded) and is otherwise not present.

Parameters
keys:Iterable<? extends K>

the keys whose associated values are to be removed

Annotations
@Override
invalidateAllback to summary
public default void invalidateAll()

Implements com.github.benmanes.caffeine.cache.Cache.invalidateAll.

Doc from com.github.benmanes.caffeine.cache.Cache.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.

Annotations
@Override
putback to summary
public default void put(K key, V value)

Implements com.github.benmanes.caffeine.cache.Cache.put.

Doc from com.github.benmanes.caffeine.cache.Cache.put.

Associates the 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.

Prefer get(Object, Function) when using the conventional "if cached, return; otherwise create, cache and return" pattern.

Parameters
key:K

the key with which the specified value is to be associated

value:V

value to be associated with the specified key

Annotations
@Override
putAllback to summary
public default void putAll(Map<? extends K, ? extends V> map)

Implements com.github.benmanes.caffeine.cache.Cache.putAll.

Doc from com.github.benmanes.caffeine.cache.Cache.putAll.

Copies all of the mappings from the specified map to the cache. The effect of this call is equivalent to that of calling put(k, v) 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.

Parameters
map:Map<? extends K, ? extends V>

the mappings to be stored in this cache

Annotations
@Override
statsback to summary
public default CacheStats stats()

Implements com.github.benmanes.caffeine.cache.Cache.stats.

Doc from com.github.benmanes.caffeine.cache.Cache.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:CacheStats

the current snapshot of the statistics of this cache

Annotations
@Override