Files
redis/wrapper.go

94 lines
3.1 KiB
Go

// Package redis ...
//
// Description : redis ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-06-19 16:15
package redis
import (
"context"
"strings"
"time"
"git.zhangdeman.cn/zhangdeman/consts"
"git.zhangdeman.cn/zhangdeman/redis/define"
)
var (
Wrapper = &wrapper{}
)
// wrapper 常用命令的包装
type wrapper struct {
}
// Exist 缓存Key是否存在
func (w *wrapper) Exist(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandExists, key)
}
// TTL ...
func (w *wrapper) TTL(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandTTL, key)
}
// Get Get命令
func (w *wrapper) Get(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandGet, key)
}
// Del 删除命令
func (w *wrapper) Del(ctx context.Context, instanceFlag string, keyList ...string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandDel, strings.Join(keyList, " "))
}
// SetEx 设置数据并且带有效期, 有效期单位 : s
func (w *wrapper) SetEx(ctx context.Context, instanceFlag string, key string, value any, ttl int64, withLock bool) *define.RedisResult {
now := time.Now().Unix()
if ttl > now {
// 传入过期时间大于当前时间, 说明是指定具体时间过期, 做一下处理
ttl = ttl - now
}
if withLock {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandSet, key, value, "EX", ttl, "NX")
}
return Client.Exec(ctx, instanceFlag, consts.RedisCommandSet, key, value, "EX", ttl)
}
// LPop ...
func (w *wrapper) LPop(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandLpop, key)
}
// RPop ...
func (w *wrapper) RPop(ctx context.Context, instanceFlag string, key string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandRpop, key)
}
// LPush ...
func (w *wrapper) LPush(ctx context.Context, instanceFlag string, key string, value string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandLpush, key, value)
}
// RPush ...
func (w *wrapper) RPush(ctx context.Context, instanceFlag string, key string, value string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandRpush, key, value)
}
// HGet ...
func (w *wrapper) HGet(ctx context.Context, instanceFlag string, key string, field string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandHget, key, field)
}
// HSet ...
func (w *wrapper) HSet(ctx context.Context, instanceFlag string, key string, field string, value string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandHset, key, field, value)
}
// HDel ...
func (w *wrapper) HDel(ctx context.Context, instanceFlag string, key string, fieldList ...string) *define.RedisResult {
return Client.Exec(ctx, instanceFlag, consts.RedisCommandHdel, key, strings.Join(fieldList, " "))
}