Interface Expiry<K,V>

Type Parameters:
K - the type of keys
V - the type of values

@NullMarked public interface Expiry<K,V>
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 Type
    Method
    Description
    static <K, V> Expiry<K,V>
    accessing(BiFunction<K,V,Duration> function)
    Returns an Expiry that 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 an Expiry that specifies that the entry should be automatically removed from the cache once the duration has elapsed after the entry's creation.
    long
    expireAfterCreate(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.
    long
    expireAfterRead(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.
    long
    expireAfterUpdate(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 an Expiry that 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

      long expireAfterCreate(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. To indicate no expiration, an entry may be given an excessively long period, such as Long.MAX_VALUE.

      Note: The currentTime is supplied by the configured Ticker and 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 entry
      value - the value associated with this entry
      currentTime - the ticker's current time, in nanoseconds
      Returns:
      the length of time before the entry expires, in nanoseconds
    • expireAfterUpdate

      long expireAfterUpdate(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. To indicate no expiration, an entry may be given an excessively long period, such as Long.MAX_VALUE. The currentDuration may be returned to not modify the expiration time.

      Note: The currentTime is supplied by the configured Ticker and 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 entry
      value - the new value associated with this entry
      currentTime - the ticker's current time, in nanoseconds
      currentDuration - the entry's current duration, in nanoseconds
      Returns:
      the length of time before the entry expires, in nanoseconds
    • expireAfterRead

      long expireAfterRead(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. To indicate no expiration, an entry may be given an excessively long period, such as Long.MAX_VALUE. The currentDuration may be returned to not modify the expiration time.

      Note: The currentTime is supplied by the configured Ticker and 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 entry
      value - the value associated with this entry
      currentTime - the ticker's current time, in nanoseconds
      currentDuration - the entry's current duration, in nanoseconds
      Returns:
      the length of time before the entry expires, in nanoseconds
    • creating

      static <K, V> Expiry<K,V> creating(BiFunction<K,V,Duration> function)
      Returns an Expiry that 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 type
      V - 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 Expiry instance with the specified expiry function
    • writing

      static <K, V> Expiry<K,V> writing(BiFunction<K,V,Duration> function)
      Returns an Expiry that 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 type
      V - 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 Expiry instance with the specified expiry function
    • accessing

      static <K, V> Expiry<K,V> accessing(BiFunction<K,V,Duration> function)
      Returns an Expiry that 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 type
      V - 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 Expiry instance with the specified expiry function