Added IncrementExpiration method

This commit is contained in:
Berk Gokden 2020-11-18 22:41:22 +01:00
parent 46f4078530
commit f5442e337b
1 changed files with 21 additions and 0 deletions

View File

@ -922,6 +922,27 @@ func (c *cache) delete(k string) (interface{}, bool) {
return nil, false 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 { type keyAndValue struct {
key string key string
value interface{} value interface{}