Module com.github.benmanes.caffeine
Interface Expiry<K,V>
- Type Parameters:
K- the type of keysV- the type of values
Calculates when cache entries expire. A single expiration time is retained so that the lifetime
of an entry may be extended or reduced by subsequent evaluations.
Usage example:
LoadingCache<Key, Graph> cache = Caffeine.newBuilder()
.expireAfter(Expiry.creating((Key key, Graph graph) ->
Duration.between(Instant.now(), graph.createdOn().plusHours(5))))
.build(key -> createExpensiveGraph(key));
-
Method Summary
Modifier and TypeMethodDescriptionstatic <K,V> Expiry <K, V> accessing(BiFunction<K, V, Duration> function) Returns anExpirythat specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation, replacement of its value, or after it was last read.static <K,V> Expiry <K, V> creating(BiFunction<K, V, Duration> function) Returns anExpirythat specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation.longexpireAfterCreate(K key, V value, long currentTime) Specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation.longexpireAfterRead(K key, V value, long currentTime, long currentDuration) Specifies that the entry should be automatically removed from the cache once the duration has elapsed after its last read.longexpireAfterUpdate(K key, V value, long currentTime, long currentDuration) Specifies that the entry should be automatically removed from the cache once the duration has elapsed after the replacement of its value.static <K,V> Expiry <K, V> writing(BiFunction<K, V, Duration> function) Returns anExpirythat specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation or replacement of its value.
-
Method Details
-
expireAfterCreate
Specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation. To indicate no expiration, an entry may be given an excessively long period, such asLong.MAX_VALUE.Note: The
currentTimeis supplied by the configuredTickerand by default does not relate to system or wall-clock time. When calculating the duration based on a timestamp, the current time should be obtained independently.- Parameters:
key- the key associated with this entryvalue- the value associated with this entrycurrentTime- the ticker's current time, in nanoseconds- Returns:
- the length of time before the entry expires, in nanoseconds
-
expireAfterUpdate
Specifies that the entry should be automatically removed from the cache once the duration has elapsed after the replacement of its value. To indicate no expiration, an entry may be given an excessively long period, such asLong.MAX_VALUE. ThecurrentDurationmay be returned to not modify the expiration time.Note: The
currentTimeis supplied by the configuredTickerand by default does not relate to system or wall-clock time. When calculating the duration based on a timestamp, the current time should be obtained independently.- Parameters:
key- the key associated with this entryvalue- the new value associated with this entrycurrentTime- the ticker's current time, in nanosecondscurrentDuration- the entry's current duration, in nanoseconds- Returns:
- the length of time before the entry expires, in nanoseconds
-
expireAfterRead
Specifies that the entry should be automatically removed from the cache once the duration has elapsed after its last read. To indicate no expiration, an entry may be given an excessively long period, such asLong.MAX_VALUE. ThecurrentDurationmay be returned to not modify the expiration time.Note: The
currentTimeis supplied by the configuredTickerand by default does not relate to system or wall-clock time. When calculating the duration based on a timestamp, the current time should be obtained independently.- Parameters:
key- the key associated with this entryvalue- the value associated with this entrycurrentTime- the ticker's current time, in nanosecondscurrentDuration- the entry's current duration, in nanoseconds- Returns:
- the length of time before the entry expires, in nanoseconds
-
creating
Returns anExpirythat specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation. The expiration time is not modified when the entry is updated or read.Expiry<Key, Graph> expiry = Expiry.creating((key, graph) -> Duration.between(Instant.now(), graph.createdOn().plusHours(5)));- Type Parameters:
K- the key typeV- the value type- Parameters:
function- the function used to calculate the length of time after an entry is created before it should be automatically removed- Returns:
- an
Expiryinstance with the specified expiry function
-
writing
Returns anExpirythat specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation or replacement of its value. The expiration time is not modified when the entry is read.Expiry<Key, Graph> expiry = Expiry.writing((key, graph) -> Duration.between(Instant.now(), graph.modifiedOn().plusHours(5)));- Type Parameters:
K- the key typeV- the value type- Parameters:
function- the function used to calculate the length of time after an entry is created or updated that it should be automatically removed- Returns:
- an
Expiryinstance with the specified expiry function
-
accessing
Returns anExpirythat specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation, replacement of its value, or after it was last read.Expiry<Key, Graph> expiry = Expiry.accessing((key, graph) -> graph.isDirected() ? Duration.ofHours(1) : Duration.ofHours(3));- Type Parameters:
K- the key typeV- the value type- Parameters:
function- the function used to calculate the length of time after an entry last accessed that it should be automatically removed- Returns:
- an
Expiryinstance with the specified expiry function
-