优化配置
This commit is contained in:
parent
93b5615dcc
commit
efdbb35412
41
client.go
41
client.go
@ -10,6 +10,7 @@ package redis
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -30,7 +31,7 @@ func init() {
|
|||||||
Client = &OwnClient{
|
Client = &OwnClient{
|
||||||
lock: &sync.RWMutex{},
|
lock: &sync.RWMutex{},
|
||||||
instanceTable: make(map[string]*RealClient),
|
instanceTable: make(map[string]*RealClient),
|
||||||
confTable: make(map[string]Options),
|
confTable: make(map[string]*FullConfig),
|
||||||
parseErrorFunc: defaultParseError,
|
parseErrorFunc: defaultParseError,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,7 +84,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, parseErrorFunc func(err error) error) (ClientInterface, error) {
|
func NewClient(config map[string]*FullConfig, parseErrorFunc func(err error) error) (ClientInterface, error) {
|
||||||
c := &OwnClient{
|
c := &OwnClient{
|
||||||
instanceTable: make(map[string]*RealClient),
|
instanceTable: make(map[string]*RealClient),
|
||||||
loggerTable: make(map[string]*zap.Logger),
|
loggerTable: make(map[string]*zap.Logger),
|
||||||
@ -105,7 +106,7 @@ type OwnClient struct {
|
|||||||
lock *sync.RWMutex // 锁
|
lock *sync.RWMutex // 锁
|
||||||
loggerTable map[string]*zap.Logger // 日志实例
|
loggerTable map[string]*zap.Logger // 日志实例
|
||||||
instanceTable map[string]*RealClient // redis 实例
|
instanceTable map[string]*RealClient // redis 实例
|
||||||
confTable map[string]Options // redis 配置
|
confTable map[string]*FullConfig // redis 配置
|
||||||
parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis:<nil>
|
parseErrorFunc func(err error) error // 解析err的function,解析执行结果是否为失败,有的场景,执行成功,返回 redis:nil / redis:<nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,9 +121,37 @@ func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) {
|
|||||||
cfg FullConfig
|
cfg FullConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
|
filePathArr := strings.Split(cfgPath, string(filepath.Separator))
|
||||||
|
if len(filePathArr) == 0 {
|
||||||
|
return nil, CfgFilePathError()
|
||||||
|
}
|
||||||
|
fileName := strings.ToLower(filePathArr[len(filePathArr)-1])
|
||||||
|
fileArr := strings.Split(fileName, ".")
|
||||||
|
if len(filePathArr) < 2 {
|
||||||
|
return nil, CfgFileFormatErr("未知")
|
||||||
|
}
|
||||||
|
flag := strings.Trim(
|
||||||
|
strings.Trim(
|
||||||
|
strings.Trim(fileName, ".json"),
|
||||||
|
".yaml"), ".yml")
|
||||||
|
switch strings.ToLower(fileArr[len(fileArr)-1]) {
|
||||||
|
case "json":
|
||||||
if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err {
|
if err = util.File.ReadJSONContent(cfgPath, &cfg); nil != err {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
flag = strings.Join(fileArr[0:len(fileArr)-1], ".")
|
||||||
|
case "yml":
|
||||||
|
fallthrough
|
||||||
|
case "yaml":
|
||||||
|
if err = util.File.ReadYmlContent(cfgPath, &cfg); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
flag = strings.Join(fileArr[0:len(fileArr)-1], ".")
|
||||||
|
default:
|
||||||
|
return nil, CfgFileFormatErr(fileArr[len(fileArr)-1])
|
||||||
|
}
|
||||||
|
|
||||||
|
c.confTable[flag] = &cfg
|
||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,12 +162,12 @@ func (c *OwnClient) loadConfig(cfgPath string) (*FullConfig, error) {
|
|||||||
// Date : 5:31 下午 2021/2/27
|
// Date : 5:31 下午 2021/2/27
|
||||||
func (c *OwnClient) init() error {
|
func (c *OwnClient) init() error {
|
||||||
|
|
||||||
for flag, conf := range c.confTable {
|
for flag, _ := range c.confTable {
|
||||||
c.instanceTable[flag] = &RealClient{
|
c.instanceTable[flag] = &RealClient{
|
||||||
Flag: flag,
|
Flag: flag,
|
||||||
Instance: redisInstance.NewClient(conf.Conf),
|
//Instance: redisInstance.NewClient(conf.Conf),
|
||||||
Logger: nil,
|
Logger: nil,
|
||||||
LoggerFieldConfig: conf.LoggerFieldConfig,
|
//LoggerFieldConfig: conf.LoggerFieldConfig,
|
||||||
}
|
}
|
||||||
if nil == c.instanceTable[flag].LoggerFieldConfig {
|
if nil == c.instanceTable[flag].LoggerFieldConfig {
|
||||||
c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{
|
c.instanceTable[flag].LoggerFieldConfig = &LogFieldConfig{
|
||||||
|
Loading…
Reference in New Issue
Block a user