Kevin Wan 3 years ago committed by GitHub
parent 189721da16
commit bf3ce16823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -23,6 +23,7 @@ type mockedNode struct {
func (mc *mockedNode) Del(keys ...string) error { func (mc *mockedNode) Del(keys ...string) error {
var be errorx.BatchError var be errorx.BatchError
for _, key := range keys { for _, key := range keys {
if _, ok := mc.vals[key]; !ok { if _, ok := mc.vals[key]; !ok {
be.Add(mc.errNotFound) be.Add(mc.errNotFound)
@ -30,6 +31,7 @@ func (mc *mockedNode) Del(keys ...string) error {
delete(mc.vals, key) delete(mc.vals, key)
} }
} }
return be.Err() return be.Err()
} }

@ -65,10 +65,19 @@ func (c cacheNode) Del(keys ...string) error {
return nil return nil
} }
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 { if _, err := c.rds.Del(keys...); err != nil {
logx.Errorf("failed to clear cache with keys: %q, error: %v", formatKeys(keys), err) logx.Errorf("failed to clear cache with keys: %q, error: %v", formatKeys(keys), err)
c.asyncRetryDelCache(keys...) c.asyncRetryDelCache(keys...)
} }
}
return nil return nil
} }

@ -29,6 +29,7 @@ func init() {
func TestCacheNode_DelCache(t *testing.T) { func TestCacheNode_DelCache(t *testing.T) {
store, clean, err := redistest.CreateRedis() store, clean, err := redistest.CreateRedis()
assert.Nil(t, err) assert.Nil(t, err)
store.Type = redis.ClusterType
defer clean() defer clean()
cn := cacheNode{ cn := cacheNode{
@ -49,6 +50,23 @@ func TestCacheNode_DelCache(t *testing.T) {
assert.Nil(t, cn.Del("first", "second")) 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) { func TestCacheNode_InvalidCache(t *testing.T) {
s, err := miniredis.Run() s, err := miniredis.Run()
assert.Nil(t, err) assert.Nil(t, err)

@ -70,7 +70,7 @@ func TestCanExec(t *testing.T) {
assert.Equal(t, canExec, CanExec()) 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) output := bytes.NewBuffer(nil)
errOutput := bytes.NewBuffer(nil) errOutput := bytes.NewBuffer(nil)
c := exec.Command(cmd, arg...) c := exec.Command(cmd, arg...)

Loading…
Cancel
Save