From aae930f03c1b58359d858ab4796c57f0947623c4 Mon Sep 17 00:00:00 2001 From: Elias Van Ootegem Date: Wed, 31 May 2017 15:36:16 +0100 Subject: [PATCH] Adding some unitlity functions to cache: * GetRefresh: Get item from cache, update TTL. This allows users to store resources in cache and have them automatically removed if they haven't been used for X amount of time * GetRefreshDefault: Same as GetRefresh with default duration (=~ SetDefault) * Pop: Get elements from cache, and remove them --- cache.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/cache.go b/cache.go index 30b1ea2..8530f30 100644 --- a/cache.go +++ b/cache.go @@ -135,6 +135,60 @@ func (c *cache) Get(k string) (interface{}, bool) { return item.Object, true } +// GetRefers returns an item from the cache in the same way as Get. +// If the item is set, and found not to have expired, its expiration is set to +// time.Now().Add(d) (ie -> TTL is incremented with given duration) +// this can be used to reduce the TTL, or keep elements in cache longer +func (c *cache) GetRefresh(k string, d time.Duration) (interface{}, bool) { + if d == DefaultExpiration { + d = c.defaultExpiration + } + c.mu.Lock() + item, found := c.items[k] + if !found { + c.mu.Unlock() + return nil, false + } + if item.Expiration > 0 { + if time.Now().UnixNano() > item.Expiration { + c.mu.Unlock() + return nil, false + } + } + + // reset expiration + item.Expiration = time.Now().Add(d).UnixNano() + c.mu.Unlock() + return item.Object, true +} + +// GetRefreshDefault is equivalent to calling GetRefresh with the +// default expiration time passed to New function +func (c *cache) GetRefreshDefault(k string) (interface{}, bool) { + return c.GetRefresh(k, DefaultExpiration) +} + +// Pop returns an item from cache if it was set and not expired +// the item is removed immediately +// expired values are not deleted, but left for the janitor to clean up +func (c *cache) Pop(k string) (interface{}, bool) { + c.mu.Lock() + item, found := c.items[k] + if !found { + c.mu.Unlock() + return nil, false + } + + if item.Expiration > 0 && time.Now().UnixNano() > item.Expiration { + c.mu.Unlock() + return nil, false + } + delete(c.items, k) + c.mu.Unlock() + + return item.Object, true +} + // GetWithExpiration returns an item and its expiration time from the cache. // It returns the item or nil, the expiration time if one is set (if the item // never expires a zero value for time.Time is returned), and a bool indicating