feat: 优化redis日志实例的注入方式
This commit is contained in:
@@ -9,6 +9,7 @@ package abstract
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"git.zhangdeman.cn/zhangdeman/redis/define"
|
"git.zhangdeman.cn/zhangdeman/redis/define"
|
||||||
"github.com/go-redis/redismock/v9"
|
"github.com/go-redis/redismock/v9"
|
||||||
@@ -23,7 +24,7 @@ import (
|
|||||||
type IRedisClient interface {
|
type IRedisClient interface {
|
||||||
GetRealClient(instanceFlag string) *define.ClientInfo // 获取客户端连接
|
GetRealClient(instanceFlag string) *define.ClientInfo // 获取客户端连接
|
||||||
GetRealClientWithError(instanceFlag string) (*define.ClientInfo, error) // 获取带error的客户端连接
|
GetRealClientWithError(instanceFlag string) (*define.ClientInfo, error) // 获取带error的客户端连接
|
||||||
AddClient(instanceFlag string, instanceConfig *define.Config) error // 添加新的客户端连接
|
AddClient(instanceFlag string, logger *zap.Logger, instanceConfig *define.Config) error // 添加新的客户端连接
|
||||||
RemoveClient(instanceFlag string) // 移除一个客户端连接
|
RemoveClient(instanceFlag string) // 移除一个客户端连接
|
||||||
SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) // 设置日志实例, 全局生效, 而非针对某一个实例
|
SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) // 设置日志实例, 全局生效, 而非针对某一个实例
|
||||||
Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult // 执行任意命令
|
Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult // 执行任意命令
|
||||||
|
|||||||
39
client.go
39
client.go
@@ -34,6 +34,7 @@ func init() {
|
|||||||
lock: &sync.RWMutex{},
|
lock: &sync.RWMutex{},
|
||||||
instanceTable: make(map[string]*define.ClientInfo),
|
instanceTable: make(map[string]*define.ClientInfo),
|
||||||
whiteCommandTable: make(map[string]bool),
|
whiteCommandTable: make(map[string]bool),
|
||||||
|
logger: zap.NewNop(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,10 +65,6 @@ func (o *OwnClient) isAllowCommand(command string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Exec 执行命令
|
// Exec 执行命令
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 11:05 2024/6/19
|
|
||||||
func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult {
|
func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult {
|
||||||
if nil == ctx {
|
if nil == ctx {
|
||||||
ctx = context.Background()
|
ctx = context.Background()
|
||||||
@@ -97,10 +94,6 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command const
|
|||||||
defer func() {
|
defer func() {
|
||||||
res.FinishTime = time.Now().UnixMilli()
|
res.FinishTime = time.Now().UnixMilli()
|
||||||
res.UsedTime = res.FinishTime - res.StartTime
|
res.UsedTime = res.FinishTime - res.StartTime
|
||||||
if nil == o.logger {
|
|
||||||
// 未注入日志实例
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logDataList := []zap.Field{
|
logDataList := []zap.Field{
|
||||||
zap.Int64("start_time", res.StartTime),
|
zap.Int64("start_time", res.StartTime),
|
||||||
zap.Int64("finish_time", res.FinishTime),
|
zap.Int64("finish_time", res.FinishTime),
|
||||||
@@ -113,10 +106,25 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command const
|
|||||||
for _, item := range o.extraLogFieldList {
|
for _, item := range o.extraLogFieldList {
|
||||||
logDataList = append(logDataList, zap.Any(item, ctx.Value(item)))
|
logDataList = append(logDataList, zap.Any(item, ctx.Value(item)))
|
||||||
}
|
}
|
||||||
o.logger.Info(
|
isError := false
|
||||||
"Redis命令执行记录",
|
if res.Err != nil && res.Err != redisClient.Nil {
|
||||||
|
isError = true
|
||||||
|
}
|
||||||
|
loggerInstance := o.logger
|
||||||
|
if nil != instance && nil != instance.Logger {
|
||||||
|
loggerInstance = instance.Logger
|
||||||
|
}
|
||||||
|
if isError {
|
||||||
|
loggerInstance.Error(
|
||||||
|
"Redis 命令执行失败",
|
||||||
logDataList...,
|
logDataList...,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
loggerInstance.Info(
|
||||||
|
"Redis 命令执行成功",
|
||||||
|
logDataList...,
|
||||||
|
)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
if instance, res.Err = o.GetRealClientWithError(instanceFlag); nil != res.Err {
|
if instance, res.Err = o.GetRealClientWithError(instanceFlag); nil != res.Err {
|
||||||
return res
|
return res
|
||||||
@@ -147,10 +155,6 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SetCommandWhiteList 设置命令白名单, 空 或者 包含 * 则认为所有命令均允许执行
|
// SetCommandWhiteList 设置命令白名单, 空 或者 包含 * 则认为所有命令均允许执行
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 11:02 2024/6/19
|
|
||||||
func (o *OwnClient) SetCommandWhiteList(commandList []consts.RedisCmd) {
|
func (o *OwnClient) SetCommandWhiteList(commandList []consts.RedisCmd) {
|
||||||
o.lock.Lock()
|
o.lock.Lock()
|
||||||
defer o.lock.Unlock()
|
defer o.lock.Unlock()
|
||||||
@@ -175,13 +179,14 @@ func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*define.ClientI
|
|||||||
return instance, nil
|
return instance, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.Config) error {
|
func (o *OwnClient) AddClient(instanceFlag string, logger *zap.Logger, instanceConfig *define.Config) error {
|
||||||
if nil == instanceConfig.Master && !instanceConfig.ReadOnly {
|
if nil == instanceConfig.Master && !instanceConfig.ReadOnly {
|
||||||
// 不是只读, 则要求 主库配置 和 从库配置都要存在
|
// 不是只读, 则要求 主库配置 和 从库配置都要存在
|
||||||
return errors.New(instanceFlag + " : master config is nil")
|
return errors.New(instanceFlag + " : master config is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
clientInfo := &define.ClientInfo{
|
clientInfo := &define.ClientInfo{
|
||||||
|
Logger: logger,
|
||||||
ReadOnly: instanceConfig.ReadOnly,
|
ReadOnly: instanceConfig.ReadOnly,
|
||||||
Master: nil,
|
Master: nil,
|
||||||
Slave: nil,
|
Slave: nil,
|
||||||
@@ -235,10 +240,6 @@ func (o *OwnClient) GetMockInstance() redismock.ClientMock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newClient 获取客户端连接
|
// newClient 获取客户端连接
|
||||||
//
|
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
|
||||||
//
|
|
||||||
// Date : 16:12 2024/10/8
|
|
||||||
func (o *OwnClient) newClient(instanceConfig *define.Options) *redisClient.Client {
|
func (o *OwnClient) newClient(instanceConfig *define.Options) *redisClient.Client {
|
||||||
if o.mockMode {
|
if o.mockMode {
|
||||||
// mock模式下, 直接返回mock实例
|
// mock模式下, 直接返回mock实例
|
||||||
|
|||||||
@@ -7,7 +7,10 @@
|
|||||||
// Date : 2024-06-18 16:12
|
// Date : 2024-06-18 16:12
|
||||||
package define
|
package define
|
||||||
|
|
||||||
import redisClient "github.com/redis/go-redis/v9"
|
import (
|
||||||
|
redisClient "github.com/redis/go-redis/v9"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
)
|
||||||
|
|
||||||
type Options struct {
|
type Options struct {
|
||||||
DB int `json:"db" yaml:"db" ini:"db" toml:"db"` // 选择的数据库序号
|
DB int `json:"db" yaml:"db" ini:"db" toml:"db"` // 选择的数据库序号
|
||||||
@@ -61,6 +64,7 @@ type Config struct {
|
|||||||
//
|
//
|
||||||
// Date : 16:06 2024/10/8
|
// Date : 16:06 2024/10/8
|
||||||
type ClientInfo struct {
|
type ClientInfo struct {
|
||||||
|
Logger *zap.Logger // 日志实例
|
||||||
ReadOnly bool // 是否只读
|
ReadOnly bool // 是否只读
|
||||||
Master *redisClient.Client // 主库连接
|
Master *redisClient.Client // 主库连接
|
||||||
Slave *redisClient.Client // 从库连接
|
Slave *redisClient.Client // 从库连接
|
||||||
|
|||||||
Reference in New Issue
Block a user