Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No need to del elem when config.CleanWindow>0 at method:Set #325

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
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,
}
}