Skip to content

Commit

Permalink
Merge pull request #115 from ZhengHe-MD/dev/zhenghe3119/remove-cache-…
Browse files Browse the repository at this point in the history
…wrapper

cache: remove wrapper, add incr to redisext
  • Loading branch information
ZhengHe-MD authored Nov 7, 2019
2 parents d629e71 + 118023a commit 7be605d
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 28 deletions.
6 changes: 0 additions & 6 deletions cache/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,3 @@ func (c ConfigerType) String() string {
}

const DefaultRouteGroup = "default"

// NOTE: 减小 key 所占空间
const (
WrapperTypeCache = "c"
WrapperTypeRedisExt = "e"
)
7 changes: 2 additions & 5 deletions cache/redis/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
Expand All @@ -58,7 +56,6 @@ func (m *InstanceManager) buildKey(conf *InstanceConf) string {
return strings.Join([]string{
conf.Group,
conf.Namespace,
conf.Wrapper,
}, keySep)
}

Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
}, ".")
}
Expand Down
11 changes: 9 additions & 2 deletions cache/redisext/redisext.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func fromRedisZSlice(rzs []redis2.Z) (zs []Z) {
}

type ZRangeBy struct {
Min, Max string
Min, Max string
Offset, Count int64
}

Expand Down Expand Up @@ -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,
}
}

Expand All @@ -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 {
Expand Down
7 changes: 0 additions & 7 deletions cache/redisext/redisext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package redisext

import (
"context"
"github.com/shawnfeng/sutil/cache"
"github.com/stretchr/testify/assert"
"testing"
"time"
Expand All @@ -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"},
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
1 change: 0 additions & 1 deletion cache/value/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
4 changes: 1 addition & 3 deletions cache/value/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package value

import (
"context"
"github.com/shawnfeng/sutil/cache"
"github.com/shawnfeng/sutil/trace"

//"fmt"
Expand All @@ -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{
Expand All @@ -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
Expand Down

0 comments on commit 7be605d

Please sign in to comment.