Skip to content

Commit

Permalink
add config for cleanup ticker duration (#342)
Browse files Browse the repository at this point in the history
## Problem
Cleanup ticker to remove entries for expired keys taking a lot of CPU
cycles around 90% in the case of pod with 1.6 CPU

## Solution
Making the duration of the cleanup ticker configurable so users can
configure it to higher values and can save on CPU cycles.
  • Loading branch information
singhvikash11 committed Aug 31, 2023
1 parent c5789d6 commit e8dc5b0
Showing 1 changed file with 5 additions and 1 deletion.
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

0 comments on commit e8dc5b0

Please sign in to comment.