Module com.github.benmanes.caffeine
Interface Policy.VarExpiration<K,V>
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default Optional<Duration>getExpiresAfter(K key)Returns the duration until the entry should be automatically removed.OptionalLonggetExpiresAfter(K key, TimeUnit unit)Returns the duration until the entry should be automatically removed.Map<K,V>oldest(@org.checkerframework.checker.index.qual.NonNegative int limit)Returns an unmodifiable snapshotMapview of the cache with ordered traversal.@Nullable Vput(K key, V value, @org.checkerframework.checker.index.qual.NonNegative long duration, TimeUnit unit)Associates thevaluewith thekeyin this cache.default @Nullable Vput(K key, V value, Duration duration)Associates thevaluewith thekeyin this cache.@Nullable VputIfAbsent(K key, V value, @org.checkerframework.checker.index.qual.NonNegative long duration, TimeUnit unit)Associates thevaluewith thekeyin this cache if the specified key is not already associated with a value.default @Nullable VputIfAbsent(K key, V value, Duration duration)Associates thevaluewith thekeyin this cache if the specified key is not already associated with a value.voidsetExpiresAfter(K key, @org.checkerframework.checker.index.qual.NonNegative long duration, TimeUnit unit)Specifies that the entry should be automatically removed from the cache once the duration has elapsed.default voidsetExpiresAfter(K key, Duration duration)Specifies that the entry should be automatically removed from the cache once the duration has elapsed.Map<K,V>youngest(@org.checkerframework.checker.index.qual.NonNegative int limit)Returns an unmodifiable snapshotMapview of the cache with ordered traversal.
-
-
-
Method Detail
-
getExpiresAfter
OptionalLong getExpiresAfter(K key, TimeUnit unit)
Returns the duration until the entry should be automatically removed. The expiration policy determines when the entry's duration is reset.- Parameters:
key- the key for the entry being queriedunit- the unit thatageis expressed in- Returns:
- the duration if the entry is present in the cache
-
getExpiresAfter
default Optional<Duration> getExpiresAfter(K key)
Returns the duration until the entry should be automatically removed. The expiration policy determines when the entry's duration is reset.- Parameters:
key- the key for the entry being queried- Returns:
- the duration if the entry is present in the cache
-
setExpiresAfter
void setExpiresAfter(K key, @org.checkerframework.checker.index.qual.NonNegative long duration, TimeUnit unit)
Specifies that the entry should be automatically removed from the cache once the duration has elapsed. The expiration policy determines when the entry's age is reset.- Parameters:
key- the key for the entry being setduration- the length of time from now when the entry should be automatically removedunit- the unit thatdurationis expressed in- Throws:
IllegalArgumentException- ifdurationis negativeNullPointerException- if the unit is null
-
setExpiresAfter
default void setExpiresAfter(K key, Duration duration)
Specifies that the entry should be automatically removed from the cache once the duration has elapsed. The expiration policy determines when the entry's age is reset.- Parameters:
key- the key for the entry being setduration- the length of time from now when the entry should be automatically removed- Throws:
IllegalArgumentException- ifdurationis negative
-
putIfAbsent
@Nullable V putIfAbsent(K key, V value, @org.checkerframework.checker.index.qual.NonNegative long duration, TimeUnit unit)
Associates thevaluewith thekeyin this cache if the specified key is not already associated with a value. This method differs fromMap.putIfAbsent(K, V)by substituting the configuredExpirywith the specified write duration, has no effect on the duration if the entry was present, and returns the success rather than a value.- Parameters:
key- the key with which the specified value is to be associatedvalue- value to be associated with the specified keyduration- the length of time from now when the entry should be automatically removedunit- the unit thatdurationis expressed in- Returns:
- the previous value associated with the specified key, or
nullif there was no mapping for the key. - Throws:
IllegalArgumentException- ifdurationis negative
-
putIfAbsent
default @Nullable V putIfAbsent(K key, V value, Duration duration)
Associates thevaluewith thekeyin this cache if the specified key is not already associated with a value. This method differs fromMap.putIfAbsent(K, V)by substituting the configuredExpirywith the specified write duration, has no effect on the duration if the entry was present, and returns the success rather than a value.- Parameters:
key- the key with which the specified value is to be associatedvalue- value to be associated with the specified keyduration- the length of time from now when the entry should be automatically removed- Returns:
- the previous value associated with the specified key, or
nullif there was no mapping for the key. - Throws:
IllegalArgumentException- ifdurationis negative
-
put
@Nullable V put(K key, V value, @org.checkerframework.checker.index.qual.NonNegative long duration, TimeUnit unit)
Associates thevaluewith thekeyin this cache. If the cache previously contained a value associated with thekey, the old value is replaced by the newvalue. This method differs fromCache.put(K, V)by substituting the configuredExpirywith the specified write duration.- Parameters:
key- the key with which the specified value is to be associatedvalue- value to be associated with the specified keyduration- the length of time from now when the entry should be automatically removedunit- the unit thatdurationis expressed in- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. - Throws:
IllegalArgumentException- ifdurationis negativeNullPointerException- if the specified key or value is null
-
put
default @Nullable V put(K key, V value, Duration duration)
Associates thevaluewith thekeyin this cache. If the cache previously contained a value associated with thekey, the old value is replaced by the newvalue. This method differs fromCache.put(K, V)by substituting the configuredExpirywith the specified write duration.- Parameters:
key- the key with which the specified value is to be associatedvalue- value to be associated with the specified keyduration- the length of time from now when the entry should be automatically removed- Returns:
- the previous value associated with
key, ornullif there was no mapping forkey. - Throws:
IllegalArgumentException- ifdurationis negativeNullPointerException- if the specified key or value is null
-
oldest
Map<K,V> oldest(@org.checkerframework.checker.index.qual.NonNegative int limit)
Returns an unmodifiable snapshotMapview of the cache with ordered traversal. The order of iteration is from the entries most likely to expire (oldest) to the entries least likely to expire (youngest). This order is determined by the expiration policy's best guess at the time of creating this snapshot view.Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries.
- Parameters:
limit- the maximum size of the returned map (useInteger.MAX_VALUEto disregard the limit)- Returns:
- a snapshot view of the cache from oldest entry to the youngest
-
youngest
Map<K,V> youngest(@org.checkerframework.checker.index.qual.NonNegative int limit)
Returns an unmodifiable snapshotMapview of the cache with ordered traversal. The order of iteration is from the entries least likely to expire (youngest) to the entries most likely to expire (oldest). This order is determined by the expiration policy's best guess at the time of creating this snapshot view.Beware that obtaining the mappings is NOT a constant-time operation. Because of the asynchronous nature of the page replacement policy, determining the retention ordering requires a traversal of the entries.
- Parameters:
limit- the maximum size of the returned map (useInteger.MAX_VALUEto disregard the limit)- Returns:
- a snapshot view of the cache from youngest entry to the oldest
-
-