升级 cmd 枚举值类型

This commit is contained in:
白茶清欢 2024-11-25 17:35:50 +08:00
parent a85ebc0203
commit 258311256f
5 changed files with 40 additions and 27 deletions

View File

@ -9,6 +9,7 @@ package abstract
import (
"context"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/redis/define"
"go.uber.org/zap"
@ -20,11 +21,11 @@ import (
//
// Date : 15:09 2024/6/18
type IRedisClient interface {
GetRealClient(instanceFlag string) *define.ClientInfo // 获取客户端连接
GetRealClientWithError(instanceFlag string) (*define.ClientInfo, error) // 获取带error的客户端连接
AddClient(instanceFlag string, instanceConfig *define.Config) error // 添加新的客户端连接
RemoveClient(instanceFlag string) // 移除一个客户端连接
SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) // 设置日志实例, 全局生效, 而非针对某一个实例
Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult // 执行任意命令
SetCommandWhiteList(command []string) // 设置命令的白名单,全局生效, 而非单独针对某一个实例
GetRealClient(instanceFlag string) *define.ClientInfo // 获取客户端连接
GetRealClientWithError(instanceFlag string) (*define.ClientInfo, error) // 获取带error的客户端连接
AddClient(instanceFlag string, instanceConfig *define.Config) error // 添加新的客户端连接
RemoveClient(instanceFlag string) // 移除一个客户端连接
SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) // 设置日志实例, 全局生效, 而非针对某一个实例
Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult // 执行任意命令
SetCommandWhiteList(command []consts.RedisCmd) // 设置命令的白名单,全局生效, 而非单独针对某一个实例
}

View File

@ -62,7 +62,7 @@ func (o *OwnClient) isAllowCommand(command string) bool {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:05 2024/6/19
func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command string, args ...any) *define.RedisResult {
func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command consts.RedisCmd, args ...any) *define.RedisResult {
if nil == ctx {
ctx = context.Background()
}
@ -99,7 +99,7 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command strin
zap.Int64("start_time", res.StartTime),
zap.Int64("finish_time", res.FinishTime),
zap.Int64("used_time", res.UsedTime),
zap.String("command", res.Command),
zap.String("command", res.Command.String()),
zap.String("arg_list", strings.Join(res.ArgList, " ")),
zap.String("execute_result", res.Result),
zap.Error(res.Err),
@ -145,11 +145,11 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command strin
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:02 2024/6/19
func (o *OwnClient) SetCommandWhiteList(commandList []string) {
func (o *OwnClient) SetCommandWhiteList(commandList []consts.RedisCmd) {
o.lock.Lock()
defer o.lock.Unlock()
for _, itemCommand := range commandList {
o.whiteCommandTable[strings.ToLower(strings.TrimSpace(itemCommand))] = true
o.whiteCommandTable[strings.ToLower(strings.TrimSpace(itemCommand.String()))] = true
}
}
@ -209,8 +209,8 @@ func (o *OwnClient) SetLogger(loggerInstance *zap.Logger, extraLogFieldList []st
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:22 2024/10/8
func (o *OwnClient) isWriteCommand(command string) bool {
return wrapperOperate.ArrayType([]string{
func (o *OwnClient) isWriteCommand(command consts.RedisCmd) bool {
return wrapperOperate.ArrayType([]consts.RedisCmd{
consts.RedisCommandDel,
consts.RedisCommandSet,
consts.RedisCommandLpush,
@ -218,7 +218,7 @@ func (o *OwnClient) isWriteCommand(command string) bool {
consts.RedisCommandMSet,
consts.RedisCommandPublish,
consts.RedisCommandPsubScribe,
}).Has(strings.ToUpper(command)) >= 0
}).Has(consts.RedisCmd(strings.ToUpper(command.String()))) >= 0
}
// newClient 获取客户端连接

View File

@ -7,18 +7,20 @@
// Date : 2024-06-19 10:49
package define
import "git.zhangdeman.cn/zhangdeman/consts"
// RedisResult redis名玲玲执行结果
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2024/6/19
type RedisResult struct {
InstanceFlag string `json:"instance_flag"` // 实例标识
StartTime int64 `json:"start_time"` // 开始执行时间, 单位 : ms
FinishTime int64 `json:"finish_time"` // 完成执行时间, 单位 : ms
UsedTime int64 `json:"used_time"` // 执行耗时, 单位 : ms
Result string `json:"result"` // 执行结果
Command string `json:"command"` // 执行的命令
ArgList []string `json:"arg_list"` // 参数列表
Err error `json:"-"` // 失败信息
InstanceFlag string `json:"instance_flag"` // 实例标识
StartTime int64 `json:"start_time"` // 开始执行时间, 单位 : ms
FinishTime int64 `json:"finish_time"` // 完成执行时间, 单位 : ms
UsedTime int64 `json:"used_time"` // 执行耗时, 单位 : ms
Result string `json:"result"` // 执行结果
Command consts.RedisCmd `json:"command"` // 执行的命令
ArgList []string `json:"arg_list"` // 参数列表
Err error `json:"-"` // 失败信息
}

10
go.mod
View File

@ -3,19 +3,19 @@ module git.zhangdeman.cn/zhangdeman/redis
go 1.22.4
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101709-d8400fb206b4
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125081557-cacc6b3cafc6
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125065949-2f87fe0cd90e
github.com/pkg/errors v0.9.1
github.com/redis/go-redis/v9 v9.6.1
github.com/redis/go-redis/v9 v9.7.0
go.uber.org/zap v1.27.0
gopkg.in/yaml.v3 v3.0.1
)
require (
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 // indirect
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 // indirect
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd // indirect
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125081853-2d10d261da4c // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect

10
go.sum
View File

@ -8,20 +8,28 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101340-1d415ef93cac h1:0V9ubW
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101340-1d415ef93cac/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101709-d8400fb206b4 h1:GUWgrIR7Gw1BIPzU6qUQgnB+7ZCE9nIwudAQTk7cyoU=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101709-d8400fb206b4/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125081557-cacc6b3cafc6 h1:f24T6CtVn8bF84fbXpWTzS47otE+U2IdBj3cZero+OQ=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241125081557-cacc6b3cafc6/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211 h1:I/wOsRpCSRkU9vo1u703slQsmK0wnNeZzsWQOGtIAG0=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20240311030808-e2a2e6a3c211/go.mod h1:SrtvrQRdzt+8KfYzvosH++gWxo2ShPTzR1m3VQ6uX7U=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4 h1:s6d4b6yY+NaK1AzoBD1pxqsuygEHQz0Oie86c45geDw=
git.zhangdeman.cn/zhangdeman/easymap v0.0.0-20241101082529-28a6c68e38a4/go.mod h1:V4Dfg1v/JVIZGEKCm6/aehs8hK+Xow1dkL1yiQymXlQ=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0 h1:gUDlQMuJ4xNfP2Abl1Msmpa3fASLWYkNlqDFF/6GN0Y=
git.zhangdeman.cn/zhangdeman/op_type v0.0.0-20240122104027-4928421213c0/go.mod h1:VHb9qmhaPDAQDcS6vUiDCamYjZ4R5lD1XtVsh55KsMI=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687 h1:uQcGqdzi4UdpZlp4f4FUPeBqoygP58pEKJkmN3ROsE0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240325080031-1f58204e8687/go.mod h1:gf7SW2TXATgux8pfdFedMkXWv2515OtIIM/5c4atkFw=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd h1:2Y37waOVCmVvx0Rp8VGEptE2/2JVMImtxB4dKKDk/3w=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20240618035451-8d48a6bd39dd/go.mod h1:6+7whkCmb4sJDIfH3HxNuXRveaM0gCCNWd2uXZqNtIE=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125081853-2d10d261da4c h1:BLcVowYti2mhYHZLFykocmbMJDMvFVg4lmXjDajhjD0=
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241125081853-2d10d261da4c/go.mod h1:+D6uPSljwHywjVY5WSBY4TRVMj26TN5f5cFGEYMldjs=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e h1:+PeWa2QdYBWnL32CfAAgy0dlaRCVNmYZDH4q+9w7Gfg=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240612083858-8d056baada2e/go.mod h1:US/pcq2vstE3iyxIHf53w8IeXKkZys7bj/ozLWkRYeE=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1 h1:LYw8NJeWcOiyrGjH9weyxnaMit94MlIngL+uskbLjtw=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20240924063449-ef80c6cb79d1/go.mod h1:+2qNxuRsfyfOvXk9HNwn+CmyPmmhhrQm/eIi1FDU1jw=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125065949-2f87fe0cd90e h1:/N5yXmOEH7N/h4S6nv/6os8sRenh95BXogTZ2RJI950=
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20241125065949-2f87fe0cd90e/go.mod h1:17TlhgwKTLQLIzYW/R6G39oN5FFPdsEEFDWniv+ovgA=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
@ -52,6 +60,8 @@ github.com/redis/go-redis/v9 v9.5.3 h1:fOAp1/uJG+ZtcITgZOfYFmTKPE7n4Vclj1wZFgRci
github.com/redis/go-redis/v9 v9.5.3/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/redis/go-redis/v9 v9.7.0 h1:HhLSs+B6O021gwzl+locl0zEDnyNkxMtf/Z3NNBMa9E=
github.com/redis/go-redis/v9 v9.7.0/go.mod h1:f6zhXITC7JUJIlPEiBOTXxJgPLdZcA93GewI7inzyWw=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=