From f5442e337b59332cff620947c288a15b6b36a621 Mon Sep 17 00:00:00 2001 From: Berk Gokden Date: Wed, 18 Nov 2020 22:41:22 +0100 Subject: [PATCH] Added IncrementExpiration method --- cache.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/cache.go b/cache.go index db88d2f..56565db 100644 --- a/cache.go +++ b/cache.go @@ -922,6 +922,27 @@ func (c *cache) delete(k string) (interface{}, bool) { return nil, false } +func (c *cache) IncrementExpiration(k string, d time.Duration) error { + c.mu.Lock() + v, found := c.items[k] + if !found || v.Expired() { + c.mu.Unlock() + return fmt.Errorf("key %s not found.", k) + } + + var e int64 + if d == DefaultExpiration { + d = c.defaultExpiration + } + if d > 0 { + e = time.Now().Add(d).UnixNano() + } + v.Expiration = e + + c.mu.Unlock() + return nil +} + type keyAndValue struct { key string value interface{}