Interface ExpiryPolicy<K,​V>

  • Type Parameters:
    K - the key type for the cache
    V - the value type for the cache

    public interface ExpiryPolicy<K,​V>
    A policy object that governs expiration for mappings in a Cache.

    Previous values are not accessible directly but are rather available through a value Supplier to indicate that access can require computation (such as deserialization).

    Negative durations are not supported, expiry policy implementation returning such a duration will result in immediate expiry, as if the duration was zero.

    NOTE: Some cache configurations (eg. caches with eventual consistency) may use local (ie. non-consistent) state to decide whether to call getExpiryForUpdate(Object, Supplier, Object) vs. getExpiryForCreation(Object, Object). For these cache configurations it is advised to return the same value for both of these methods

    • Field Detail

      • INFINITE

        static final Duration INFINITE
        A duration that represents an infinite time.
      • NO_EXPIRY

        static final ExpiryPolicy<Object,​Object> NO_EXPIRY
        An ExpiryPolicy that represents a no expiration policy
    • Method Detail

      • getExpiryForCreation

        Duration getExpiryForCreation​(K key,
                                      V value)
        Returns the lifetime of an entry when it is initially added to a Cache.

        This method must not return null.

        Exceptions thrown from this method will be swallowed and result in the expiry duration being ZERO.

        Parameters:
        key - the key of the newly added entry
        value - the value of the newly added entry
        Returns:
        a non-null Duration
      • getExpiryForAccess

        Duration getExpiryForAccess​(K key,
                                    Supplier<? extends V> value)
        Returns the expiration duration (relative to the current time) when an existing entry is accessed from a Cache.

        Returning null indicates that the expiration time remains unchanged.

        Exceptions thrown from this method will be swallowed and result in the expiry duration being ZERO.

        Parameters:
        key - the key of the accessed entry
        value - a value supplier for the accessed entry
        Returns:
        an expiration Duration, null means unchanged
      • getExpiryForUpdate

        Duration getExpiryForUpdate​(K key,
                                    Supplier<? extends V> oldValue,
                                    V newValue)
        Returns the expiration duration (relative to the current time) when an existing entry is updated in a Cache.

        Returning null indicates that the expiration time remains unchanged.

        Exceptions thrown from this method will be swallowed and result in the expiry duration being ZERO.

        Parameters:
        key - the key of the updated entry
        oldValue - a value supplier for the previous value of the entry
        newValue - the new value of the entry
        Returns:
        an expiration Duration, null means unchanged