Skip to content

Commit

Permalink
check LifeWindow when CleanWindow is set (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyahui authored Nov 28, 2023
1 parent 93a748a commit 05b47f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion bigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ func newBigCache(ctx context.Context, config Config, clock clock) (*BigCache, er
return nil, errors.New("HardMaxCacheSize must be >= 0")
}

lifeWindowSeconds := uint64(config.LifeWindow.Seconds())
if config.CleanWindow > 0 && lifeWindowSeconds == 0 {
return nil, errors.New("LifeWindow must be >= 1s when CleanWindow is set")
}

if config.Hasher == nil {
config.Hasher = newDefaultHasher()
}

cache := &BigCache{
shards: make([]*cacheShard, config.Shards),
lifeWindow: uint64(config.LifeWindow.Seconds()),
lifeWindow: lifeWindowSeconds,
clock: clock,
hash: config.Hasher,
config: config,
Expand Down
3 changes: 2 additions & 1 deletion bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ func TestCacheDelRandomly(t *testing.T) {
func TestWriteAndReadParallelSameKeyWithStats(t *testing.T) {
t.Parallel()

c := DefaultConfig(0)
c := DefaultConfig(10 * time.Second)
c.StatsEnabled = true

cache, _ := New(context.Background(), c)
Expand Down Expand Up @@ -1101,6 +1101,7 @@ func TestClosing(t *testing.T) {
config := Config{
CleanWindow: time.Minute,
Shards: 1,
LifeWindow: 1 * time.Second,
}
startGR := runtime.NumGoroutine()

Expand Down

0 comments on commit 05b47f3

Please sign in to comment.