Module io.github.bucket4j.core
Package io.github.bucket4j.distributed
Interface ExpirationAfterWriteStrategy
-
public interface ExpirationAfterWriteStrategyRepresents the strategy for choosing time to live for buckets in the cache.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Modifier and Type Method Description static ExpirationAfterWriteStrategybasedOnTimeForRefillingBucketUpToMax(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 keepAfterRefillDurationlongcalculateTimeToLiveMillis(RemoteBucketState state, long currentTimeNanos)Calculates the time to live for bucket that is going to be persisted to the remote storagestatic ExpirationAfterWriteStrategyfixedTimeToLive(Duration ttl)Returns strategy that makes to store buckets with fixed TTL.static ExpirationAfterWriteStrategynone()Returns strategy that makes to store buckets forever.
-
-
-
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 storagecurrentTimeNanos- 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
-
none
static ExpirationAfterWriteStrategy none()
Returns strategy that makes to store buckets forever.Do not use this strategy in use-case of many buckets in order to avoid storage overflowing by unused buckets.
- Returns:
- See Also:
fixedTimeToLive(Duration),basedOnTimeForRefillingBucketUpToMax(Duration)
-
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)
-
-