From eb4f9f6b2f44f94b24f25173e58ffb8fa5c4f37e Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Mon, 30 Nov 2015 13:54:01 -0500 Subject: [PATCH] Use UnixNano int64s instead of Time --- cache.go | 18 +++++++++--------- cache_test.go | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cache.go b/cache.go index a94bbd0..5839ac1 100644 --- a/cache.go +++ b/cache.go @@ -14,23 +14,23 @@ var emptyTime = time.Time{} type Item struct { Object interface{} - Expiration time.Time + Expiration int64 } -func (item Item) expired(now time.Time) bool { - if item.Expiration == emptyTime { +func (item Item) expired(now int64) bool { + if item.Expiration == 0 { return false } - return item.Expiration.Before(now) + return now > item.Expiration } // Returns true if the item has expired. func (item Item) Expired() bool { // "Inlining" of expired - if item.Expiration == emptyTime { + if item.Expiration == 0 { return false } - return item.Expiration.Before(time.Now()) + return time.Now().UnixNano() > item.Expiration } const ( @@ -67,12 +67,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) { } func (c *cache) set(k string, x interface{}, d time.Duration) { - e := emptyTime + var e int64 if d == DefaultExpiration { d = c.defaultExpiration } if d > 0 { - e = time.Now().Add(d) + e = time.Now().Add(d).UnixNano() } c.items[k] = Item{ Object: x, @@ -881,7 +881,7 @@ type keyAndValue struct { // Delete all expired items from the cache. func (c *cache) DeleteExpired() { var evictedItems []keyAndValue - now := time.Now() + now := time.Now().UnixNano() c.mu.Lock() for k, v := range c.items { if v.expired(now) { diff --git a/cache_test.go b/cache_test.go index 2ff220e..f604794 100644 --- a/cache_test.go +++ b/cache_test.go @@ -110,11 +110,11 @@ func TestNewFrom(t *testing.T) { m := map[string]Item{ "a": Item{ Object: 1, - Expiration: emptyTime, + Expiration: 0, }, "b": Item{ Object: 2, - Expiration: emptyTime, + Expiration: 0, }, } tc := NewFrom(DefaultExpiration, 0, m)