update redis client

This commit is contained in:
白茶清欢 2021-08-15 20:57:35 +08:00
parent c7e65cd925
commit b0d67ca05a

View File

@ -8,11 +8,10 @@
package redis
import (
"encoding/json"
"strings"
"time"
"git.zhangdeman.cn/zhangdeman/gopkg/convert"
"git.zhangdeman.cn/zhangdeman/gopkg/logger"
redisInstance "github.com/go-redis/redis/v8"
"go.uber.org/zap"
@ -201,26 +200,26 @@ func (c *Client) log(ctx *Context, realClient *RealClient, cmdResult redisInstan
// Author : go_developer@163.com<白茶清欢>
//
// Date : 9:41 下午 2021/2/27
func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error) {
func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (string, error) {
var (
realClient *RealClient
err error
)
if len(cmd) == 0 {
return nil, EmptyCmd()
return "", EmptyCmd()
}
if nil == ctx {
ctx = NewContext(flag)
}
if realClient, err = c.GetRedisClient(ctx.Flag); nil != err {
return nil, err
return "", err
}
redisCmd := append([]interface{}{cmd}, param...)
startTime := time.Now().Unix()
cmdResult := realClient.Instance.Do(ctx.Ctx, redisCmd...)
go c.log(ctx, realClient, cmdResult, startTime, time.Now().UnixNano())
return cmdResult.Val(), c.parseErrorFunc(cmdResult.Err())
return cmdResult.String(), c.parseErrorFunc(cmdResult.Err())
}
// CommandProxyWithReceiver 执行命令,并解析结果
@ -234,14 +233,14 @@ func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver in
}
var (
err error
result interface{}
result string
)
if result, err = c.CommandProxy(ctx, flag, cmd, param...); nil != err {
return err
}
return ResultConvertFail(convert.ConvertAssign(receiver, result))
return ResultConvertFail(json.Unmarshal([]byte(result), receiver))
}
// ClientInterface 定义redis client的接口实现,方便单元测试数据mock
@ -251,6 +250,6 @@ func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver in
// Date : 10:49 下午 2021/2/27
type ClientInterface interface {
GetRedisClient(flag string) (*RealClient, error)
CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error)
CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (string, error)
CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error
}