From 05b47f335c8ebaa1238535b45f1f393495291300 Mon Sep 17 00:00:00 2001 From: cyhone Date: Tue, 28 Nov 2023 22:40:39 +0800 Subject: [PATCH] check LifeWindow when CleanWindow is set (#386) --- bigcache.go | 7 ++++++- bigcache_test.go | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bigcache.go b/bigcache.go index 5620c0ef..8a818eb8 100644 --- a/bigcache.go +++ b/bigcache.go @@ -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, diff --git a/bigcache_test.go b/bigcache_test.go index 43ec3f57..2f86a6d6 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -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) @@ -1101,6 +1101,7 @@ func TestClosing(t *testing.T) { config := Config{ CleanWindow: time.Minute, Shards: 1, + LifeWindow: 1 * time.Second, } startGR := runtime.NumGoroutine()