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