mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
优化redis eval的key重命名
This commit is contained in:
@ -20,7 +20,7 @@ const (
|
|||||||
|
|
||||||
var Nil = redis.Nil
|
var Nil = redis.Nil
|
||||||
|
|
||||||
type ReidsOption struct {
|
type RedisOption struct {
|
||||||
Addr string
|
Addr string
|
||||||
Addrs []string
|
Addrs []string
|
||||||
Password string // 密码
|
Password string // 密码
|
||||||
@ -67,7 +67,7 @@ type redisClient struct {
|
|||||||
separator string
|
separator string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(opt *ReidsOption) redisClient {
|
func New(opt *RedisOption) redisClient {
|
||||||
var rdb redisClient
|
var rdb redisClient
|
||||||
|
|
||||||
if len(opt.Addrs) == 0 && len(opt.Addr) > 0 {
|
if len(opt.Addrs) == 0 && len(opt.Addr) > 0 {
|
||||||
@ -114,12 +114,6 @@ func New(opt *ReidsOption) redisClient {
|
|||||||
return rdb
|
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 {
|
func (rdb redisClient) Prefix() string {
|
||||||
return rdb.prefix
|
return rdb.prefix
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ func TestRedisPool(t *testing.T) {
|
|||||||
|
|
||||||
t.Log(client.ConfigGet(context.Background(), "slowlog-log-slower-than").Result())
|
t.Log(client.ConfigGet(context.Background(), "slowlog-log-slower-than").Result())
|
||||||
|
|
||||||
}, redis.ReidsOption{
|
}, redis.RedisOption{
|
||||||
Addr: "192.168.123.100:6379",
|
Addr: "192.168.123.100:6379",
|
||||||
PoolSize: 100,
|
PoolSize: 100,
|
||||||
PoolFIFO: true,
|
PoolFIFO: true,
|
||||||
|
@ -49,7 +49,7 @@ func (r renameKey) renameKey(cmd redis.Cmder) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch strings.ToUpper(cmd.Name()) {
|
switch strings.ToUpper(cmd.Name()) {
|
||||||
case "SELECT", "EVAL":
|
case "SELECT":
|
||||||
// 无KEY指令
|
// 无KEY指令
|
||||||
case
|
case
|
||||||
"RENAME", "RENAMENX",
|
"RENAME", "RENAMENX",
|
||||||
@ -69,6 +69,11 @@ func (r renameKey) renameKey(cmd redis.Cmder) {
|
|||||||
case "MSET", "MSETNX":
|
case "MSET", "MSETNX":
|
||||||
// 间隔KEY,KEY位置规则1,3,5,7
|
// 间隔KEY,KEY位置规则1,3,5,7
|
||||||
r.rename(args, createSepuence(1, len(args), 2)...)
|
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:
|
default:
|
||||||
// 默认第一个参数为键值
|
// 默认第一个参数为键值
|
||||||
r.rename(args, 1)
|
r.rename(args, 1)
|
||||||
|
@ -1,9 +1,24 @@
|
|||||||
package redis
|
package redis
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
func TestRename(t *testing.T) {
|
func TestRename(t *testing.T) {
|
||||||
New(&ReidsOption{
|
New(&RedisOption{
|
||||||
Addrs: []string{"192.168.123.100:6379"},
|
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"
|
"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 redis redis.Client
|
||||||
var clean func()
|
var clean func()
|
||||||
var err error
|
var err error
|
||||||
@ -22,7 +22,7 @@ func RunOnRedis(t assert.TestingT, fn func(rdb redis.Client), opt ...redis.Reids
|
|||||||
fn(redis)
|
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 {
|
if len(opt) > 0 {
|
||||||
return createRedisClient(opt[0])
|
return createRedisClient(opt[0])
|
||||||
} else {
|
} 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)
|
rdb := redis.New(&opt)
|
||||||
|
|
||||||
if err := rdb.Ping(context.Background()).Err(); err != nil {
|
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()
|
addr := mr.Addr()
|
||||||
log.Println("mini redis run at:", addr)
|
log.Println("mini redis run at:", addr)
|
||||||
|
|
||||||
rdb := redis.New(&redis.ReidsOption{
|
rdb := redis.New(&redis.RedisOption{
|
||||||
Addrs: []string{addr},
|
Addrs: []string{addr},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user