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")) +}