From f86d048fee2aee24edbe33b3f858c15f2e29df16 Mon Sep 17 00:00:00 2001 From: Yaping Zhang Date: Thu, 28 Jul 2022 16:57:40 +0800 Subject: [PATCH 1/2] No need to del elem when config.CleanWindow>0 at method:Set --- shard.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/shard.go b/shard.go index efb30310..b524c2ea 100644 --- a/shard.go +++ b/shard.go @@ -30,6 +30,7 @@ type cacheShard struct { hashmapStats map[uint64]uint32 stats Stats + cleanEnabled bool } func (s *cacheShard) getWithInfo(key string, hashedKey uint64) (entry []byte, resp Response, err error) { @@ -129,8 +130,10 @@ func (s *cacheShard) set(key string, hashedKey uint64, entry []byte) error { } } - if oldestEntry, err := s.entries.Peek(); err == nil { - s.onEvict(oldestEntry, currentTimestamp, s.removeOldestEntry) + if !s.cleanEnabled { + if oldestEntry, err := s.entries.Peek(); err == nil { + s.onEvict(oldestEntry, currentTimestamp, s.removeOldestEntry) + } } w := wrapEntry(currentTimestamp, hashedKey, key, entry, &s.entryBuffer) @@ -436,5 +439,6 @@ func initNewShard(config Config, callback onRemoveCallback, clock clock) *cacheS clock: clock, lifeWindow: uint64(config.LifeWindow.Seconds()), statsEnabled: config.StatsEnabled, + cleanEnabled: config.CleanWindow > 0, } } From 3a94d9499f5bf3d06cfb9fedf44ff1f24de5b4b3 Mon Sep 17 00:00:00 2001 From: Yaping Zhang Date: Tue, 2 Aug 2022 10:33:10 +0800 Subject: [PATCH 2/2] Test the set method without CleanWindow --- bigcache_test.go | 14 ++++++++++++++ shard.go | 12 ++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bigcache_test.go b/bigcache_test.go index 83714508..b448e075 100644 --- a/bigcache_test.go +++ b/bigcache_test.go @@ -1137,6 +1137,20 @@ func blob(char byte, len int) []byte { return bytes.Repeat([]byte{char}, len) } +func TestCache_SetWithoutCleanWindow(t *testing.T) { + + opt := DefaultConfig(time.Second) + opt.CleanWindow = 0 + opt.HardMaxCacheSize = 1 + bc, _ := NewBigCache(opt) + + err := bc.Set("2225", make([]byte, 200)) + if nil != err { + t.Error(err) + t.FailNow() + } +} + // func TestCache_RepeatedSetWithBiggerEntry(t *testing.T) { diff --git a/shard.go b/shard.go index b524c2ea..af718b42 100644 --- a/shard.go +++ b/shard.go @@ -154,8 +154,10 @@ func (s *cacheShard) set(key string, hashedKey uint64, entry []byte) error { func (s *cacheShard) addNewWithoutLock(key string, hashedKey uint64, entry []byte) error { currentTimestamp := uint64(s.clock.Epoch()) - if oldestEntry, err := s.entries.Peek(); err == nil { - s.onEvict(oldestEntry, currentTimestamp, s.removeOldestEntry) + if !s.cleanEnabled { + if oldestEntry, err := s.entries.Peek(); err == nil { + s.onEvict(oldestEntry, currentTimestamp, s.removeOldestEntry) + } } w := wrapEntry(currentTimestamp, hashedKey, key, entry, &s.entryBuffer) @@ -178,8 +180,10 @@ func (s *cacheShard) setWrappedEntryWithoutLock(currentTimestamp uint64, w []byt } } - if oldestEntry, err := s.entries.Peek(); err == nil { - s.onEvict(oldestEntry, currentTimestamp, s.removeOldestEntry) + if !s.cleanEnabled { + if oldestEntry, err := s.entries.Peek(); err == nil { + s.onEvict(oldestEntry, currentTimestamp, s.removeOldestEntry) + } } for {