Module com.github.benmanes.caffeine
Interface Policy<K,V>
-
public interface Policy<K,V>
An access point for inspecting and performing low-level operations based on the cache's runtime characteristics. These operations are optional and dependent on how the cache was constructed and what abilities the implementation exposes.
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interface
Policy.CacheEntry<K,V>
A key-value pair that may include policy metadata for the cached entry.static interface
Policy.Eviction<K,V>
The low-level operations for a cache with a size-based eviction policy.static interface
Policy.FixedExpiration<K,V>
The low-level operations for a cache with a fixed expiration policy.static interface
Policy.FixedRefresh<K,V>
The low-level operations for a cache with a fixed refresh policy.static interface
Policy.VarExpiration<K,V>
The low-level operations for a cache with a variable expiration policy.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Optional<Policy.Eviction<K,V>>
eviction()
Returns access to perform operations based on the maximum size or maximum weight eviction policy.Optional<Policy.FixedExpiration<K,V>>
expireAfterAccess()
Returns access to perform operations based on the time-to-idle expiration policy.Optional<Policy.FixedExpiration<K,V>>
expireAfterWrite()
Returns access to perform operations based on the time-to-live expiration policy.Optional<Policy.VarExpiration<K,V>>
expireVariably()
Returns access to perform operations based on the variable expiration policy.default @Nullable Policy.CacheEntry<K,V>
getEntryIfPresentQuietly(K key)
Returns the cache entry associated with thekey
in this cache, ornull
if there is no cached value for thekey
.@Nullable V
getIfPresentQuietly(K key)
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
.boolean
isRecordingStats()
Returns whether the cache statistics are being accumulated.Optional<Policy.FixedRefresh<K,V>>
refreshAfterWrite()
Returns access to perform operations based on the time-to-live refresh policy.Map<K,CompletableFuture<V>>
refreshes()
Returns an unmodifiable snapshotMap
view of the in-flight refresh operations.
-
-
-
Method Detail
-
isRecordingStats
boolean isRecordingStats()
Returns whether the cache statistics are being accumulated.- Returns:
- if cache statistics are being recorded
-
getIfPresentQuietly
@Nullable V getIfPresentQuietly(K key)
Returns the value associated with thekey
in this cache, ornull
if there is no cached value for thekey
. UnlikeCache.getIfPresent(Object)
, this method does not produce any side effects such as updating statistics, the eviction policy, resetting the expiration time, or triggering a refresh.- 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 contains no mapping for the key - Throws:
NullPointerException
- if the specified key is null
-
getEntryIfPresentQuietly
default @Nullable Policy.CacheEntry<K,V> getEntryIfPresentQuietly(K key)
Returns the cache entry associated with thekey
in this cache, ornull
if there is no cached value for thekey
. UnlikeCache.getIfPresent(Object)
, this method does not produce any side effects such as updating statistics, the eviction policy, resetting the expiration time, or triggering a refresh.- Parameters:
key
- the key whose associated value is to be returned- Returns:
- the entry mapping for the specified key, or
null
if this cache contains no mapping for the key - Throws:
NullPointerException
- if the specified key is null
-
refreshes
Map<K,CompletableFuture<V>> refreshes()
Returns an unmodifiable snapshotMap
view of the in-flight refresh operations.- Returns:
- a snapshot view of the in-flight refresh operations
-
eviction
Optional<Policy.Eviction<K,V>> eviction()
Returns access to perform operations based on the maximum size or maximum weight eviction policy. If the cache was not constructed with a size-based bound or the implementation does not support these operations, an emptyOptional
is returned.- Returns:
- access to low-level operations for this cache if an eviction policy is used
-
expireAfterAccess
Optional<Policy.FixedExpiration<K,V>> expireAfterAccess()
Returns access to perform operations based on the time-to-idle expiration policy. This policy determines that an entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, the most recent replacement of its value, or its last access. Access time is reset by all cache read and write operations (includingCache.asMap().get(Object)
andCache.asMap().put(K, V)
), but not by operations on the collection-views ofCache.asMap()
.If the cache was not constructed with access-based expiration or the implementation does not support these operations, an empty
Optional
is returned.- Returns:
- access to low-level operations for this cache if a time-to-idle expiration policy is used
-
expireAfterWrite
Optional<Policy.FixedExpiration<K,V>> expireAfterWrite()
Returns access to perform operations based on the time-to-live expiration policy. This policy determines that an entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.If the cache was not constructed with write-based expiration or the implementation does not support these operations, an empty
Optional
is returned.- Returns:
- access to low-level operations for this cache if a time-to-live expiration policy is used
-
expireVariably
Optional<Policy.VarExpiration<K,V>> expireVariably()
Returns access to perform operations based on the variable expiration policy. This policy determines that an entry should be automatically removed from the cache once a per-entry duration has elapsed.If the cache was not constructed with variable expiration or the implementation does not support these operations, an empty
Optional
is returned.- Returns:
- access to low-level operations for this cache if a variable expiration policy is used
-
refreshAfterWrite
Optional<Policy.FixedRefresh<K,V>> refreshAfterWrite()
Returns access to perform operations based on the time-to-live refresh policy. This policy determines that an entry should be automatically reloaded once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.If the cache was not constructed with write-based refresh or the implementation does not support these operations, an empty
Optional
is returned.- Returns:
- access to low-level operations for this cache if a time-to-live refresh policy is used
-
-