From 4a10e24f29d4d4d3ac7415611308cf0e6293ae7c Mon Sep 17 00:00:00 2001 From: BugLesser <17674638850@163.com> Date: Thu, 29 Dec 2022 13:08:49 +0800 Subject: [PATCH 1/3] add Rename() support --- cache.go | 12 ++++++++++++ cache_test.go | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/cache.go b/cache.go index db88d2f..51f1876 100644 --- a/cache.go +++ b/cache.go @@ -115,6 +115,18 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error { return nil } +func (c *cache) Rename(oldk, newk string) error { + c.mu.Lock() + defer c.mu.Unlock() + item, found := c.get(oldk) + if !found { + return fmt.Errorf("Item %s doesn't exist", 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) { diff --git a/cache_test.go b/cache_test.go index de3e9d6..3146ad7 100644 --- a/cache_test.go +++ b/cache_test.go @@ -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(err) + } + 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) From e521233701ec685d6aa0ca33b0727992230c74dc Mon Sep 17 00:00:00 2001 From: BugLesser <17674638850@163.com> Date: Thu, 29 Dec 2022 05:32:13 +0000 Subject: [PATCH 2/3] add Rename() support --- cache.go | 4 ++-- cache_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cache.go b/cache.go index 51f1876..4f7630f 100644 --- a/cache.go +++ b/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 { c.mu.Lock() defer c.mu.Unlock() - item, found := c.get(oldk) - if !found { + 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 diff --git a/cache_test.go b/cache_test.go index 3146ad7..57c09be 100644 --- a/cache_test.go +++ b/cache_test.go @@ -1140,8 +1140,8 @@ func TestReplace(t *testing.T) { func TestRename(t *testing.T) { tc := New(DefaultExpiration, 0) err := tc.Rename("foo", "bar") - if err != nil { - t.Error(err) + if err == nil { + t.Error("Renamed foo when it shouldn't exist") } tc.Set("foo", 123, DefaultExpiration) err = tc.Rename("foo", "bar") From c0459cf8967306eb1e3f1b9b5249ccfc210fbef1 Mon Sep 17 00:00:00 2001 From: BugLesser <17674638850@163.com> Date: Thu, 29 Dec 2022 05:35:39 +0000 Subject: [PATCH 3/3] add Rename() support --- cache.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cache.go b/cache.go index 4f7630f..4c33353 100644 --- a/cache.go +++ b/cache.go @@ -115,6 +115,8 @@ 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()