Skip to content

Commit

Permalink
expirable LRU: fix so that Get/Peek cannot return an ok and empty val…
Browse files Browse the repository at this point in the history
…ue (#156)

* expirable LRU: fix so that Get/Peek cannot return an ok and empty value
  • Loading branch information
irenarindos authored Sep 14, 2023
1 parent 56a2dc0 commit d46c1d9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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

0 comments on commit d46c1d9

Please sign in to comment.