package redis import ( "context" "time" cstring "github.com/cherry-game/cherry/extend/string" clog "github.com/cherry-game/cherry/logger" "github.com/go-redis/redis/v8" jsoniter "github.com/json-iterator/go" ) type ( IClient interface { ConfigID() string // 节点配置ID RedisID() string // redisID load(client *client) // 加载函数 } client struct { *redis.Client redisID string } ) func newClient(redisID string, options *redis.Options) *client { return &client{ redisID: redisID, Client: redis.NewClient(options), } } func Context() context.Context { return context.Background() } func (p *client) IsNil() bool { return p.Client == nil } func (p *client) RedisID() string { return p.redisID } func (p *client) GetString(key string) (string, error) { return p.Get(Context(), key).Result() } func (p *client) GetInt32(key string) (int32, error) { val, err := p.GetString(key) if err != nil { return 0, err } return cstring.ToInt32D(val, 0), nil } func (p *client) GetInt64(key string) (int64, error) { cmd := p.Get(Context(), key) if cmd.Err() != nil { return 0, cmd.Err() } return cmd.Int64() } func (p *client) GetBytes(key string) ([]byte, error) { cmd := p.Get(Context(), key) if cmd.Err() != nil { return nil, cmd.Err() } return cmd.Bytes() } func (p *client) GetJSON(key string, value any) error { cmd := p.Get(Context(), key) if cmd.Err() != nil { return cmd.Err() } return jsoniter.UnmarshalFromString(cmd.Val(), value) } func (p *client) HGetString(key, field string) (string, error) { cmd := p.HGet(Context(), key, field) if cmd.Err() != nil { return "", cmd.Err() } return cmd.Result() } func (p *client) HGetInt64(key, field string) (int64, error) { cmd := p.HGet(Context(), key, field) if cmd.Err() != nil { return 0, cmd.Err() } return cmd.Int64() } func (p *client) HGetJSON(key, field string, value any) error { cmd := p.HGet(Context(), key, field) if cmd.Err() != nil { return cmd.Err() } return jsoniter.UnmarshalFromString(cmd.Val(), value) } func (p *client) HGetStringMap(key string, fields ...string) (map[string]string, error) { result, err := p.Client.HMGet(Context(), key).Result() if err != nil { return nil, err } m := map[string]string{} for i := 0; i < len(result)-1; i += 2 { k := cstring.ToString(result[i]) v := cstring.ToString(result[i+1]) m[k] = v } return m, nil } func (p *client) SetInt64(key string, value int64, expire time.Duration) bool { return p.SetAny(key, value, expire) } func (p *client) SetString(key string, value string, expire time.Duration) bool { return p.SetAny(key, value, expire) } func (p *client) SetBytes(key string, value []byte, expire time.Duration) bool { return p.SetAny(key, value, expire) } func (p *client) SetJSON(key string, value any, expire time.Duration) bool { str, err := jsoniter.MarshalToString(value) if err != nil { clog.Warnf("[SetJSON] marshal error. value = %v", value) return false } return p.SetAny(key, str, expire) } func (p *client) SetAny(key string, value any, expire time.Duration) bool { _, err := p.Set(Context(), key, value, expire).Result() if err != nil { clog.Warnf("[SetAny] result error. [redisID = %s, key = %s] [error = %v]", p.redisID, key, err) return false } return true } func (p *client) Exists(key string) bool { val, err := p.Client.Exists(Context(), key).Result() if err != nil { clog.Warnf("[Exists] result error. [redisID = %s, key = %s] [error = %v]", p.redisID, key, err) return false } return val == 1 } func (p *client) Del(key ...string) bool { val, err := p.Client.Del(Context(), key...).Result() if err != nil { clog.Warnf("[Del] result error. [redisID = %s, key = %s] [error = %v]", p.redisID, key, err) return false } return val == 1 } func (p *client) Scan(pattern string, cursor uint64, count int64) ([]string, uint64, error) { sc := p.Client.Scan(Context(), cursor, pattern, count) return sc.Result() } func (p *client) ListRange(key string, start, stop int64) ([]string, error) { val, err := p.LRange(Context(), key, start, stop).Result() if err != nil { clog.Warnf("[ListRange] result error. [redisID = %s, key = %s, start = %d, stop = %d] [error = %v]", p.redisID, key, start, stop, err, ) return nil, err } return val, nil } func (p *client) HSetJSON(key, field string, value any) error { jsonBytes, err := jsoniter.Marshal(value) if err != nil { clog.Warnf("[HSetJSON] marshal error. [redisID = %s, key = %s, field = %v] [error = %v]", p.redisID, key, field, err, ) return err } return p.HSet(Context(), key, field, jsonBytes).Err() }