Skip to content

Commit d46c1d9

Browse files
authored
expirable LRU: fix so that Get/Peek cannot return an ok and empty value (#156)
* expirable LRU: fix so that Get/Peek cannot return an ok and empty value
1 parent 56a2dc0 commit d46c1d9

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

expirable/expirable_lru.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (c *LRU[K, V]) Get(key K) (value V, ok bool) {
153153
if ent, ok = c.items[key]; ok {
154154
// Expired item check
155155
if time.Now().After(ent.ExpiresAt) {
156-
return
156+
return value, false
157157
}
158158
c.evictList.MoveToFront(ent)
159159
return ent.Value, true
@@ -179,7 +179,7 @@ func (c *LRU[K, V]) Peek(key K) (value V, ok bool) {
179179
if ent, ok = c.items[key]; ok {
180180
// Expired item check
181181
if time.Now().After(ent.ExpiresAt) {
182-
return
182+
return value, false
183183
}
184184
return ent.Value, true
185185
}

expirable/expirable_lru_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,17 @@ func TestLoadingExpired(t *testing.T) {
390390
t.Fatalf("should be true")
391391
}
392392

393-
time.Sleep(time.Millisecond * 100) // wait for entry to expire
393+
for {
394+
result, ok := lc.Get("key1")
395+
if ok && result == "" {
396+
t.Fatalf("ok should return a result")
397+
}
398+
if !ok {
399+
break
400+
}
401+
}
402+
403+
time.Sleep(time.Millisecond * 100) // wait for expiration reaper
394404
if lc.Len() != 0 {
395405
t.Fatalf("length differs from expected")
396406
}

0 commit comments

Comments
 (0)