package redis import ( "context" "encoding/json" "fmt" "time" "go_dreamfactory/lego/sys/redis/cluster" "go_dreamfactory/lego/sys/redis/single" "github.com/go-redis/redis/v8" "google.golang.org/protobuf/proto" ) func newSys(options Options) (sys *Redis, err error) { sys = &Redis{options: options} err = sys.init() return } type Redis struct { options Options client IRedis } func (this *Redis) init() (err error) { if this.options.RedisType == Redis_Single { this.client, err = single.NewSys( this.options.Redis_Single_Addr, this.options.Redis_Single_Password, this.options.Redis_Single_DB, this.options.Redis_Single_PoolSize, this.options.TimeOut, this.Encode, this.Decode, ) } else if this.options.RedisType == Redis_Cluster { this.client, err = cluster.NewSys( this.options.Redis_Cluster_Addr, this.options.Redis_Cluster_Password, this.options.TimeOut, this.Encode, this.Decode, ) } else { err = fmt.Errorf("init Redis err:RedisType - %d", this.options.RedisType) } return } func (this *Redis) Close() (err error) { return this.client.Close() } func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd { return this.client.Do(ctx, args...) } func (this *Redis) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { return this.client.Pipeline(ctx, fn) } func (this *Redis) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) { return this.client.TxPipelined(ctx, fn) } func (this *Redis) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) { return this.client.Watch(ctx, fn) } func (this *Redis) Lock(key string, outTime int) (result bool, err error) { return this.client.Lock(key, outTime) } func (this *Redis) UnLock(key string) (err error) { return this.client.UnLock(key) } ///数据编码 func (this *Redis) Encode(value interface{}) (result []byte, err error) { if this.options.RedisStorageType == JsonData { result, err = json.Marshal(value) } else { if _, ok := value.(proto.Message); ok { result, err = proto.Marshal(value.(proto.Message)) } else { result, err = json.Marshal(value) } } return } func (this *Redis) Decode(value []byte, result interface{}) (err error) { if this.options.RedisStorageType == JsonData { err = json.Unmarshal(value, result) } else { if _, ok := result.(proto.Message); ok { err = proto.Unmarshal(value, result.(proto.Message)) } else { err = json.Unmarshal(value, result) } } return } func (this *Redis) Delete(key ...string) (int64, error) { return this.client.Delete(key...) } func (this *Redis) Exists(key ...string) (int64, error) { return this.client.Exists(key...) } func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { return this.client.Expire(key, expire) } func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { return this.client.ExpireAt(key, expire) } func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { return this.client.PExpire(key, expire) } func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { return this.client.PExpireAt(key, expire) } func (this *Redis) Persist(key string) (bool, error) { return this.client.Persist(key) } func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { return this.client.PTTL(key) } func (this *Redis) TTL(key string) (surplus time.Duration, err error) { return this.client.TTL(key) } func (this *Redis) Rename(oldkey string, newkey string) (string, error) { return this.client.Rename(oldkey, newkey) } func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { return this.client.RenameNX(oldkey, newkey) } func (this *Redis) Keys(pattern string) (keys []string, err error) { return this.client.Keys(pattern) } ///获取键类型 func (this *Redis) Type(key string) (ty string, err error) { return this.client.Type(key) } /*String*/ func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { return this.client.Set(key, v, expiration) } func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { return this.client.SetNX(key, v, expiration) } func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { return this.client.MSet(keyvalues) } func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { return this.client.MSetNX(keyvalues) } func (this *Redis) Incr(key string) (int64, error) { return this.client.Incr(key) } func (this *Redis) IncrBY(key string, value int64) (int64, error) { return this.client.IncrBY(key, value) } func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { return this.client.Incrbyfloat(key, value) } func (this *Redis) Decr(key string, value int) (int64, error) { return this.client.Decr(key, value) } func (this *Redis) DecrBy(key string, value int64) (int64, error) { return this.client.DecrBy(key, value) } func (this *Redis) Append(key string, value string) (err error) { return this.client.Append(key, value) } func (this *Redis) Get(key string, value interface{}) (err error) { return this.client.Get(key, value) } func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { return this.client.GetSet(key, value, result) } func (this *Redis) MGet(keys ...string) *redis.SliceCmd { return this.client.MGet(keys...) } /*List*/ func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { return this.client.Lindex(key, index, v) } func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { return this.client.Linsert(key, isbefore, pivot, value) } func (this *Redis) Llen(key string) (int64, error) { return this.client.Llen(key) } func (this *Redis) LPop(key string, value interface{}) (err error) { return this.client.LPop(key, value) } func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { return this.client.LPush(key, values...) } func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { return this.client.LPushX(key, values...) } func (this *Redis) LRange(key string, start int64, end int64) *redis.StringSliceCmd { return this.client.LRange(key, start, end) } func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { return this.client.LRem(key, count, v) } func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { return this.client.LSet(key, index, v) } func (this *Redis) LTrim(key string, start int64, stop int64) (string, error) { return this.client.LTrim(key, start, stop) } func (this *Redis) RPop(key string, value interface{}) (err error) { return this.client.RPop(key, value) } func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { return this.client.RPopLPush(oldkey, newkey, value) } func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { return this.client.RPush(key, values...) } func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { return this.client.RPushX(key, values...) } /*Hash*/ func (this *Redis) HDel(key string, fields ...string) (err error) { return this.client.HDel(key, fields...) } func (this *Redis) HExists(key string, field string) (result bool, err error) { return this.client.HExists(key, field) } func (this *Redis) HGet(key string, field string, value interface{}) (err error) { return this.client.HGet(key, field, value) } func (this *Redis) HGetAll(key string, v interface{}) (err error) { return this.client.HGetAll(key, v) } func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { return this.client.HIncrBy(key, field, value) } func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { return this.client.HIncrByFloat(key, field, value) } func (this *Redis) Hkeys(key string) (result []string, err error) { return this.client.Hkeys(key) } func (this *Redis) Hlen(key string) (result int64, err error) { return this.client.Hlen(key) } func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd { return this.client.HMGet(key, fields...) } func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) { return this.client.HMSet(key, value) } func (this *Redis) HSet(key string, field string, value interface{}) (err error) { return this.client.HSet(key, field, value) } func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { return this.client.HSetNX(key, field, value) } /*Set*/ func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { return this.client.SAdd(key, values...) } func (this *Redis) SCard(key string) (result int64, err error) { return this.client.SCard(key) } func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { return this.client.SDiff(keys...) } func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { return this.client.SDiffStore(destination, keys...) } func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { return this.client.SInter(keys...) } func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { return this.client.SInterStore(destination, keys...) } func (this *Redis) SIsMember(key string, value interface{}) (iskeep bool, err error) { return this.client.SIsMember(key, value) } func (this *Redis) SMembers(key string) *redis.StringSliceCmd { return this.client.SMembers(key) } func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { return this.client.SMove(source, destination, member) } func (this *Redis) SPop(key string) (string, error) { return this.client.SPop(key) } func (this *Redis) SRandMember(key string) (string, error) { return this.client.SRandMember(key) } func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { return this.client.SRem(key, members...) } func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { return this.client.SUnion(keys...) } func (this *Redis) SUnionStore(destination string, keys ...string) (result int64, err error) { return this.client.SUnionStore(destination, keys...) } func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { return this.client.SScan(key, _cursor, match, count) } /*ZSet*/ func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { return this.client.ZAdd(key, members...) } func (this *Redis) ZCard(key string) (result int64, err error) { return this.client.ZCard(key) } func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { return this.client.ZCount(key, min, max) } func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { return this.client.ZIncrBy(key, increment, member) } func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { return this.client.ZInterStore(destination, store) } func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { return this.client.ZLexCount(key, min, max) } func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { return this.client.ZRange(key, start, stop) } func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { return this.client.ZRangeByLex(key, opt) } func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { return this.client.ZRangeByScore(key, opt) } func (this *Redis) ZRank(key string, member string) (result int64, err error) { return this.client.ZRank(key, member) } func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { return this.client.ZRem(key, members...) } func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { return this.client.ZRemRangeByLex(key, min, max) } func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { return this.client.ZRemRangeByRank(key, start, stop) } func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { return this.client.ZRemRangeByScore(key, min, max) } func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { return this.client.ZRevRange(key, start, stop) } func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { return this.client.ZRevRangeByScore(key, opt) } func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { return this.client.ZRevRank(key, member) } func (this *Redis) ZScore(key string, member string) (result float64, err error) { return this.client.ZScore(key, member) } func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { return this.client.ZUnionStore(dest, store) } func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { return this.client.ZScan(key, _cursor, match, count) }