From 4e0d34ef0010c94c55fbdc0ef08ea751979e7ed9 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Mon, 30 Nov 2015 13:39:27 -0500 Subject: [PATCH] Only get the current time once in the DeleteExpired loop --- cache.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/cache.go b/cache.go index 61040b8..2599f1a 100644 --- a/cache.go +++ b/cache.go @@ -17,12 +17,16 @@ type Item struct { Expiration time.Time } -// Returns true if the item has expired. -func (item Item) Expired() bool { +func (item Item) expired(now time.Time) bool { if item.Expiration == emptyTime { return false } - return item.Expiration.Before(time.Now()) + return item.Expiration.Before(now) +} + +// Returns true if the item has expired. +func (item Item) Expired() bool { + return item.expired(time.Now()) } const ( @@ -868,9 +872,10 @@ type keyAndValue struct { // Delete all expired items from the cache. func (c *cache) DeleteExpired() { var evictedItems []keyAndValue + now := time.Now() c.mu.Lock() for k, v := range c.items { - if v.Expired() { + if v.expired(now) { ov, evicted := c.delete(k) if evicted { evictedItems = append(evictedItems, keyAndValue{k, ov})