Skip to content

Commit 4e0d34e

Browse files
committed
Only get the current time once in the DeleteExpired loop
1 parent a45ed98 commit 4e0d34e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

cache.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ type Item struct {
1717
Expiration time.Time
1818
}
1919

20-
// Returns true if the item has expired.
21-
func (item Item) Expired() bool {
20+
func (item Item) expired(now time.Time) bool {
2221
if item.Expiration == emptyTime {
2322
return false
2423
}
25-
return item.Expiration.Before(time.Now())
24+
return item.Expiration.Before(now)
25+
}
26+
27+
// Returns true if the item has expired.
28+
func (item Item) Expired() bool {
29+
return item.expired(time.Now())
2630
}
2731

2832
const (
@@ -868,9 +872,10 @@ type keyAndValue struct {
868872
// Delete all expired items from the cache.
869873
func (c *cache) DeleteExpired() {
870874
var evictedItems []keyAndValue
875+
now := time.Now()
871876
c.mu.Lock()
872877
for k, v := range c.items {
873-
if v.Expired() {
878+
if v.expired(now) {
874879
ov, evicted := c.delete(k)
875880
if evicted {
876881
evictedItems = append(evictedItems, keyAndValue{k, ov})

0 commit comments

Comments
 (0)