From 118023a573b07757f7bc65db392165a814e3d81c Mon Sep 17 00:00:00 2001 From: zhenghe3119 Date: Thu, 7 Nov 2019 19:47:57 +0800 Subject: [PATCH] cache: remove wrapper, add incr to redisext --- cache/constants.go | 6 ------ cache/redis/instance.go | 7 ++----- cache/redis/redis.go | 5 +---- cache/redisext/redisext.go | 11 +++++++++-- cache/redisext/redisext_test.go | 7 ------- cache/value/value.go | 1 - cache/value/value_test.go | 4 +--- 7 files changed, 13 insertions(+), 28 deletions(-) diff --git a/cache/constants.go b/cache/constants.go index 7b62307..b87aacf 100644 --- a/cache/constants.go +++ b/cache/constants.go @@ -43,9 +43,3 @@ func (c ConfigerType) String() string { } const DefaultRouteGroup = "default" - -// NOTE: 减小 key 所占空间 -const ( - WrapperTypeCache = "c" - WrapperTypeRedisExt = "e" -) diff --git a/cache/redis/instance.go b/cache/redis/instance.go index 5b0fad6..f75f7b6 100644 --- a/cache/redis/instance.go +++ b/cache/redis/instance.go @@ -22,11 +22,10 @@ const ( type InstanceConf struct { Group string Namespace string - Wrapper string } func (m *InstanceConf) String() string { - return fmt.Sprintf("group:%s namespace:%s wrapper:%s", m.Group, m.Namespace, m.Wrapper) + return fmt.Sprintf("group:%s namespace:%s", m.Group, m.Namespace) } func instanceConfFromString(s string) (conf *InstanceConf, err error) { @@ -38,7 +37,6 @@ func instanceConfFromString(s string) (conf *InstanceConf, err error) { conf = &InstanceConf{ Group: items[0], Namespace: items[1], - Wrapper: items[2], } return conf, nil } @@ -58,7 +56,6 @@ func (m *InstanceManager) buildKey(conf *InstanceConf) string { return strings.Join([]string{ conf.Group, conf.Namespace, - conf.Wrapper, }, keySep) } @@ -67,7 +64,7 @@ func (m *InstanceManager) add(key string, client *Client) { } func (m *InstanceManager) newInstance(ctx context.Context, conf *InstanceConf) (*Client, error) { - return NewClient(ctx, conf.Namespace, conf.Wrapper) + return NewClient(ctx, conf.Namespace) } func (m *InstanceManager) GetInstance(ctx context.Context, conf *InstanceConf) (*Client, error) { diff --git a/cache/redis/redis.go b/cache/redis/redis.go index 441ff43..4721bca 100644 --- a/cache/redis/redis.go +++ b/cache/redis/redis.go @@ -17,10 +17,9 @@ var RedisNil = fmt.Sprintf("redis: nil") type Client struct { client *redis.Client namespace string - wrapper string } -func NewClient(ctx context.Context, namespace string, wrapper string) (*Client, error) { +func NewClient(ctx context.Context, namespace string) (*Client, error) { fun := "NewClient -->" config, err := DefaultConfiger.GetConfig(ctx, namespace) @@ -45,14 +44,12 @@ func NewClient(ctx context.Context, namespace string, wrapper string) (*Client, return &Client{ client: client, namespace: namespace, - wrapper: wrapper, }, err } func (m *Client) fixKey(key string) string { return strings.Join([]string{ m.namespace, - m.wrapper, key, }, ".") } diff --git a/cache/redisext/redisext.go b/cache/redisext/redisext.go index 2ebfb7f..d61b289 100644 --- a/cache/redisext/redisext.go +++ b/cache/redisext/redisext.go @@ -54,7 +54,7 @@ func fromRedisZSlice(rzs []redis2.Z) (zs []Z) { } type ZRangeBy struct { - Min, Max string + Min, Max string Offset, Count int64 } @@ -83,7 +83,6 @@ func (m *RedisExt) getInstanceConf(ctx context.Context) *redis.InstanceConf { return &redis.InstanceConf{ Group: scontext.GetControlRouteGroupWithDefault(ctx, cache.DefaultRouteGroup), Namespace: m.namespace, - Wrapper: cache.WrapperTypeRedisExt, } } @@ -103,6 +102,14 @@ func (m *RedisExt) Set(ctx context.Context, key string, val interface{}, exp tim return } +func (m *RedisExt) Incr(ctx context.Context, key string, val interface{}) (n int64, err error) { + client, err := m.getRedisInstance(ctx) + if err == nil { + n, err = client.Incr(ctx, m.prefixKey(key)).Result() + } + return +} + func (m *RedisExt) SetNX(ctx context.Context, key string, val interface{}, exp time.Duration) (b bool, err error) { client, err := m.getRedisInstance(ctx) if err == nil { diff --git a/cache/redisext/redisext_test.go b/cache/redisext/redisext_test.go index 10980f7..2298e8b 100644 --- a/cache/redisext/redisext_test.go +++ b/cache/redisext/redisext_test.go @@ -2,7 +2,6 @@ package redisext import ( "context" - "github.com/shawnfeng/sutil/cache" "github.com/stretchr/testify/assert" "testing" "time" @@ -16,7 +15,6 @@ func TestRedisExt_ZAdd(t *testing.T) { ctx := context.Background() re := NewRedisExt("base/report", "test") - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) members := []Z{ {1, "one"}, @@ -40,7 +38,6 @@ func TestRedisExt_ZRange(t *testing.T) { ctx := context.Background() re := NewRedisExt("base/report", "test") - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) // prepare members := []Z{ @@ -83,7 +80,6 @@ func TestRedisExt_ZRange(t *testing.T) { func TestRedisExt_ZRank(t *testing.T) { ctx := context.Background() re := NewRedisExt("base/report", "test") - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) // prepare members := []Z{ @@ -122,7 +118,6 @@ func TestRedisExt_ZRank(t *testing.T) { func TestRedisExt_ZCount(t *testing.T) { ctx := context.Background() re := NewRedisExt("base/report", "test") - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) // prepare members := []Z{ @@ -149,7 +144,6 @@ func TestRedisExt_ZCount(t *testing.T) { func TestRedisExt_ZScore(t *testing.T) { ctx := context.Background() re := NewRedisExt("base/report", "test") - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) // prepare members := []Z{ @@ -184,7 +178,6 @@ func TestRedisExt_ZScore(t *testing.T) { func TestRedisExt_Expire(t *testing.T) { ctx := context.Background() re := NewRedisExt("base/report", "test") - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) // prepare members := []Z{ diff --git a/cache/value/value.go b/cache/value/value.go index 81f36eb..6da3cb1 100644 --- a/cache/value/value.go +++ b/cache/value/value.go @@ -44,7 +44,6 @@ func (m *Cache) getInstanceConf(ctx context.Context) *redis.InstanceConf { return &redis.InstanceConf{ Group: scontext.GetControlRouteGroupWithDefault(ctx, cache.DefaultRouteGroup), Namespace: m.namespace, - Wrapper: cache.WrapperTypeCache, } } diff --git a/cache/value/value_test.go b/cache/value/value_test.go index 7d3a3be..8bb4b15 100644 --- a/cache/value/value_test.go +++ b/cache/value/value_test.go @@ -2,7 +2,6 @@ package value import ( "context" - "github.com/shawnfeng/sutil/cache" "github.com/shawnfeng/sutil/trace" //"fmt" @@ -15,7 +14,7 @@ type Test struct { Id int64 } -func load(key interface{}) (value interface{}, err error) { +func load(ctx context.Context, key interface{}) (value interface{}, err error) { //return nil, fmt.Errorf("not found") return &Test{ @@ -28,7 +27,6 @@ func TestGet(t *testing.T) { _ = trace.InitDefaultTracer("cache.test") c := NewCache("base/report", "test", 60*time.Second, load) - _ = SetConfiger(ctx, cache.ConfigerTypeApollo) WatchUpdate(ctx) var test Test