From bf3ce168237cd78ede1afdc3b96e47decf613f13 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 19 Aug 2021 22:48:21 +0800 Subject: [PATCH] fix #820 (#934) --- core/stores/cache/cache_test.go | 2 ++ core/stores/cache/cachenode.go | 15 ++++++++++++--- core/stores/cache/cachenode_test.go | 18 ++++++++++++++++++ tools/goctl/util/env/env_test.go | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/stores/cache/cache_test.go b/core/stores/cache/cache_test.go index 92d30aed..65b710ff 100644 --- a/core/stores/cache/cache_test.go +++ b/core/stores/cache/cache_test.go @@ -23,6 +23,7 @@ type mockedNode struct { func (mc *mockedNode) Del(keys ...string) error { var be errorx.BatchError + for _, key := range keys { if _, ok := mc.vals[key]; !ok { be.Add(mc.errNotFound) @@ -30,6 +31,7 @@ func (mc *mockedNode) Del(keys ...string) error { delete(mc.vals, key) } } + return be.Err() } diff --git a/core/stores/cache/cachenode.go b/core/stores/cache/cachenode.go index 2405e3ff..9a39c355 100644 --- a/core/stores/cache/cachenode.go +++ b/core/stores/cache/cachenode.go @@ -65,9 +65,18 @@ func (c cacheNode) Del(keys ...string) error { return nil } - if _, err := c.rds.Del(keys...); err != nil { - logx.Errorf("failed to clear cache with keys: %q, error: %v", formatKeys(keys), err) - c.asyncRetryDelCache(keys...) + if len(keys) > 1 && c.rds.Type == redis.ClusterType { + for _, key := range keys { + if _, err := c.rds.Del(key); err != nil { + logx.Errorf("failed to clear cache with key: %q, error: %v", key, err) + c.asyncRetryDelCache(key) + } + } + } else { + if _, err := c.rds.Del(keys...); err != nil { + logx.Errorf("failed to clear cache with keys: %q, error: %v", formatKeys(keys), err) + c.asyncRetryDelCache(keys...) + } } return nil diff --git a/core/stores/cache/cachenode_test.go b/core/stores/cache/cachenode_test.go index 95c86a5c..13f83d9e 100644 --- a/core/stores/cache/cachenode_test.go +++ b/core/stores/cache/cachenode_test.go @@ -29,6 +29,7 @@ func init() { func TestCacheNode_DelCache(t *testing.T) { store, clean, err := redistest.CreateRedis() assert.Nil(t, err) + store.Type = redis.ClusterType defer clean() cn := cacheNode{ @@ -49,6 +50,23 @@ func TestCacheNode_DelCache(t *testing.T) { assert.Nil(t, cn.Del("first", "second")) } +func TestCacheNode_DelCacheWithErrors(t *testing.T) { + store, clean, err := redistest.CreateRedis() + assert.Nil(t, err) + store.Type = redis.ClusterType + clean() + + cn := cacheNode{ + rds: store, + r: rand.New(rand.NewSource(time.Now().UnixNano())), + lock: new(sync.Mutex), + unstableExpiry: mathx.NewUnstable(expiryDeviation), + stat: NewStat("any"), + errNotFound: errTestNotFound, + } + assert.Nil(t, cn.Del("third", "fourth")) +} + func TestCacheNode_InvalidCache(t *testing.T) { s, err := miniredis.Run() assert.Nil(t, err) diff --git a/tools/goctl/util/env/env_test.go b/tools/goctl/util/env/env_test.go index 044a870a..8de8e9c6 100644 --- a/tools/goctl/util/env/env_test.go +++ b/tools/goctl/util/env/env_test.go @@ -70,7 +70,7 @@ func TestCanExec(t *testing.T) { assert.Equal(t, canExec, CanExec()) } -func execCommand(cmd string, arg ...string) (stdout string, stderr string, err error) { +func execCommand(cmd string, arg ...string) (stdout, stderr string, err error) { output := bytes.NewBuffer(nil) errOutput := bytes.NewBuffer(nil) c := exec.Command(cmd, arg...)