From 31c7be0bed73008bb6b43fc60c23711455d66614 Mon Sep 17 00:00:00 2001 From: Patrick Mylund Nielsen Date: Mon, 30 Nov 2015 13:50:17 -0500 Subject: [PATCH] 'Inline' Get and Expired --- cache.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cache.go b/cache.go index 2599f1a..a94bbd0 100644 --- a/cache.go +++ b/cache.go @@ -26,7 +26,11 @@ func (item Item) expired(now time.Time) bool { // Returns true if the item has expired. func (item Item) Expired() bool { - return item.expired(time.Now()) + // "Inlining" of expired + if item.Expiration == emptyTime { + return false + } + return item.Expiration.Before(time.Now()) } const ( @@ -108,9 +112,14 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error { // whether the key was found. func (c *cache) Get(k string) (interface{}, bool) { c.mu.RLock() - x, found := c.get(k) + // "Inlining" of get + item, found := c.items[k] + if !found || item.Expired() { + c.mu.RUnlock() + return nil, false + } c.mu.RUnlock() - return x, found + return item.Object, found } func (c *cache) get(k string) (interface{}, bool) {