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

expirable LRU: fix so that Get/Peek cannot return an ok and empty value #156

Merged
merged 2 commits into from
Sep 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions expirable/expirable_lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
if ent, ok = c.items[key]; ok {
// Expired item check
if time.Now().After(ent.ExpiresAt) {
return
return value, false
}
c.evictList.MoveToFront(ent)
return ent.Value, true
Expand All @@ -179,7 +179,7 @@ func (c *LRU[K, V]) Peek(key K) (value V, ok bool) {
if ent, ok = c.items[key]; ok {
// Expired item check
if time.Now().After(ent.ExpiresAt) {
return
return value, false
}
return ent.Value, true
}
Expand Down
12 changes: 11 additions & 1 deletion expirable/expirable_lru_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,17 @@ func TestLoadingExpired(t *testing.T) {
t.Fatalf("should be true")
}

time.Sleep(time.Millisecond * 100) // wait for entry to expire
for {
result, ok := lc.Get("key1")
if ok && result == "" {
t.Fatalf("ok should return a result")
}
if !ok {
break
}
}

time.Sleep(time.Millisecond * 100) // wait for expiration reaper
if lc.Len() != 0 {
t.Fatalf("length differs from expected")
}
Expand Down