From 56403766d833a2fd044204523f022c5860d48e74 Mon Sep 17 00:00:00 2001 From: zhuangzebo Date: Tue, 14 Apr 2015 17:28:35 +0800 Subject: [PATCH] add Expire ExpireAt command --- cache.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/cache.go b/cache.go index 12f7f0b..cddc11d 100644 --- a/cache.go +++ b/cache.go @@ -818,6 +818,34 @@ func (c *cache) delete(k string) { delete(c.items, k) } +func (c *cache) Expire(k string, d time.Duration) error { + c.Lock() + v, found := c.items[k] + if !found || v.Expired() { + c.Unlock() + return fmt.Errorf("key %s not found.", k) + } + + e := time.Now().Add(d) + v.Expiration = &e + + c.Unlock() + return nil +} + +func (c *cache) ExpireAt(k string, expire *time.Time) error { + c.Lock() + v, found := c.items[k] + if !found || v.Expired() { + c.Unlock() + return fmt.Errorf("key %s not found.", k) + } + + v.Expiration = expire + c.Unlock() + return nil +} + // Delete all expired items from the cache. func (c *cache) DeleteExpired() { c.Lock()