add Expire ExpireAt command

This commit is contained in:
zhuangzebo 2015-04-14 17:28:35 +08:00
parent 03284ca422
commit 56403766d8
1 changed files with 28 additions and 0 deletions

View File

@ -818,6 +818,34 @@ func (c *cache) delete(k string) {
delete(c.items, k) 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. // Delete all expired items from the cache.
func (c *cache) DeleteExpired() { func (c *cache) DeleteExpired() {
c.Lock() c.Lock()