Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(configurable cleanup ticker): add config for cleanup ticker duration #342

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ type Config struct {
// cost passed to set is not using bytes as units. Keep in mind that setting
// this to true will increase the memory usage.
IgnoreInternalCost bool
// TtlTickerDurationInSec set the value of time ticker for cleanup keys on ttl
TtlTickerDurationInSec int64
}

type itemFlag byte
Expand Down Expand Up @@ -165,6 +167,8 @@ func NewCache(config *Config) (*Cache, error) {
return nil, errors.New("MaxCost can't be zero")
case config.BufferItems == 0:
return nil, errors.New("BufferItems can't be zero")
case config.TtlTickerDurationInSec == 0:
config.TtlTickerDurationInSec = bucketDurationSecs
}
policy := newPolicy(config.NumCounters, config.MaxCost)
cache := &Cache{
Expand All @@ -176,7 +180,7 @@ func NewCache(config *Config) (*Cache, error) {
stop: make(chan struct{}),
cost: config.Cost,
ignoreInternalCost: config.IgnoreInternalCost,
cleanupTicker: time.NewTicker(time.Duration(bucketDurationSecs) * time.Second / 2),
cleanupTicker: time.NewTicker(time.Duration(config.TtlTickerDurationInSec) * time.Second / 2),
}
cache.onExit = func(val interface{}) {
if config.OnExit != nil && val != nil {
Expand Down