This commit is contained in:
2023-03-20 20:53:18 +08:00
parent 9c83b42ccd
commit 2fa24a33ab
2 changed files with 41 additions and 42 deletions

View File

@@ -26,8 +26,8 @@ var (
type (
MyRedis struct {
redis *redis.Client
ctx context.Context
*redis.Client
ctx context.Context
}
Config struct {
@@ -106,8 +106,8 @@ func New(key string, config *Config) (*MyRedis, error) {
ctx := context.Background()
rd := &MyRedis{}
rd.ctx = ctx
rd.redis = client
ping := rd.redis.Ping(ctx)
rd.Client = client
ping := rd.Client.Ping(ctx)
if ping.Err() != nil {
return nil, fmt.Errorf("connet redis err: %s", ping.Err())
}
@@ -116,23 +116,23 @@ func New(key string, config *Config) (*MyRedis, error) {
return rd, nil
}
// Get 通用get
func (r *MyRedis) Get(key string) (string, error) {
return r.redis.Get(r.ctx, key).Result()
// GetSimple 通用get
func (r *MyRedis) GetSimple(key string) (string, error) {
return r.Client.Get(r.ctx, key).Result()
}
// Set 通用set
func (r *MyRedis) Set(key string, value interface{}, t ...time.Duration) (string, error) {
// SetSimple 通用set
func (r *MyRedis) SetSimple(key string, value interface{}, t ...time.Duration) (string, error) {
var t2 time.Duration
if len(t) > 0 {
t2 = t[0]
}
return r.redis.Set(r.ctx, key, value, t2).Result()
return r.Client.Set(r.ctx, key, value, t2).Result()
}
// GetJson json序列化
func (r *MyRedis) GetJson(key string) (interface{}, error) {
res := r.redis.Get(r.ctx, key)
res := r.Client.Get(r.ctx, key)
if res.Err() != nil {
return nil, res.Err()
}
@@ -159,15 +159,25 @@ 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.redis.Set(r.ctx, key, v, t2).Result()
return r.Client.Set(r.ctx, key, v, t2).Result()
}
func (r *MyRedis) Close() {
if r.redis != nil {
r.redis.Close()
if r.Client != nil {
r.Client.Close()
}
}
func (r *MyRedis) Redis() *redis.Client {
return r.redis
func (r *MyRedis) GetConn() *redis.Client {
return r.Client
}
func Close(key string) {
Instance(key).Client.Close()
}
func CloseAll() {
for _, v := range instanceMap {
v.Client.Close()
}
}