Compare commits
8 Commits
eec9374d48
...
master
Author | SHA1 | Date | |
---|---|---|---|
ef3a36f288 | |||
5d1ebcf505 | |||
ac633385b7 | |||
258311256f | |||
a85ebc0203 | |||
ec5f5f0161 | |||
2e05d47fb7 | |||
6f502a5bdd |
@ -9,9 +9,9 @@ package abstract
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/redis/define"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/go-redis/redismock/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@ -21,11 +21,13 @@ import (
|
||||
//
|
||||
// Date : 15:09 2024/6/18
|
||||
type IRedisClient interface {
|
||||
GetRealClient(instanceFlag string) *redis.Client // 获取客户端连接
|
||||
GetRealClientWithError(instanceFlag string) (*redis.Client, error) // 获取带error的客户端连接
|
||||
AddClient(instanceFlag string, instanceConfig *define.Options) 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) // 设置命令的白名单,全局生效, 而非单独针对某一个实例
|
||||
SetMockMode() // 设置mock模式, 这是单元测试专用的, 生产环境不要使用!!!!!!
|
||||
GetMockInstance() redismock.ClientMock // 数据mock实例
|
||||
}
|
||||
|
132
client.go
132
client.go
@ -9,6 +9,8 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"github.com/go-redis/redismock/v9"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@ -28,17 +30,20 @@ var (
|
||||
func init() {
|
||||
Client = &OwnClient{
|
||||
lock: &sync.RWMutex{},
|
||||
instanceTable: make(map[string]*redisClient.Client),
|
||||
instanceTable: make(map[string]*define.ClientInfo),
|
||||
whiteCommandTable: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
type OwnClient struct {
|
||||
lock *sync.RWMutex
|
||||
instanceTable map[string]*redisClient.Client
|
||||
instanceTable map[string]*define.ClientInfo
|
||||
whiteCommandTable map[string]bool
|
||||
logger *zap.Logger
|
||||
extraLogFieldList []string
|
||||
mockMode bool
|
||||
mockInstance redismock.ClientMock
|
||||
mockClient *redisClient.Client
|
||||
}
|
||||
|
||||
func (o *OwnClient) isAllowCommand(command string) bool {
|
||||
@ -61,16 +66,16 @@ 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()
|
||||
}
|
||||
var (
|
||||
instance *redisClient.Client
|
||||
instance *define.ClientInfo
|
||||
)
|
||||
|
||||
cmdParamList := []any{
|
||||
command,
|
||||
strings.ToLower(command.String()), // redis 命令不区分大小写, 统一使用小写
|
||||
}
|
||||
argStrList := make([]string, 0)
|
||||
for _, itemArg := range args {
|
||||
@ -98,7 +103,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),
|
||||
@ -117,7 +122,21 @@ func (o *OwnClient) Exec(ctx context.Context, instanceFlag string, command strin
|
||||
if nil == ctx {
|
||||
ctx = context.Background()
|
||||
}
|
||||
cmdRes := instance.Do(ctx, cmdParamList...)
|
||||
if instance.ReadOnly && o.isWriteCommand(command) {
|
||||
// 只读实例, 尝试执行写命令
|
||||
res.Err = errors.New(instanceFlag + " : instance is read only")
|
||||
return res
|
||||
}
|
||||
if instance.ReadOnly && o.isWriteCommand(command) && instance.MasterClient() == nil {
|
||||
// 写命令, 没有主库连接
|
||||
res.Err = errors.New(instanceFlag + " : instance master client is nil")
|
||||
return res
|
||||
}
|
||||
redisRealClient := instance.MasterClient()
|
||||
if !o.isWriteCommand(command) {
|
||||
redisRealClient = instance.SlaveClient()
|
||||
}
|
||||
cmdRes := redisRealClient.Do(ctx, cmdParamList...)
|
||||
if res.Err = cmdRes.Err(); nil != res.Err {
|
||||
return res
|
||||
}
|
||||
@ -130,21 +149,21 @@ 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
|
||||
}
|
||||
}
|
||||
|
||||
func (o *OwnClient) GetRealClient(instanceFlag string) *redisClient.Client {
|
||||
func (o *OwnClient) GetRealClient(instanceFlag string) *define.ClientInfo {
|
||||
o.lock.RLock()
|
||||
defer o.lock.RUnlock()
|
||||
return o.instanceTable[instanceFlag]
|
||||
}
|
||||
|
||||
func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.Client, error) {
|
||||
func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*define.ClientInfo, error) {
|
||||
o.lock.RLock()
|
||||
defer o.lock.RUnlock()
|
||||
instance, exist := o.instanceTable[instanceFlag]
|
||||
@ -154,8 +173,80 @@ func (o *OwnClient) GetRealClientWithError(instanceFlag string) (*redisClient.Cl
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.Options) error {
|
||||
instance := redisClient.NewClient(&redisClient.Options{
|
||||
func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.Config) error {
|
||||
if nil == instanceConfig.Master && !instanceConfig.ReadOnly {
|
||||
// 不是只读, 则要求 主库配置 和 从库配置都要存在
|
||||
return errors.New(instanceFlag + " : master config is nil")
|
||||
}
|
||||
|
||||
clientInfo := &define.ClientInfo{
|
||||
ReadOnly: instanceConfig.ReadOnly,
|
||||
Master: nil,
|
||||
Slave: nil,
|
||||
}
|
||||
|
||||
if nil != instanceConfig.Master {
|
||||
clientInfo.Master = o.newClient(instanceConfig.Master)
|
||||
}
|
||||
if nil != instanceConfig.Slave {
|
||||
clientInfo.Master = o.newClient(instanceConfig.Slave)
|
||||
}
|
||||
o.lock.Lock()
|
||||
defer o.lock.Unlock()
|
||||
o.instanceTable[instanceFlag] = clientInfo
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *OwnClient) RemoveClient(instanceFlag string) {
|
||||
o.lock.Lock()
|
||||
defer o.lock.Unlock()
|
||||
delete(o.instanceTable, instanceFlag)
|
||||
}
|
||||
|
||||
func (o *OwnClient) SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) {
|
||||
o.logger = loggerInstance
|
||||
o.extraLogFieldList = extraLogFieldList
|
||||
}
|
||||
|
||||
// isWriteCommand 判断是否写命令
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:22 2024/10/8
|
||||
func (o *OwnClient) isWriteCommand(command consts.RedisCmd) bool {
|
||||
return wrapperOperate.ArrayType([]consts.RedisCmd{
|
||||
consts.RedisCommandDel,
|
||||
consts.RedisCommandSet,
|
||||
consts.RedisCommandLpush,
|
||||
consts.RedisCommandRpush,
|
||||
consts.RedisCommandMSet,
|
||||
consts.RedisCommandPublish,
|
||||
consts.RedisCommandPsubScribe,
|
||||
}).Has(consts.RedisCmd(strings.ToUpper(command.String()))) >= 0
|
||||
}
|
||||
|
||||
// SetMockMode 启用mock, 非单元测试不要使用!!!!!!!!
|
||||
func (o *OwnClient) SetMockMode() {
|
||||
o.mockMode = true
|
||||
o.mockClient, o.mockInstance = redismock.NewClientMock()
|
||||
}
|
||||
|
||||
// GetMockInstance 获取mock实例
|
||||
func (o *OwnClient) GetMockInstance() redismock.ClientMock {
|
||||
return o.mockInstance
|
||||
}
|
||||
|
||||
// newClient 获取客户端连接
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:12 2024/10/8
|
||||
func (o *OwnClient) newClient(instanceConfig *define.Options) *redisClient.Client {
|
||||
if o.mockMode {
|
||||
// mock模式下, 直接返回mock实例
|
||||
return o.mockClient
|
||||
}
|
||||
return redisClient.NewClient(&redisClient.Options{
|
||||
DB: instanceConfig.DB,
|
||||
Addr: instanceConfig.Addr,
|
||||
ClientName: instanceConfig.ClientName,
|
||||
@ -187,19 +278,4 @@ func (o *OwnClient) AddClient(instanceFlag string, instanceConfig *define.Option
|
||||
// DisableIndentity: instanceConfig.DisableIdentity,
|
||||
// IdentitySuffix: instanceConfig.IdentitySuffix,
|
||||
})
|
||||
o.lock.Lock()
|
||||
defer o.lock.Unlock()
|
||||
o.instanceTable[instanceFlag] = instance
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *OwnClient) RemoveClient(instanceFlag string) {
|
||||
o.lock.Lock()
|
||||
defer o.lock.Unlock()
|
||||
delete(o.instanceTable, instanceFlag)
|
||||
}
|
||||
|
||||
func (o *OwnClient) SetLogger(loggerInstance *zap.Logger, extraLogFieldList []string) {
|
||||
o.logger = loggerInstance
|
||||
o.extraLogFieldList = extraLogFieldList
|
||||
}
|
||||
|
@ -7,6 +7,8 @@
|
||||
// Date : 2024-06-18 16:12
|
||||
package define
|
||||
|
||||
import redisClient "github.com/redis/go-redis/v9"
|
||||
|
||||
type Options struct {
|
||||
DB int `json:"db" yaml:"db" ini:"db" toml:"db"` // 选择的数据库序号
|
||||
Network string `json:"network" yaml:"network" ini:"network" toml:"network"` // 网络连接方式
|
||||
@ -41,3 +43,36 @@ type Options struct {
|
||||
|
||||
IdentitySuffix string `json:"identity_suffix" yaml:"identity_suffix" ini:"identity_suffix" toml:"identity_suffix"` // Add suffix to client name. Default is empty.
|
||||
}
|
||||
|
||||
// Config 数据库连接配置
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 15:59 2024/10/8
|
||||
type Config struct {
|
||||
ReadOnly bool `json:"readonly" yaml:"readonly" ini:"readonly" toml:"readonly"`
|
||||
Master *Options `json:"master" yaml:"master" ini:"master" toml:"master"` // 主库配置
|
||||
Slave *Options `json:"slave" yaml:"slave" ini:"slave" toml:"slave"` // 主库配置
|
||||
}
|
||||
|
||||
// ClientInfo 客户端连接信息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 16:06 2024/10/8
|
||||
type ClientInfo struct {
|
||||
ReadOnly bool // 是否只读
|
||||
Master *redisClient.Client // 主库连接
|
||||
Slave *redisClient.Client // 从库连接
|
||||
}
|
||||
|
||||
func (cf *ClientInfo) MasterClient() *redisClient.Client {
|
||||
return cf.Master
|
||||
}
|
||||
|
||||
func (cf *ClientInfo) SlaveClient() *redisClient.Client {
|
||||
if nil != cf.Slave {
|
||||
return cf.Slave
|
||||
}
|
||||
return cf.Master
|
||||
}
|
||||
|
@ -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:"-"` // 失败信息
|
||||
}
|
||||
|
17
go.mod
17
go.mod
@ -1,26 +1,29 @@
|
||||
module git.zhangdeman.cn/zhangdeman/redis
|
||||
|
||||
go 1.22.4
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.2
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420101447-0b570213d5c7
|
||||
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-20250321102712-1cbfbe959740
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/redis/go-redis/v9 v9.6.1
|
||||
github.com/redis/go-redis/v9 v9.7.3
|
||||
go.uber.org/zap v1.27.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 // indirect
|
||||
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
|
||||
github.com/BurntSushi/toml v1.4.0 // indirect
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd // indirect
|
||||
github.com/BurntSushi/toml v1.5.0 // indirect
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-redis/redismock/v9 v9.2.0 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mozillazg/go-pinyin v0.20.0 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
|
30
go.sum
30
go.sum
@ -2,22 +2,46 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7 h1:QR8vMX
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240612081722-31c64d4d4ce7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7 h1:tyCPCMK+68PZ0axZylQHitMVp1d5mzNr9/YqMHXqo+A=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240924065029-c865046cd9e7/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241008084126-0b1c661317ee h1:4nuaCr5GQcx4z9/xWeEnjmLVV6J0j+QT68+AUKI9dFc=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241008084126-0b1c661317ee/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241009101340-1d415ef93cac h1:0V9ubWn7VLmefcPuuUfmmioNhdWQ9FeTXpwGCwbb3hE=
|
||||
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/consts v0.0.0-20250420100501-55e6ac5d835d h1:kWfJiJcJCaN3xEby1fepTg8Vy3umi7L54K/18on2bfw=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420100501-55e6ac5d835d/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420101447-0b570213d5c7 h1:dmZ/mwRQ/AGlKXYWyk4Ci+KRiMcJwZii7nUCj2F2bpM=
|
||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250420101447-0b570213d5c7/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||
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/serialize v0.0.0-20241223084948-de2e49144fcd h1:q7GG14qgXKB4MEXQFOe7/UYebsqMfPaSX80TcPdOosI=
|
||||
git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20241223084948-de2e49144fcd/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=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740 h1:zPUoylfJTbc0EcxW+NEzOTBmoeFZ2I/rLFBnEzxb4Wk=
|
||||
git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20250321102712-1cbfbe959740/go.mod h1:1ct92dbVc49pmXusA/iGfcQUJzcYmJ+cjAhgc3sDv1I=
|
||||
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/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
|
||||
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
@ -34,6 +58,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-redis/redismock/v9 v9.2.0 h1:ZrMYQeKPECZPjOj5u9eyOjg8Nnb0BS9lkVIZ6IpsKLw=
|
||||
github.com/go-redis/redismock/v9 v9.2.0/go.mod h1:18KHfGDK4Y6c2R0H38EUGWAdc7ZQS9gfYxc94k7rWT0=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
|
||||
@ -46,6 +72,10 @@ 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/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
|
||||
github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA=
|
||||
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=
|
||||
|
34
wrapper.go
34
wrapper.go
@ -9,6 +9,7 @@ package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
"git.zhangdeman.cn/zhangdeman/redis/define"
|
||||
"strings"
|
||||
"time"
|
||||
@ -32,7 +33,16 @@ type wrapper struct {
|
||||
//
|
||||
// Date : 12:31 2024/6/21
|
||||
func (w *wrapper) Exist(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "EXISTS", key)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandExists, key)
|
||||
}
|
||||
|
||||
// TTL ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 18:14 2024/10/9
|
||||
func (w *wrapper) TTL(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandTTL, key)
|
||||
}
|
||||
|
||||
// Get Get命令
|
||||
@ -41,7 +51,7 @@ func (w *wrapper) Exist(ctx context.Context, instanceFlag string, key string) *d
|
||||
//
|
||||
// Date : 16:17 2024/6/19
|
||||
func (w *wrapper) Get(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "GET", key)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandGet, key)
|
||||
}
|
||||
|
||||
// Del 删除命令
|
||||
@ -50,7 +60,7 @@ func (w *wrapper) Get(ctx context.Context, instanceFlag string, key string) *def
|
||||
//
|
||||
// Date : 16:19 2024/6/19
|
||||
func (w *wrapper) Del(ctx context.Context, instanceFlag string, keyList ...string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "DEL", strings.Join(keyList, " "))
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandDel, strings.Join(keyList, " "))
|
||||
}
|
||||
|
||||
// SetEx 设置数据并且带有效期, 有效期单位 : s
|
||||
@ -65,9 +75,9 @@ func (w *wrapper) SetEx(ctx context.Context, instanceFlag string, key string, va
|
||||
ttl = ttl - now
|
||||
}
|
||||
if withLock {
|
||||
return Client.Exec(ctx, instanceFlag, "SET", key, value, "EX", ttl, "NX")
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandSet, key, value, "EX", ttl, "NX")
|
||||
}
|
||||
return Client.Exec(ctx, instanceFlag, "SET", key, value, "EX", ttl)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandSet, key, value, "EX", ttl)
|
||||
}
|
||||
|
||||
// LPop ...
|
||||
@ -76,7 +86,7 @@ func (w *wrapper) SetEx(ctx context.Context, instanceFlag string, key string, va
|
||||
//
|
||||
// Date : 16:29 2024/6/19
|
||||
func (w *wrapper) LPop(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "LPOP", key)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandLpop, key)
|
||||
}
|
||||
|
||||
// RPop ...
|
||||
@ -85,7 +95,7 @@ func (w *wrapper) LPop(ctx context.Context, instanceFlag string, key string) *de
|
||||
//
|
||||
// Date : 16:30 2024/6/19
|
||||
func (w *wrapper) RPop(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "RPOP", key)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandRpop, key)
|
||||
}
|
||||
|
||||
// LPush ...
|
||||
@ -94,7 +104,7 @@ func (w *wrapper) RPop(ctx context.Context, instanceFlag string, key string) *de
|
||||
//
|
||||
// Date : 16:31 2024/6/19
|
||||
func (w *wrapper) LPush(ctx context.Context, instanceFlag string, key string, value string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "LPUSH", key, value)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandLpush, key, value)
|
||||
}
|
||||
|
||||
// RPush ...
|
||||
@ -103,7 +113,7 @@ func (w *wrapper) LPush(ctx context.Context, instanceFlag string, key string, va
|
||||
//
|
||||
// Date : 16:31 2024/6/19
|
||||
func (w *wrapper) RPush(ctx context.Context, instanceFlag string, key string, value string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "RPUSH", key, value)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandRpush, key, value)
|
||||
}
|
||||
|
||||
// HGet ...
|
||||
@ -112,7 +122,7 @@ func (w *wrapper) RPush(ctx context.Context, instanceFlag string, key string, va
|
||||
//
|
||||
// Date : 16:33 2024/6/19
|
||||
func (w *wrapper) HGet(ctx context.Context, instanceFlag string, key string, field string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "HGET", key, field)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandHget, key, field)
|
||||
}
|
||||
|
||||
// HSet ...
|
||||
@ -121,7 +131,7 @@ func (w *wrapper) HGet(ctx context.Context, instanceFlag string, key string, fie
|
||||
//
|
||||
// Date : 16:33 2024/6/19
|
||||
func (w *wrapper) HSet(ctx context.Context, instanceFlag string, key string, field string, value string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "HSET", key, field, value)
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandHset, key, field, value)
|
||||
}
|
||||
|
||||
// HDel ...
|
||||
@ -130,5 +140,5 @@ func (w *wrapper) HSet(ctx context.Context, instanceFlag string, key string, fie
|
||||
//
|
||||
// Date : 16:37 2024/6/19
|
||||
func (w *wrapper) HDel(ctx context.Context, instanceFlag string, key string, fieldList ...string) *define.RedisResult {
|
||||
return Client.Exec(ctx, instanceFlag, "HSET", key, strings.Join(fieldList, " "))
|
||||
return Client.Exec(ctx, instanceFlag, consts.RedisCommandHdel, key, strings.Join(fieldList, " "))
|
||||
}
|
||||
|
Reference in New Issue
Block a user