Skip to content

Commit 53739fc

Browse files
authored
Fix: encountering expired items during the traversal process will result in zero values in the output. (#163)
1 parent 97f49b4 commit 53739fc

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

expirable/expirable_lru.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,16 @@ func (c *LRU[K, V]) GetOldest() (key K, value V, ok bool) {
220220
}
221221

222222
// Keys returns a slice of the keys in the cache, from oldest to newest.
223+
// Expired entries are filtered out.
223224
func (c *LRU[K, V]) Keys() []K {
224225
c.mu.Lock()
225226
defer c.mu.Unlock()
226227
keys := make([]K, 0, len(c.items))
228+
now := time.Now()
227229
for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() {
230+
if now.After(ent.ExpiresAt) {
231+
continue
232+
}
228233
keys = append(keys, ent.Key)
229234
}
230235
return keys
@@ -235,15 +240,13 @@ func (c *LRU[K, V]) Keys() []K {
235240
func (c *LRU[K, V]) Values() []V {
236241
c.mu.Lock()
237242
defer c.mu.Unlock()
238-
values := make([]V, len(c.items))
239-
i := 0
243+
values := make([]V, 0, len(c.items))
240244
now := time.Now()
241245
for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() {
242246
if now.After(ent.ExpiresAt) {
243247
continue
244248
}
245-
values[i] = ent.Value
246-
i++
249+
values = append(values, ent.Value)
247250
}
248251
return values
249252
}

0 commit comments

Comments
 (0)