You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.0 KiB
129 lines
3.0 KiB
package common_utils
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/redis/go-redis/v9"
|
|
"log"
|
|
"time"
|
|
)
|
|
|
|
type RedisHelper struct {
|
|
rdb redis.UniversalClient
|
|
isReady bool
|
|
ctx context.Context
|
|
}
|
|
|
|
func NewRedisHelper(master string, address ...string) *RedisHelper {
|
|
r := &RedisHelper{ctx: context.Background()}
|
|
r.InitialCluster(master, address...)
|
|
return r
|
|
|
|
}
|
|
|
|
func (the *RedisHelper) InitialCluster(master string, address ...string) {
|
|
|
|
if master != "" {
|
|
the.rdb = redis.NewUniversalClient(&redis.UniversalOptions{
|
|
Addrs: address,
|
|
MasterName: "mymaster",
|
|
})
|
|
} else {
|
|
the.rdb = redis.NewUniversalClient(&redis.UniversalOptions{
|
|
Addrs: address,
|
|
})
|
|
}
|
|
log.Printf("redis 初始化完成 %s", address)
|
|
the.isReady = true
|
|
}
|
|
|
|
func (the *RedisHelper) Get(key string) string {
|
|
val, err := the.rdb.Get(the.ctx, key).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
log.Printf("%s does not exist", key)
|
|
} else if err != nil {
|
|
panic(err)
|
|
} else {
|
|
//log.Printf("get key => %s =%s", key, val)
|
|
}
|
|
return val
|
|
}
|
|
|
|
func (the *RedisHelper) GetObj(keys string, addr any) error {
|
|
err := the.rdb.Get(the.ctx, keys).Scan(addr)
|
|
if errors.Is(err, redis.Nil) {
|
|
log.Printf("%s does not exist", keys)
|
|
} else if err != nil {
|
|
es := err.Error()
|
|
log.Printf("err=%s ", es)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
func (the *RedisHelper) SetObj(keys string, obj any) error {
|
|
rs, err := the.rdb.Set(the.ctx, keys, obj, time.Minute*5).Result()
|
|
log.Printf("rs=%s ", rs)
|
|
if err != nil {
|
|
log.Printf("err=%s ", err.Error())
|
|
}
|
|
return err
|
|
}
|
|
func (the *RedisHelper) GetLRange(keys string, addr any) error {
|
|
err := the.rdb.LRange(the.ctx, keys, 0, -1).ScanSlice(addr)
|
|
if errors.Is(err, redis.Nil) {
|
|
log.Printf("%s does not exist", keys)
|
|
} else if err != nil {
|
|
log.Printf("err=%s ", err.Error())
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (the *RedisHelper) MGetObj(addr any, keys ...string) error {
|
|
err := the.rdb.MGet(the.ctx, keys...).Scan(addr)
|
|
if errors.Is(err, redis.Nil) {
|
|
log.Printf("%s does not exist", keys)
|
|
} else if err != nil {
|
|
log.Printf("err=%s ", err.Error())
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
func (the *RedisHelper) HMGetObj(addr any, key, field string) error {
|
|
rp, err := the.rdb.HMGet(the.ctx, key, field).Result()
|
|
if errors.Is(err, redis.Nil) {
|
|
log.Printf("%s does not exist", key)
|
|
return err
|
|
} else if err != nil {
|
|
log.Printf("err=%s ", err.Error())
|
|
return err
|
|
}
|
|
for _, i := range rp {
|
|
if v, ok := i.(string); ok {
|
|
err := json.Unmarshal([]byte(v), addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
//todo scan有问题 后续待排查
|
|
return nil
|
|
|
|
//err := the.rdb.HMGet(the.ctx, key, field).Scan(addr)
|
|
//if errors.Is(err, redis.Nil) {
|
|
// log.Printf("%s does not exist", key)
|
|
//} else if err != nil {
|
|
// log.Printf("err=%s ", err.Error())
|
|
// return err
|
|
//}
|
|
//return nil
|
|
}
|
|
|
|
func (the *RedisHelper) SRem(key string, members ...string) int64 {
|
|
return the.rdb.SRem(the.ctx, key, members).Val()
|
|
}
|
|
|
|
func (the *RedisHelper) SAdd(key string, members ...string) int64 {
|
|
return the.rdb.SAdd(the.ctx, key, members).Val()
|
|
}
|
|
|