fix golint issues in core/collection, refine cache interface (#475)

This commit is contained in:
Kevin Wan
2021-02-18 15:49:56 +08:00
committed by GitHub
parent f14ab70035
commit 457048bfac
14 changed files with 139 additions and 89 deletions

View File

@@ -12,11 +12,11 @@ import (
type (
Cache interface {
DelCache(keys ...string) error
GetCache(key string, v interface{}) error
Del(keys ...string) error
Get(key string, v interface{}) error
IsNotFound(err error) bool
SetCache(key string, v interface{}) error
SetCacheWithExpire(key string, v interface{}, expire time.Duration) error
Set(key string, v interface{}) error
SetWithExpire(key string, v interface{}, expire time.Duration) error
Take(v interface{}, key string, query func(v interface{}) error) error
TakeWithExpire(v interface{}, key string, query func(v interface{}, expire time.Duration) error) error
}
@@ -27,19 +27,19 @@ type (
}
)
func NewCache(c ClusterConf, barrier syncx.SharedCalls, st *CacheStat, errNotFound error,
func New(c ClusterConf, barrier syncx.SharedCalls, st *CacheStat, errNotFound error,
opts ...Option) Cache {
if len(c) == 0 || TotalWeights(c) <= 0 {
log.Fatal("no cache nodes")
}
if len(c) == 1 {
return NewCacheNode(c[0].NewRedis(), barrier, st, errNotFound, opts...)
return NewNode(c[0].NewRedis(), barrier, st, errNotFound, opts...)
}
dispatcher := hash.NewConsistentHash()
for _, node := range c {
cn := NewCacheNode(node.NewRedis(), barrier, st, errNotFound, opts...)
cn := NewNode(node.NewRedis(), barrier, st, errNotFound, opts...)
dispatcher.AddWithWeight(cn, node.Weight)
}
@@ -49,7 +49,7 @@ func NewCache(c ClusterConf, barrier syncx.SharedCalls, st *CacheStat, errNotFou
}
}
func (cc cacheCluster) DelCache(keys ...string) error {
func (cc cacheCluster) Del(keys ...string) error {
switch len(keys) {
case 0:
return nil
@@ -60,7 +60,7 @@ func (cc cacheCluster) DelCache(keys ...string) error {
return cc.errNotFound
}
return c.(Cache).DelCache(key)
return c.(Cache).Del(key)
default:
var be errorx.BatchError
nodes := make(map[interface{}][]string)
@@ -74,7 +74,7 @@ func (cc cacheCluster) DelCache(keys ...string) error {
nodes[c] = append(nodes[c], key)
}
for c, ks := range nodes {
if err := c.(Cache).DelCache(ks...); err != nil {
if err := c.(Cache).Del(ks...); err != nil {
be.Add(err)
}
}
@@ -83,35 +83,35 @@ func (cc cacheCluster) DelCache(keys ...string) error {
}
}
func (cc cacheCluster) GetCache(key string, v interface{}) error {
func (cc cacheCluster) Get(key string, v interface{}) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).GetCache(key, v)
return c.(Cache).Get(key, v)
}
func (cc cacheCluster) IsNotFound(err error) bool {
return err == cc.errNotFound
}
func (cc cacheCluster) SetCache(key string, v interface{}) error {
func (cc cacheCluster) Set(key string, v interface{}) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).SetCache(key, v)
return c.(Cache).Set(key, v)
}
func (cc cacheCluster) SetCacheWithExpire(key string, v interface{}, expire time.Duration) error {
func (cc cacheCluster) SetWithExpire(key string, v interface{}, expire time.Duration) error {
c, ok := cc.dispatcher.Get(key)
if !ok {
return cc.errNotFound
}
return c.(Cache).SetCacheWithExpire(key, v, expire)
return c.(Cache).SetWithExpire(key, v, expire)
}
func (cc cacheCluster) Take(v interface{}, key string, query func(v interface{}) error) error {

View File

@@ -21,7 +21,7 @@ type mockedNode struct {
errNotFound error
}
func (mc *mockedNode) DelCache(keys ...string) error {
func (mc *mockedNode) Del(keys ...string) error {
var be errorx.BatchError
for _, key := range keys {
if _, ok := mc.vals[key]; !ok {
@@ -33,7 +33,7 @@ func (mc *mockedNode) DelCache(keys ...string) error {
return be.Err()
}
func (mc *mockedNode) GetCache(key string, v interface{}) error {
func (mc *mockedNode) Get(key string, v interface{}) error {
bs, ok := mc.vals[key]
if ok {
return json.Unmarshal(bs, v)
@@ -46,7 +46,7 @@ func (mc *mockedNode) IsNotFound(err error) bool {
return err == mc.errNotFound
}
func (mc *mockedNode) SetCache(key string, v interface{}) error {
func (mc *mockedNode) Set(key string, v interface{}) error {
data, err := json.Marshal(v)
if err != nil {
return err
@@ -56,20 +56,20 @@ func (mc *mockedNode) SetCache(key string, v interface{}) error {
return nil
}
func (mc *mockedNode) SetCacheWithExpire(key string, v interface{}, expire time.Duration) error {
return mc.SetCache(key, v)
func (mc *mockedNode) SetWithExpire(key string, v interface{}, expire time.Duration) error {
return mc.Set(key, v)
}
func (mc *mockedNode) Take(v interface{}, key string, query func(v interface{}) error) error {
if _, ok := mc.vals[key]; ok {
return mc.GetCache(key, v)
return mc.Get(key, v)
}
if err := query(v); err != nil {
return err
}
return mc.SetCache(key, v)
return mc.Set(key, v)
}
func (mc *mockedNode) TakeWithExpire(v interface{}, key string, query func(v interface{}, expire time.Duration) error) error {
@@ -102,26 +102,26 @@ func TestCache_SetDel(t *testing.T) {
Weight: 100,
},
}
c := NewCache(conf, syncx.NewSharedCalls(), NewCacheStat("mock"), errPlaceholder)
c := New(conf, syncx.NewSharedCalls(), NewCacheStat("mock"), errPlaceholder)
for i := 0; i < total; i++ {
if i%2 == 0 {
assert.Nil(t, c.SetCache(fmt.Sprintf("key/%d", i), i))
assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i))
} else {
assert.Nil(t, c.SetCacheWithExpire(fmt.Sprintf("key/%d", i), i, 0))
assert.Nil(t, c.SetWithExpire(fmt.Sprintf("key/%d", i), i, 0))
}
}
for i := 0; i < total; i++ {
var v int
assert.Nil(t, c.GetCache(fmt.Sprintf("key/%d", i), &v))
assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &v))
assert.Equal(t, i, v)
}
assert.Nil(t, c.DelCache())
assert.Nil(t, c.Del())
for i := 0; i < total; i++ {
assert.Nil(t, c.DelCache(fmt.Sprintf("key/%d", i)))
assert.Nil(t, c.Del(fmt.Sprintf("key/%d", i)))
}
for i := 0; i < total; i++ {
var v int
assert.True(t, c.IsNotFound(c.GetCache(fmt.Sprintf("key/%d", i), &v)))
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &v)))
assert.Equal(t, 0, v)
}
}
@@ -140,26 +140,26 @@ func TestCache_OneNode(t *testing.T) {
Weight: 100,
},
}
c := NewCache(conf, syncx.NewSharedCalls(), NewCacheStat("mock"), errPlaceholder)
c := New(conf, syncx.NewSharedCalls(), NewCacheStat("mock"), errPlaceholder)
for i := 0; i < total; i++ {
if i%2 == 0 {
assert.Nil(t, c.SetCache(fmt.Sprintf("key/%d", i), i))
assert.Nil(t, c.Set(fmt.Sprintf("key/%d", i), i))
} else {
assert.Nil(t, c.SetCacheWithExpire(fmt.Sprintf("key/%d", i), i, 0))
assert.Nil(t, c.SetWithExpire(fmt.Sprintf("key/%d", i), i, 0))
}
}
for i := 0; i < total; i++ {
var v int
assert.Nil(t, c.GetCache(fmt.Sprintf("key/%d", i), &v))
assert.Nil(t, c.Get(fmt.Sprintf("key/%d", i), &v))
assert.Equal(t, i, v)
}
assert.Nil(t, c.DelCache())
assert.Nil(t, c.Del())
for i := 0; i < total; i++ {
assert.Nil(t, c.DelCache(fmt.Sprintf("key/%d", i)))
assert.Nil(t, c.Del(fmt.Sprintf("key/%d", i)))
}
for i := 0; i < total; i++ {
var v int
assert.True(t, c.IsNotFound(c.GetCache(fmt.Sprintf("key/%d", i), &v)))
assert.True(t, c.IsNotFound(c.Get(fmt.Sprintf("key/%d", i), &v)))
assert.Equal(t, 0, v)
}
}
@@ -188,7 +188,7 @@ func TestCache_Balance(t *testing.T) {
errNotFound: errPlaceholder,
}
for i := 0; i < total; i++ {
assert.Nil(t, c.SetCache(strconv.Itoa(i), i))
assert.Nil(t, c.Set(strconv.Itoa(i), i))
}
counts := make(map[int]int)
@@ -201,13 +201,13 @@ func TestCache_Balance(t *testing.T) {
for i := 0; i < total; i++ {
var v int
assert.Nil(t, c.GetCache(strconv.Itoa(i), &v))
assert.Nil(t, c.Get(strconv.Itoa(i), &v))
assert.Equal(t, i, v)
}
for i := 0; i < total/10; i++ {
assert.Nil(t, c.DelCache(strconv.Itoa(i*10), strconv.Itoa(i*10+1), strconv.Itoa(i*10+2)))
assert.Nil(t, c.DelCache(strconv.Itoa(i*10+9)))
assert.Nil(t, c.Del(strconv.Itoa(i*10), strconv.Itoa(i*10+1), strconv.Itoa(i*10+2)))
assert.Nil(t, c.Del(strconv.Itoa(i*10+9)))
}
var count int
@@ -237,11 +237,11 @@ func TestCacheNoNode(t *testing.T) {
dispatcher: dispatcher,
errNotFound: errPlaceholder,
}
assert.NotNil(t, c.DelCache("foo"))
assert.NotNil(t, c.DelCache("foo", "bar", "any"))
assert.NotNil(t, c.GetCache("foo", nil))
assert.NotNil(t, c.SetCache("foo", nil))
assert.NotNil(t, c.SetCacheWithExpire("foo", nil, time.Second))
assert.NotNil(t, c.Del("foo"))
assert.NotNil(t, c.Del("foo", "bar", "any"))
assert.NotNil(t, c.Get("foo", nil))
assert.NotNil(t, c.Set("foo", nil))
assert.NotNil(t, c.SetWithExpire("foo", nil, time.Second))
assert.NotNil(t, c.Take(nil, "foo", func(v interface{}) error {
return nil
}))

View File

@@ -37,13 +37,13 @@ type cacheNode struct {
errNotFound error
}
// NewCacheNode returns a cacheNode.
// NewNode returns a cacheNode.
// rds is the underlying redis node or cluster.
// barrier is the barrier that maybe shared with other cache nodes on cache cluster.
// st is used to stat the cache.
// errNotFound defines the error that returned on cache not found.
// opts are the options that customize the cacheNode.
func NewCacheNode(rds *redis.Redis, barrier syncx.SharedCalls, st *CacheStat,
func NewNode(rds *redis.Redis, barrier syncx.SharedCalls, st *CacheStat,
errNotFound error, opts ...Option) Cache {
o := newOptions(opts...)
return cacheNode{
@@ -60,7 +60,7 @@ func NewCacheNode(rds *redis.Redis, barrier syncx.SharedCalls, st *CacheStat,
}
// DelCache deletes cached values with keys.
func (c cacheNode) DelCache(keys ...string) error {
func (c cacheNode) Del(keys ...string) error {
if len(keys) == 0 {
return nil
}
@@ -74,7 +74,7 @@ func (c cacheNode) DelCache(keys ...string) error {
}
// GetCache gets the cache with key and fills into v.
func (c cacheNode) GetCache(key string, v interface{}) error {
func (c cacheNode) Get(key string, v interface{}) error {
err := c.doGetCache(key, v)
if err == errPlaceholder {
return c.errNotFound
@@ -89,12 +89,12 @@ func (c cacheNode) IsNotFound(err error) bool {
}
// SetCache sets the cache with key and v, using c.expiry.
func (c cacheNode) SetCache(key string, v interface{}) error {
return c.SetCacheWithExpire(key, v, c.aroundDuration(c.expiry))
func (c cacheNode) Set(key string, v interface{}) error {
return c.SetWithExpire(key, v, c.aroundDuration(c.expiry))
}
// SetCacheWithExpire sets the cache with key and v, using given expire.
func (c cacheNode) SetCacheWithExpire(key string, v interface{}, expire time.Duration) error {
func (c cacheNode) SetWithExpire(key string, v interface{}, expire time.Duration) error {
data, err := jsonx.Marshal(v)
if err != nil {
return err
@@ -112,7 +112,7 @@ func (c cacheNode) String() string {
// query from DB and set cache using c.expiry, then return the result.
func (c cacheNode) Take(v interface{}, key string, query func(v interface{}) error) error {
return c.doTake(v, key, query, func(v interface{}) error {
return c.SetCache(key, v)
return c.Set(key, v)
})
}
@@ -124,7 +124,7 @@ func (c cacheNode) TakeWithExpire(v interface{}, key string, query func(v interf
return c.doTake(v, key, func(v interface{}) error {
return query(v, expire)
}, func(v interface{}) error {
return c.SetCacheWithExpire(key, v, expire)
return c.SetWithExpire(key, v, expire)
})
}

View File

@@ -39,14 +39,14 @@ func TestCacheNode_DelCache(t *testing.T) {
stat: NewCacheStat("any"),
errNotFound: errTestNotFound,
}
assert.Nil(t, cn.DelCache())
assert.Nil(t, cn.DelCache([]string{}...))
assert.Nil(t, cn.DelCache(make([]string, 0)...))
cn.SetCache("first", "one")
assert.Nil(t, cn.DelCache("first"))
cn.SetCache("first", "one")
cn.SetCache("second", "two")
assert.Nil(t, cn.DelCache("first", "second"))
assert.Nil(t, cn.Del())
assert.Nil(t, cn.Del([]string{}...))
assert.Nil(t, cn.Del(make([]string, 0)...))
cn.Set("first", "one")
assert.Nil(t, cn.Del("first"))
cn.Set("first", "one")
cn.Set("second", "two")
assert.Nil(t, cn.Del("first", "second"))
}
func TestCacheNode_InvalidCache(t *testing.T) {
@@ -64,7 +64,7 @@ func TestCacheNode_InvalidCache(t *testing.T) {
}
s.Set("any", "value")
var str string
assert.NotNil(t, cn.GetCache("any", &str))
assert.NotNil(t, cn.Get("any", &str))
assert.Equal(t, "", str)
_, err = s.Get("any")
assert.Equal(t, miniredis.ErrKeyNotFound, err)
@@ -91,7 +91,7 @@ func TestCacheNode_Take(t *testing.T) {
})
assert.Nil(t, err)
assert.Equal(t, "value", str)
assert.Nil(t, cn.GetCache("any", &str))
assert.Nil(t, cn.Get("any", &str))
val, err := store.Get("any")
assert.Nil(t, err)
assert.Equal(t, `"value"`, val)
@@ -116,7 +116,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
return errTestNotFound
})
assert.True(t, cn.IsNotFound(err))
assert.True(t, cn.IsNotFound(cn.GetCache("any", &str)))
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
val, err := store.Get("any")
assert.Nil(t, err)
assert.Equal(t, `*`, val)
@@ -126,7 +126,7 @@ func TestCacheNode_TakeNotFound(t *testing.T) {
return nil
})
assert.True(t, cn.IsNotFound(err))
assert.True(t, cn.IsNotFound(cn.GetCache("any", &str)))
assert.True(t, cn.IsNotFound(cn.Get("any", &str)))
store.Del("any")
var errDummy = errors.New("dummy")
@@ -157,7 +157,7 @@ func TestCacheNode_TakeWithExpire(t *testing.T) {
})
assert.Nil(t, err)
assert.Equal(t, "value", str)
assert.Nil(t, cn.GetCache("any", &str))
assert.Nil(t, cn.Get("any", &str))
val, err := store.Get("any")
assert.Nil(t, err)
assert.Equal(t, `"value"`, val)
@@ -200,8 +200,8 @@ func TestCacheValueWithBigInt(t *testing.T) {
value int64 = 323427211229009810
)
assert.Nil(t, cn.SetCache(key, value))
assert.Nil(t, cn.Set(key, value))
var val interface{}
assert.Nil(t, cn.GetCache(key, &val))
assert.Nil(t, cn.Get(key, &val))
assert.Equal(t, strconv.FormatInt(value, 10), fmt.Sprintf("%v", val))
}