From c150262e50101beb3b89fd273575e02b5b383f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 15 Jun 2022 15:02:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E7=9A=84=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 90 ++++++++++++++++++++++++++++++------------------------- config.go | 4 ++- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/client.go b/client.go index c65c69a..3b85a57 100644 --- a/client.go +++ b/client.go @@ -11,13 +11,30 @@ import ( "encoding/json" "fmt" "strings" + "sync" "time" + "git.zhangdeman.cn/zhangdeman/util" + "git.zhangdeman.cn/zhangdeman/logger" redisInstance "github.com/go-redis/redis/v8" "go.uber.org/zap" ) +var ( + // Client 连接实例 + Client ClientInterface +) + +func init() { + Client = &OwnClient{ + lock: &sync.RWMutex{}, + instanceTable: make(map[string]*RealClient), + confTable: make(map[string]Options), + parseErrorFunc: defaultParseError, + } +} + // defaultParseError ... // // Author : go_developer@163.com<白茶清欢> @@ -67,8 +84,9 @@ type RealClient struct { // // Date : 5:05 下午 2021/2/27 func NewClient(config map[string]Options, parseErrorFunc func(err error) error) (ClientInterface, error) { - c := &Client{ + c := &OwnClient{ instanceTable: make(map[string]*RealClient), + loggerTable: make(map[string]*zap.Logger), confTable: config, parseErrorFunc: parseErrorFunc, } @@ -78,26 +96,42 @@ func NewClient(config map[string]Options, parseErrorFunc func(err error) error) return c, c.init() } -// Client 包装的redis client +// OwnClient 包装的redis client // // Author : go_developer@163.com<白茶清欢> // // Date : 4:52 下午 2021/2/27 -type Client struct { +type OwnClient struct { + lock *sync.RWMutex // 锁 + loggerTable map[string]*zap.Logger // 日志实例 instanceTable map[string]*RealClient // redis 实例 confTable map[string]Options // redis 配置 parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis: } +// loadConfig 载入配置文件 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:31 2022/6/15 +func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) { + var ( + err error + cfg FullConfig + ) + + if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err { + return nil, err + } + return &cfg, nil +} + // init 初始化redis连接 // // Author : go_developer@163.com<白茶清欢> // // Date : 5:31 下午 2021/2/27 -func (c *Client) init() error { - var ( - err error - ) +func (c *OwnClient) init() error { for flag, conf := range c.confTable { c.instanceTable[flag] = &RealClient{ @@ -106,9 +140,6 @@ func (c *Client) init() error { Logger: nil, LoggerFieldConfig: conf.LoggerFieldConfig, } - if c.instanceTable[flag].Logger, err = c.getLogger(conf.Logger); nil != err { - return LoggerInitFail(flag, err) - } if nil == c.instanceTable[flag].LoggerFieldConfig { c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{ Message: "", @@ -133,36 +164,15 @@ func (c *Client) init() error { return nil } -// getLogger ... +// initLogger 初始化日志 // // Author : go_developer@163.com<白茶清欢> // // Date : 7:07 下午 2021/2/27 -func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) { - if nil == conf || nil == conf.SplitConfig { - return nil, nil - } - optionFuncList := make([]logger.SetLoggerOptionFunc, 0) - if conf.ConsoleOutput { - optionFuncList = append(optionFuncList, logger.WithConsoleOutput()) - } - - if conf.Encoder != nil { - optionFuncList = append(optionFuncList, logger.WithEncoder(conf.Encoder)) - } - - splitConfigFuncList := []logger.SetRotateLogConfigFunc{ - logger.WithTimeIntervalType(conf.SplitConfig.TimeIntervalType), - logger.WithDivisionChar(conf.SplitConfig.DivisionChar), - logger.WithMaxAge(conf.SplitConfig.MaxAge), - } - - splitConfig, _ := logger.NewRotateLogConfig(conf.SplitConfig.LogPath, conf.SplitConfig.LogFileName, splitConfigFuncList...) - return logger.NewLogger( - conf.LoggerLevel, - splitConfig, - optionFuncList..., - ) +func (c *OwnClient) initLogger(flag string, conf *logger.InputLogConfig) error { + var err error + c.loggerTable[flag], err = logger.GetLogInstanceFromInputConfig(conf) + return LoggerInitFail(flag, err) } // GetRedisClient 获取redis实例 @@ -170,7 +180,7 @@ func (c *Client) getLogger(conf *LoggerConfig) (*zap.Logger, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 5:16 下午 2021/2/27 -func (c *Client) GetRedisClient(flag string) (*RealClient, error) { +func (c *OwnClient) GetRedisClient(flag string) (*RealClient, error) { redisClient, exist := c.instanceTable[flag] if !exist { return nil, FlagNotFound(flag) @@ -183,7 +193,7 @@ func (c *Client) GetRedisClient(flag string) (*RealClient, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 8:52 下午 2021/2/27 -func (c *Client) log(ctx *Context, realClient *RealClient, cmdResult redisInstance.Cmder, startTime int64, finishTime int64) { +func (c *OwnClient) log(ctx *Context, realClient *RealClient, cmdResult redisInstance.Cmder, startTime int64, finishTime int64) { if nil == realClient || nil == realClient.Logger { return } @@ -201,7 +211,7 @@ 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{}) (string, error) { +func (c *OwnClient) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (string, error) { var ( realClient *RealClient err error @@ -228,7 +238,7 @@ func (c *Client) CommandProxy(ctx *Context, flag string, cmd string, param ...in // Author : go_developer@163.com<白茶清欢> // // Date : 10:00 下午 2021/2/27 -func (c *Client) CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error { +func (c *OwnClient) CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error { if nil == receiver { return ReceiverISNIL() } diff --git a/config.go b/config.go index 3967e94..e024af3 100644 --- a/config.go +++ b/config.go @@ -7,7 +7,9 @@ // Date : 2022-06-15 11:52 package redis -import "git.zhangdeman.cn/zhangdeman/logger" +import ( + "git.zhangdeman.cn/zhangdeman/logger" +) // FullConfig 完整配置 //