From 5a17236fd7ddcef10439fbec7a8474183c9584e1 Mon Sep 17 00:00:00 2001 From: charlie <3140647@qq.com> Date: Thu, 26 Oct 2023 15:47:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96redis=20eval=E7=9A=84key?= =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis/redis.go | 10 ++-------- redis/redis_test.go | 2 +- redis/rename_hook.go | 7 ++++++- redis/rename_hook_test.go | 19 +++++++++++++++++-- tests/redis.go | 8 ++++---- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/redis/redis.go b/redis/redis.go index 9cefccc..709f8a9 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -20,7 +20,7 @@ const ( var Nil = redis.Nil -type ReidsOption struct { +type RedisOption struct { Addr string Addrs []string Password string // 密码 @@ -67,7 +67,7 @@ type redisClient struct { separator string } -func New(opt *ReidsOption) redisClient { +func New(opt *RedisOption) redisClient { var rdb redisClient if len(opt.Addrs) == 0 && len(opt.Addr) > 0 { @@ -114,12 +114,6 @@ func New(opt *ReidsOption) redisClient { return rdb } -func (rdb redisClient) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd { - newKeys := rdb.FormatKeys(keys...) - - return rdb.UniversalClient.Eval(ctx, script, newKeys, args...) -} - func (rdb redisClient) Prefix() string { return rdb.prefix } diff --git a/redis/redis_test.go b/redis/redis_test.go index 44adbc0..4243a6c 100644 --- a/redis/redis_test.go +++ b/redis/redis_test.go @@ -143,7 +143,7 @@ func TestRedisPool(t *testing.T) { t.Log(client.ConfigGet(context.Background(), "slowlog-log-slower-than").Result()) - }, redis.ReidsOption{ + }, redis.RedisOption{ Addr: "192.168.123.100:6379", PoolSize: 100, PoolFIFO: true, diff --git a/redis/rename_hook.go b/redis/rename_hook.go index ca8ad2b..fdc3a36 100644 --- a/redis/rename_hook.go +++ b/redis/rename_hook.go @@ -49,7 +49,7 @@ func (r renameKey) renameKey(cmd redis.Cmder) { } switch strings.ToUpper(cmd.Name()) { - case "SELECT", "EVAL": + case "SELECT": // 无KEY指令 case "RENAME", "RENAMENX", @@ -69,6 +69,11 @@ func (r renameKey) renameKey(cmd redis.Cmder) { case "MSET", "MSETNX": // 间隔KEY,KEY位置规则1,3,5,7 r.rename(args, createSepuence(1, len(args), 2)...) + case "EVAL": + // 命令中包含键数量 EVAL script numkeys [key [key ...]] [arg [arg ...]] + if n, ok := args[2].(int); ok && n > 0 { + r.rename(args, createSepuence(3, 3+n, 1)...) + } default: // 默认第一个参数为键值 r.rename(args, 1) diff --git a/redis/rename_hook_test.go b/redis/rename_hook_test.go index 6d75a54..c386c51 100644 --- a/redis/rename_hook_test.go +++ b/redis/rename_hook_test.go @@ -1,9 +1,24 @@ package redis -import "testing" +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" +) func TestRename(t *testing.T) { - New(&ReidsOption{ + New(&RedisOption{ Addrs: []string{"192.168.123.100:6379"}, }) } + +func TestEvalName(t *testing.T) { + rdb := New(&RedisOption{ + Addrs: []string{"192.168.123.100:6379"}, + Prefix: "aabbcc", + }) + + _, err := rdb.Eval(context.Background(), "return 1", []string{"a1", "a2", "a3"}, "b1", "b2", "b3").Result() + assert.Nil(t, err, err) +} diff --git a/tests/redis.go b/tests/redis.go index c3b0468..f40f326 100644 --- a/tests/redis.go +++ b/tests/redis.go @@ -10,7 +10,7 @@ import ( "github.com/stretchr/testify/assert" ) -func RunOnRedis(t assert.TestingT, fn func(rdb redis.Client), opt ...redis.ReidsOption) { +func RunOnRedis(t assert.TestingT, fn func(rdb redis.Client), opt ...redis.RedisOption) { var redis redis.Client var clean func() var err error @@ -22,7 +22,7 @@ func RunOnRedis(t assert.TestingT, fn func(rdb redis.Client), opt ...redis.Reids fn(redis) } -func CreateRedis(opt ...redis.ReidsOption) (r redis.Client, clean func(), err error) { +func CreateRedis(opt ...redis.RedisOption) (r redis.Client, clean func(), err error) { if len(opt) > 0 { return createRedisClient(opt[0]) } else { @@ -30,7 +30,7 @@ func CreateRedis(opt ...redis.ReidsOption) (r redis.Client, clean func(), err er } } -func createRedisClient(opt redis.ReidsOption) (r redis.Client, clean func(), err error) { +func createRedisClient(opt redis.RedisOption) (r redis.Client, clean func(), err error) { rdb := redis.New(&opt) if err := rdb.Ping(context.Background()).Err(); err != nil { @@ -49,7 +49,7 @@ func createMiniRedis() (r redis.Client, clean func(), err error) { addr := mr.Addr() log.Println("mini redis run at:", addr) - rdb := redis.New(&redis.ReidsOption{ + rdb := redis.New(&redis.RedisOption{ Addrs: []string{addr}, })