main
lizifeng 2023-05-17 19:14:10 +08:00
parent 49b5ec8cd8
commit c9e3d8f858
1 changed files with 9 additions and 6 deletions

View File

@ -27,7 +27,6 @@ var (
type (
MyRedis struct {
*redis.Client
ctx context.Context
}
Config struct {
@ -105,7 +104,6 @@ func New(key string, config *Config) (*MyRedis, error) {
ctx := context.Background()
rd := &MyRedis{}
rd.ctx = ctx
rd.Client = client
ping := rd.Client.Ping(ctx)
if ping.Err() != nil {
@ -118,21 +116,24 @@ func New(key string, config *Config) (*MyRedis, error) {
// GetSimple 通用get
func (r *MyRedis) GetSimple(key string) (string, error) {
return r.Client.Get(r.ctx, key).Result()
ctx := context.Background()
return r.Client.Get(ctx, key).Result()
}
// SetSimple 通用set
func (r *MyRedis) SetSimple(key string, value interface{}, t ...time.Duration) (string, error) {
ctx := context.Background()
var t2 time.Duration
if len(t) > 0 {
t2 = t[0]
}
return r.Client.Set(r.ctx, key, value, t2).Result()
return r.Client.Set(ctx, key, value, t2).Result()
}
// GetJson json序列化
func (r *MyRedis) GetJson(key string) (interface{}, error) {
res := r.Client.Get(r.ctx, key)
ctx := context.Background()
res := r.Client.Get(ctx, key)
if res.Err() != nil {
return nil, res.Err()
}
@ -151,6 +152,8 @@ func (r *MyRedis) GetJson(key string) (interface{}, error) {
// SetJson json序列化set
func (r *MyRedis) SetJson(key string, value interface{}, t ...time.Duration) (string, error) {
ctx := context.Background()
var t2 time.Duration
if len(t) > 0 {
t2 = t[0]
@ -159,7 +162,7 @@ func (r *MyRedis) SetJson(key string, value interface{}, t ...time.Duration) (st
if err != nil {
return "", fmt.Errorf("set key:%s 序列化json失败", key)
}
return r.Client.Set(r.ctx, key, v, t2).Result()
return r.Client.Set(ctx, key, v, t2).Result()
}
func (r *MyRedis) Close() {