Interface ExpirationAfterWriteStrategy


  • public interface ExpirationAfterWriteStrategy
    Represents the strategy for choosing time to live for buckets in the cache.
    • Method Detail

      • calculateTimeToLiveMillis

        long calculateTimeToLiveMillis​(RemoteBucketState state,
                                       long currentTimeNanos)
        Calculates the time to live for bucket that is going to be persisted to the remote storage
        Parameters:
        state - the state of bucket that is going to be persisted to the remote storage
        currentTimeNanos - the time of operation
        Returns:
        time to live for bucket in milliseconds, negative value must be considered that bucket should be stored forever without expiration
      • fixedTimeToLive

        static ExpirationAfterWriteStrategy fixedTimeToLive​(Duration ttl)
        Returns strategy that makes to store buckets with fixed TTL.

        This strategy can lead to overconsumption if provided ttl is less significantly than refill period, because information about previously consumed in such misconfiguration case can be lost in remote storage faster than it should be. As well as consumed capacity to store all buckets in storage can be significantly more than it can be in case of TTL is significantly greater than refill period of bucket. So consider to use basedOnTimeForRefillingBucketUpToMax(Duration) for more precise TTL calculation based on current bucket state.

        Parameters:
        ttl - fixed TTL that will be used for storing the buckets
        Returns:
        See Also:
        basedOnTimeForRefillingBucketUpToMax(Duration)
      • basedOnTimeForRefillingBucketUpToMax

        static ExpirationAfterWriteStrategy basedOnTimeForRefillingBucketUpToMax​(Duration keepAfterRefillDuration)
        Returns strategy that smartly calculates TTL based of current bucket state, buckets are not persisted in storage longer than time required to refill all consumed tokens plus keepAfterRefillDuration
        Parameters:
        keepAfterRefillDuration - specifies how long bucket should be held in the cache after all consumed tokens have been refilled. It can be a zero duration, but from performance considerations it is recommended to provide at least few seconds, because reusing of already persisted can be faster that initialization of bucket from scratch, so keepAfterRefillDuration helps to not evict buckets from storage too frequent.
        Returns:
        See Also:
        basedOnTimeForRefillingBucketUpToMax(Duration)