redis client 增加 单测 && bug fix

This commit is contained in:
白茶清欢 2021-02-27 22:48:20 +08:00
parent 3411acc73d
commit 017e4bc540
3 changed files with 53 additions and 21 deletions

View File

@ -41,7 +41,7 @@ type RealClient struct {
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 5:05 下午 2021/2/27 // Date : 5:05 下午 2021/2/27
func NewClient(config map[string]Options) (*Client, error) { func NewClient(config map[string]Options) (ClientInterface, error) {
c := &Client{ c := &Client{
instanceTable: make(map[string]*RealClient), instanceTable: make(map[string]*RealClient),
confTable: config, confTable: config,
@ -164,6 +164,10 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in
if len(cmd) == 0 { if len(cmd) == 0 {
return nil, EmptyCmd() return nil, EmptyCmd()
} }
if nil == ctx {
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 nil, err
} }
@ -195,23 +199,8 @@ func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver in
return ResultConvertFail(convert.ConvertAssign(receiver, result)) return ResultConvertFail(convert.ConvertAssign(receiver, result))
} }
// Set set 命令 type ClientInterface interface {
// GetRedisClient(flag string) (*RealClient, error)
// Author : go_developer@163.com<张德满> CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error)
// CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error
// Date : 8:18 下午 2021/2/27
func (c *Client) Set(ctx *Context, key string, value interface{}, expiration time.Duration) error {
var (
realClient *RealClient
err error
statusCmd *redisInstance.StatusCmd
)
if realClient, err = c.GetRedisClient(ctx.Flag); nil != err {
return err
}
startTime := time.Now().UnixNano()
statusCmd = realClient.Instance.Set(ctx.Ctx, key, value, expiration)
go c.log(ctx, realClient, statusCmd, startTime, time.Now().UnixNano())
return statusCmd.Err()
} }

View File

@ -71,7 +71,7 @@ func NewContext(flag string, of ...SetContextFunc) *Context {
// requestID 填充 // requestID 填充
if len(ctx.RequestID) == 0 { if len(ctx.RequestID) == 0 {
// 先从 gin 读 // 先从 gin 读
if nil != ctx.Ctx { if nil != ctx.GinCtx {
ctx.RequestID = ctx.GinCtx.GetString(ctx.RequestIDField) ctx.RequestID = ctx.GinCtx.GetString(ctx.RequestIDField)
} }
// 再从extra读取 // 再从extra读取

View File

@ -0,0 +1,43 @@
// Package redis...
//
// Description : redis...
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-02-27 10:14 下午
package redis
import (
"fmt"
"testing"
redisInstance "github.com/go-redis/redis/v8"
)
// TestCommandProxy ...
//
// Author : go_developer@163.com<张德满>
//
// Date : 10:22 下午 2021/2/27
func TestCommandProxy(t *testing.T) {
instance, err := NewClient(map[string]Options{
"test_redis": Options{
Conf: &redisInstance.Options{
Addr: "127.0.0.1:6379",
},
Logger: &LoggerConfig{
LoggerPath: "/tmp/test-log",
LoggerFile: "test-pkg-redis-client.log",
LoggerLevel: 0,
ConsoleOutput: true,
Encoder: nil,
SplitConfig: nil,
},
LoggerFieldConfig: nil,
},
})
if nil != err {
panic(err.Error())
}
fmt.Println(instance.CommandProxy(nil, "test_redis", "set", "command_proxy", "hello world"))
}