Skip to content

Commit 9289779

Browse files
committed
DeleteExpired: avoid unnecessary m.Cache.Get()
1 parent 0c0a353 commit 9289779

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

cache.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -249,16 +249,12 @@ func (c *Cache[K, V]) DeleteExpired() {
249249
if c.expManager.len() == 0 {
250250
return false
251251
}
252-
key := c.expManager.pop()
253-
// if is expired, delete it and return nil instead
254-
item, ok := c.cache.Get(key)
255-
if ok {
256-
if item.Expired() {
257-
c.cache.Delete(key)
258-
return true
259-
}
260-
c.expManager.update(key, item.Expiration)
252+
key, expiration := c.expManager.pop()
253+
if nowFunc().After(expiration) {
254+
c.cache.Delete(key)
255+
return true
261256
}
257+
c.expManager.update(key, expiration)
262258
return false
263259
}
264260

expiration.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ func (m *expirationManager[K]) len() int {
3737
return m.queue.Len()
3838
}
3939

40-
func (m *expirationManager[K]) pop() K {
40+
func (m *expirationManager[K]) pop() (K, time.Time) {
4141
v := heap.Pop(&m.queue)
4242
key := v.(*expirationKey[K]).key
43+
exp := v.(*expirationKey[K]).expiration
4344
delete(m.mapping, key)
44-
return key
45+
return key, exp
4546
}
4647

4748
func (m *expirationManager[K]) remove(key K) {

0 commit comments

Comments
 (0)