From d1c269ed90492a51cbcc502d6f0ecce17929061a Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Fri, 13 Oct 2023 10:52:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/cache_builder.go | 11 +++-------- cache/cache_test.go | 12 ++++++++++-- cache/readme.md | 5 ++++- cache/redis.go | 35 ++++------------------------------- redis/redis_test.go | 1 + redis/rename_hook.go | 3 +++ redis/rename_hook_test.go | 9 +++++++++ tests/redis.go | 14 ++++++++++++++ 8 files changed, 48 insertions(+), 42 deletions(-) create mode 100644 redis/rename_hook_test.go diff --git a/cache/cache_builder.go b/cache/cache_builder.go index e7344ea..0163ffe 100644 --- a/cache/cache_builder.go +++ b/cache/cache_builder.go @@ -7,25 +7,20 @@ import ( "github.com/charlienet/go-mixed/cache/bigcache" "github.com/charlienet/go-mixed/cache/freecache" "github.com/charlienet/go-mixed/logx" + "github.com/charlienet/go-mixed/redis" ) const defaultPrefix = "cache" - type option func(*Cache) error type options struct { Prefix string } -func WithRedis(opts RedisConfig) option { +func WithRedis(rdb redis.Client) option { return func(c *Cache) error { - if len(opts.Prefix) == 0 { - opts.Prefix = defaultPrefix - } - - rds := NewRedis(opts) - + rds := NewRedis(rdb) c.rds = rds ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) diff --git a/cache/cache_test.go b/cache/cache_test.go index 8745eac..dd919be 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -6,6 +6,8 @@ import ( "sync/atomic" "testing" "time" + + "github.com/charlienet/go-mixed/redis" ) var ( @@ -60,7 +62,9 @@ func TestMemCache(t *testing.T) { } func TestDistributedCache(t *testing.T) { - c := NewRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456", Prefix: "abcdef"}) + c := NewRedis(redis.New(&redis.ReidsOption{ + Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456", Prefix: "abcdef", + })) ctx := context.Background() if err := c.Ping(ctx); err != nil { @@ -134,7 +138,11 @@ func load() (any, error) { func buildCache() *Cache { c, err := New( WithFreeCache(10*1024*1024), - WithRedis(RedisConfig{Addrs: []string{"192.168.2.222:6379"}, DB: 6, Password: "123456"})) + WithRedis(redis.New(&redis.ReidsOption{ + Addrs: []string{"192.168.2.222:6379"}, + DB: 6, + Password: "123456", + }))) if err != nil { panic(err) diff --git a/cache/readme.md b/cache/readme.md index c50e1e2..90d157d 100644 --- a/cache/readme.md +++ b/cache/readme.md @@ -1,4 +1,7 @@ -# 多级缓存模块 +# 二级缓存模块 + +提供本地缓存和分布式缓存组合的缓存模块,可以只使用本地缓存或分布式缓存。并可全局禁用缓存。 + 1. 一级缓存可使用freecache或bigcache作为本地缓存,当数据在本地缓存不存在时,会向二级缓存请求数据 2. 二级缓存使用redis作为缓存模块,当数据在二级缓存不存在时向资源请求数据。 diff --git a/cache/redis.go b/cache/redis.go index 2b068cf..fc3445e 100644 --- a/cache/redis.go +++ b/cache/redis.go @@ -9,48 +9,21 @@ import ( "github.com/charlienet/go-mixed/bytesconv" "github.com/charlienet/go-mixed/json" "github.com/charlienet/go-mixed/rand" - "github.com/go-redis/redis/v8" + "github.com/charlienet/go-mixed/redis" ) const redisEmptyObject = "redis object not exist" -type RedisConfig struct { - Prefix string // key perfix - Addrs []string - - // Database to be selected after connecting to the server. - // Only single-node and failover clients. - DB int - - Username string - Password string - MaxRetries int - MinRetryBackoff time.Duration - MaxRetryBackoff time.Duration - - DialTimeout time.Duration - ReadTimeout time.Duration - WriteTimeout time.Duration -} - type redisClient struct { - client redis.UniversalClient + client redis.Client emptyStamp string // 空对象标识,每个实例隔离 prefix string // 缓存键前缀 } -func NewRedis(c RedisConfig) *redisClient { - client := redis.NewUniversalClient(&redis.UniversalOptions{ - Addrs: c.Addrs, - DB: c.DB, - Username: c.Username, - Password: c.Password, - }) - +func NewRedis(c redis.Client) *redisClient { return &redisClient{ emptyStamp: fmt.Sprintf("redis-empty-%d-%s", time.Now().Unix(), rand.Hex.Generate(6)), - prefix: c.Prefix, - client: client, + client: c, } } diff --git a/redis/redis_test.go b/redis/redis_test.go index d7865a8..54c99ec 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -132,3 +132,4 @@ func TestPubSub(t *testing.T) { t.Logf("total received %d message", total) }) } + diff --git a/redis/rename_hook.go b/redis/rename_hook.go index 383add1..38afb28 100644 --- a/redis/rename_hook.go +++ b/redis/rename_hook.go @@ -60,6 +60,9 @@ func (r renameKey) renameKey(cmd redis.Cmder) { case "RENAME", "RENAMENX", "MGET", "BLPOP", "BRPOP", "RPOPLPUSH", "SDIFFSTORE", "SINTER", "SINTERSTORE", "SUNIONSTORE": // 连续KEY r.rename(args, createSepuence(1, len(args), 1)...) + case "sssss": + // 除最后一个外连续键 + r.rename(args, createSepuence(1, len(args)-1, 1)...) case "MSET", "MSETNX": // 间隔KEY,KEY位置规则1,3,5,7 r.rename(args, createSepuence(1, len(args), 2)...) diff --git a/redis/rename_hook_test.go b/redis/rename_hook_test.go new file mode 100644 index 0000000..6d75a54 --- /dev/null +++ b/redis/rename_hook_test.go @@ -0,0 +1,9 @@ +package redis + +import "testing" + +func TestRename(t *testing.T) { + New(&ReidsOption{ + Addrs: []string{"192.168.123.100:6379"}, + }) +} diff --git a/tests/redis.go b/tests/redis.go index db8dd95..26072c1 100644 --- a/tests/redis.go +++ b/tests/redis.go @@ -1,6 +1,7 @@ package tests import ( + "context" "log" "testing" "time" @@ -10,6 +11,19 @@ import ( "github.com/stretchr/testify/assert" ) +func RunOnSpecifiedRedis(t *testing.T, fn func(client redis.Client), addr ...string) { + rdb := redis.New(&redis.ReidsOption{ + Addrs: addr, + }) + defer rdb.Close() + + if err := rdb.Ping(context.Background()).Err(); err != nil { + t.Fatal(err) + } + + fn(rdb) +} + func RunOnRedis(t *testing.T, fn func(client redis.Client)) { redis, clean, err := createMiniRedis() assert.Nil(t, err)