Merge c0459cf896
into 46f4078530
This commit is contained in:
commit
70c3c63553
14
cache.go
14
cache.go
|
@ -115,6 +115,20 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Rename for the cache key only if it already exists, and the existing
|
||||
// item hasn't expired. Returns an error otherwise.
|
||||
func (c *cache) Rename(oldk, newk string) error {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if _, found := c.get(oldk); !found {
|
||||
return fmt.Errorf("Item %s doesn't exist", oldk)
|
||||
}
|
||||
item := c.items[oldk]
|
||||
delete(c.items, oldk)
|
||||
c.items[newk] = item
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get an item from the cache. Returns the item or nil, and a bool indicating
|
||||
// whether the key was found.
|
||||
func (c *cache) Get(k string) (interface{}, bool) {
|
||||
|
|
|
@ -1137,6 +1137,19 @@ func TestReplace(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRename(t *testing.T) {
|
||||
tc := New(DefaultExpiration, 0)
|
||||
err := tc.Rename("foo", "bar")
|
||||
if err == nil {
|
||||
t.Error("Renamed foo when it shouldn't exist")
|
||||
}
|
||||
tc.Set("foo", 123, DefaultExpiration)
|
||||
err = tc.Rename("foo", "bar")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
tc := New(DefaultExpiration, 0)
|
||||
tc.Set("foo", "bar", DefaultExpiration)
|
||||
|
|
Loading…
Reference in New Issue