Skip to content

Commit

Permalink
No need to del elem when config.CleanWindow>0 at method:Set (#325)
Browse files Browse the repository at this point in the history
Co-authored-by: Yaping Zhang <[email protected]>
  • Loading branch information
zhangyaping-CN and Yaping Zhang authored Aug 2, 2022
1 parent 7c2932e commit 8f80589
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
14 changes: 14 additions & 0 deletions bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
20 changes: 14 additions & 6 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
}
}

0 comments on commit 8f80589

Please sign in to comment.