mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-17 16:12:42 +08:00
优化redis eval的key重命名
This commit is contained in:
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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},
|
||||
})
|
||||
|
||||
|
Reference in New Issue
Block a user