From 017e4bc54063b7667d5463230c3a9f82ddfe4eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Sat, 27 Feb 2021 22:48:20 +0800 Subject: [PATCH] =?UTF-8?q?redis=20client=20=E5=A2=9E=E5=8A=A0=20=E5=8D=95?= =?UTF-8?q?=E6=B5=8B=20&&=20bug=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/redis/clent.go | 29 +++++++---------------- middleware/redis/context.go | 2 +- middleware/redis/redis_test.go | 43 ++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 21 deletions(-) create mode 100644 middleware/redis/redis_test.go diff --git a/middleware/redis/clent.go b/middleware/redis/clent.go index 71d1e43..a006d4d 100644 --- a/middleware/redis/clent.go +++ b/middleware/redis/clent.go @@ -41,7 +41,7 @@ type RealClient struct { // Author : go_developer@163.com<张德满> // // Date : 5:05 下午 2021/2/27 -func NewClient(config map[string]Options) (*Client, error) { +func NewClient(config map[string]Options) (ClientInterface, error) { c := &Client{ instanceTable: make(map[string]*RealClient), confTable: config, @@ -164,6 +164,10 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in if len(cmd) == 0 { return nil, EmptyCmd() } + + if nil == ctx { + ctx = NewContext(flag) + } if realClient, err = c.GetRedisClient(ctx.Flag); 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)) } -// Set set 命令 -// -// Author : go_developer@163.com<张德满> -// -// 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() +type ClientInterface interface { + GetRedisClient(flag string) (*RealClient, error) + CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (interface{}, error) + CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error } diff --git a/middleware/redis/context.go b/middleware/redis/context.go index 8ee47e4..d7fe806 100644 --- a/middleware/redis/context.go +++ b/middleware/redis/context.go @@ -71,7 +71,7 @@ func NewContext(flag string, of ...SetContextFunc) *Context { // requestID 填充 if len(ctx.RequestID) == 0 { // 先从 gin 读 - if nil != ctx.Ctx { + if nil != ctx.GinCtx { ctx.RequestID = ctx.GinCtx.GetString(ctx.RequestIDField) } // 再从extra读取 diff --git a/middleware/redis/redis_test.go b/middleware/redis/redis_test.go new file mode 100644 index 0000000..b82daff --- /dev/null +++ b/middleware/redis/redis_test.go @@ -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")) +}