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 efb30310..af718b42 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) @@ -151,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) @@ -175,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 { @@ -436,5 +443,6 @@ func initNewShard(config Config, callback onRemoveCallback, clock clock) *cacheS clock: clock, lifeWindow: uint64(config.LifeWindow.Seconds()), statsEnabled: config.StatsEnabled, + cleanEnabled: config.CleanWindow > 0, } }