add Rename() support
This commit is contained in:
parent
4a10e24f29
commit
e521233701
4
cache.go
4
cache.go
|
@ -118,10 +118,10 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
|
||||||
func (c *cache) Rename(oldk, newk string) error {
|
func (c *cache) Rename(oldk, newk string) error {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
item, found := c.get(oldk)
|
if _, found := c.get(oldk); !found {
|
||||||
if !found {
|
|
||||||
return fmt.Errorf("Item %s doesn't exist", oldk)
|
return fmt.Errorf("Item %s doesn't exist", oldk)
|
||||||
}
|
}
|
||||||
|
item := c.items[oldk]
|
||||||
delete(c.items, oldk)
|
delete(c.items, oldk)
|
||||||
c.items[newk] = item
|
c.items[newk] = item
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1140,8 +1140,8 @@ func TestReplace(t *testing.T) {
|
||||||
func TestRename(t *testing.T) {
|
func TestRename(t *testing.T) {
|
||||||
tc := New(DefaultExpiration, 0)
|
tc := New(DefaultExpiration, 0)
|
||||||
err := tc.Rename("foo", "bar")
|
err := tc.Rename("foo", "bar")
|
||||||
if err != nil {
|
if err == nil {
|
||||||
t.Error(err)
|
t.Error("Renamed foo when it shouldn't exist")
|
||||||
}
|
}
|
||||||
tc.Set("foo", 123, DefaultExpiration)
|
tc.Set("foo", 123, DefaultExpiration)
|
||||||
err = tc.Rename("foo", "bar")
|
err = tc.Rename("foo", "bar")
|
||||||
|
|
Loading…
Reference in New Issue