diff --git a/client.go b/client.go index 8a86bd6..a67fd95 100644 --- a/client.go +++ b/client.go @@ -188,6 +188,18 @@ func (c *OwnClient) AddClientWithCfgDir(cfgDir string) error { return c.init() } +// AddClientWithCfg 通过配置增加实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:45 2023/8/16 +func (c *OwnClient) AddClientWithCfg(flag string, cfg *FullConfig) error { + c.lock.Lock() + c.confTable[flag] = cfg + c.lock.Unlock() + return c.init() +} + // loadConfig 载入配置文件 // // Author : go_developer@163.com<白茶清欢> @@ -258,6 +270,10 @@ func (c *OwnClient) batchLoadConfig(cfgDir string) error { func (c *OwnClient) init() error { var err error for flag, conf := range c.confTable { + if _, exist := c.instanceTable[flag]; exist { + // 实例已经初始化 + continue + } c.instanceTable[flag] = &RealClient{ Flag: flag, Master: redisInstance.NewClient(Config2Options(conf.Master)), @@ -281,12 +297,14 @@ func (c *OwnClient) initLogger(flag string, conf *logger.InputLogConfig) error { return LoggerInitFail(flag, err) } -// GetRedisClient 获取redis实例 +// GetRedisClientWithError 获取redis实例 // // Author : go_developer@163.com<白茶清欢> // // Date : 5:16 下午 2021/2/27 -func (c *OwnClient) GetRedisClient(flag string) (*RealClient, error) { +func (c *OwnClient) GetRedisClientWithError(flag string) (*RealClient, error) { + c.lock.RLock() + defer c.lock.RUnlock() redisClient, exist := c.instanceTable[flag] if !exist { return nil, FlagNotFound(flag) @@ -294,6 +312,16 @@ func (c *OwnClient) GetRedisClient(flag string) (*RealClient, error) { return redisClient, nil } +// GetRedisClient ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:52 2023/8/16 +func (c *OwnClient) GetRedisClient(flag string) *RealClient { + redisClient, _ := c.GetRedisClientWithError(flag) + return redisClient +} + // log 记录redis请求日志 // // Author : go_developer@163.com<白茶清欢> @@ -333,7 +361,7 @@ func (c *OwnClient) CommandProxy(ctx *Context, flag string, cmd string, param .. if nil == ctx { ctx = NewContext(flag) } - if realClient, err = c.GetRedisClient(ctx.Flag); nil != err { + if realClient, err = c.GetRedisClientWithError(ctx.Flag); nil != err { return "", err } flagArr := strings.Split(flag, "#") @@ -373,13 +401,38 @@ func (c *OwnClient) CommandProxyWithReceiver(ctx *Context, flag string, receiver return ResultConvertFail(json.Unmarshal([]byte(result), receiver)) } +// RemoveClient 移除client +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:49 2023/8/16 +func (c *OwnClient) RemoveClient(flag string) { + c.lock.Lock() + defer c.lock.Unlock() + delete(c.confTable, flag) + if _, exist := c.instanceTable[flag]; exist { + if nil != c.instanceTable[flag].Master { + _ = c.instanceTable[flag].Master.Close() + } + if nil != c.instanceTable[flag].Slave { + _ = c.instanceTable[flag].Slave.Close() + } + } + delete(c.instanceTable, flag) +} + // ClientInterface 定义redis client的接口实现,方便单元测试数据mock // // Author : go_developer@163.com<白茶清欢> // // Date : 10:49 下午 2021/2/27 type ClientInterface interface { - GetRedisClient(flag string) (*RealClient, error) + GetRedisClient(flag string) *RealClient + GetRedisClientWithError(flag string) (*RealClient, error) CommandProxy(ctx *Context, flag string, cmd string, param ...interface{}) (string, error) CommandProxyWithReceiver(ctx *Context, flag string, receiver interface{}, cmd string, param ...interface{}) error + AddClientWithCfgFile(cfgPath string) error + AddClientWithCfgDir(cfgDir string) error + AddClientWithCfg(flag string, cfg *FullConfig) error + RemoveClient(flag string) }