diff --git a/lego/sys/redis/cluster/core.go b/lego/sys/redis/cluster/core.go index a59688ae9..7d1ccdf91 100644 --- a/lego/sys/redis/cluster/core.go +++ b/lego/sys/redis/cluster/core.go @@ -88,6 +88,6 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) { //锁 func (this *Redis) UnLock(key string) (err error) { - _, err = this.Delete(key) + err = this.Delete(key) return } diff --git a/lego/sys/redis/cluster/hash.go b/lego/sys/redis/cluster/hash.go index 4572af92b..875e1235c 100644 --- a/lego/sys/redis/cluster/hash.go +++ b/lego/sys/redis/cluster/hash.go @@ -1,6 +1,8 @@ package cluster import ( + "reflect" + "github.com/go-redis/redis/v8" ) @@ -8,7 +10,13 @@ import ( Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略 */ func (this *Redis) HDel(key string, fields ...string) (err error) { - err = this.client.HDel(this.getContext(), key, fields...).Err() + agrs := make([]interface{}, 0) + agrs = append(agrs, "HDEL") + agrs = append(agrs, key) + for _, v := range fields { + agrs = append(agrs, v) + } + err = this.client.Do(this.getContext(), agrs...).Err() return } @@ -16,15 +24,18 @@ func (this *Redis) HDel(key string, fields ...string) (err error) { Redis Hexists 命令用于查看哈希表的指定字段是否存在 */ func (this *Redis) HExists(key string, field string) (result bool, err error) { - this.client.HExists(this.getContext(), key, field) + result, err = this.client.Do(this.getContext(), "HEXISTS", key, field).Bool() return } /* Redis Hget 命令用于返回哈希表中指定字段的值 */ -func (this *Redis) HGet(key string, field string, v interface{}) (err error) { - err = this.client.HGet(this.getContext(), key, field).Scan(v) +func (this *Redis) HGet(key string, field string, value interface{}) (err error) { + var resultvalue string + if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) { + err = this.Decode([]byte(resultvalue), value) + } return } @@ -32,8 +43,19 @@ func (this *Redis) HGet(key string, field string, v interface{}) (err error) { Redis Hgetall 命令用于返回哈希表中,所有的字段和值。 在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍 */ -func (this *Redis) HGetAll(key string, v interface{}) (err error) { - err = this.client.HGetAll(this.getContext(), key).Scan(v) +func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { + cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) + this.client.Process(this.getContext(), cmd) + var _result []string + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } return } @@ -45,8 +67,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 本操作的值被限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { - err = this.client.HIncrBy(this.getContext(), key, field, value).Err() +func (this *Redis) HIncrBy(key string, field string, value int) (err error) { + err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() return } @@ -54,8 +76,8 @@ func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 */ -func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { - err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err() +func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { + err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() return } @@ -63,21 +85,17 @@ func (this *Redis) HIncrByFloat(key string, field string, value float64) (err er Redis Hkeys 命令用于获取哈希表中的所有域(field) */ func (this *Redis) Hkeys(key string) (result []string, err error) { - var cmd *redis.StringSliceCmd - cmd = this.client.HKeys(this.getContext(), key) - result = cmd.Val() - err = cmd.Err() + cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() return } /* Redis Hlen 命令用于获取哈希表中字段的数量 */ -func (this *Redis) Hlen(key string) (result int64, err error) { - var cmd *redis.IntCmd - cmd = this.client.HLen(this.getContext(), key) - result = cmd.Val() - err = cmd.Err() +func (this *Redis) Hlen(key string) (result int, err error) { + result, err = this.client.Do(this.getContext(), "HLEN", key).Int() return } @@ -85,8 +103,26 @@ func (this *Redis) Hlen(key string) (result int64, err error) { Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 如果指定的字段不存在于哈希表,那么返回一个 nil 值 */ -func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd { - return this.client.HMGet(this.getContext(), key, fields...) +func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "HMGET") + agrs = append(agrs, key) + for _, v := range fields { + agrs = append(agrs, v) + } + cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) + this.client.Process(this.getContext(), cmd) + var _result []string + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* @@ -112,7 +148,10 @@ Redis Hset 命令用于为哈希表中的字段赋值 如果字段已经存在于哈希表中,旧值将被覆盖 */ func (this *Redis) HSet(key string, field string, value interface{}) (err error) { - err = this.client.HSet(this.getContext(), key, field, value).Err() + var resultvalue []byte + if resultvalue, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err() + } return } @@ -123,6 +162,9 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令 */ func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { - err = this.client.HSetNX(this.getContext(), key, field, value).Err() + var resultvalue []byte + if resultvalue, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err() + } return } diff --git a/lego/sys/redis/cluster/key.go b/lego/sys/redis/cluster/key.go index 311515e50..2db92cd50 100644 --- a/lego/sys/redis/cluster/key.go +++ b/lego/sys/redis/cluster/key.go @@ -1,69 +1,84 @@ package cluster import ( - "time" + "github.com/go-redis/redis/v8" ) /* Key *******************************************************************************/ ///删除redis key -func (this *Redis) Delete(key ...string) (int64, error) { - return this.client.Del(this.getContext(), key...).Result() +func (this *Redis) Delete(key string) (err error) { + err = this.client.Do(this.getContext(), "DEL", key).Err() + + return } ///判断是否存在key -func (this *Redis) Exists(key ...string) (int64, error) { - return this.client.Exists(this.getContext(), key...).Result() +func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { + iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() + return } ///设置key的过期时间 单位以秒级 -func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { - return this.client.Expire(this.getContext(), key, expire).Result() +func (this *Redis) ExpireKey(key string, expire int) (err error) { + err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() + return } ///设置key的过期时间戳 秒级时间戳 -func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { - return this.client.ExpireAt(this.getContext(), key, expire).Result() +func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { + err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() + return } ///设置key的过期时间 单位以毫秒级 -func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { - return this.client.PExpire(this.getContext(), key, expire).Result() +func (this *Redis) Pexpirekey(key string, expire int) (err error) { + err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() + return } ///设置key的过期时间戳 单位以豪秒级 -func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { - return this.client.PExpireAt(this.getContext(), key, expire).Result() +func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { + err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() + return } ///移除Key的过期时间 -func (this *Redis) Persist(key string) (bool, error) { - return this.client.Persist(this.getContext(), key).Result() +func (this *Redis) PersistKey(key string) (err error) { + err = this.client.Do(this.getContext(), "PERSIST", key).Err() + return } ///获取key剩余过期时间 单位毫秒 -func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { - return this.client.PTTL(this.getContext(), key).Result() +func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { + leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() + return } ///获取key剩余过期时间 单位秒 -func (this *Redis) TTL(key string) (surplus time.Duration, err error) { - return this.client.TTL(this.getContext(), key).Result() +func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { + leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() + return } ///重命名Key -func (this *Redis) Rename(oldkey string, newkey string) (string, error) { - return this.client.Rename(this.getContext(), oldkey, newkey).Result() +func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { + err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() + return } ///重命名key 在新的 key 不存在时修改 key 的名称 -func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { - return this.client.RenameNX(this.getContext(), oldkey, newkey).Result() +func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { + err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() + return } ///判断是否存在key pattern:key* func (this *Redis) Keys(pattern string) (keys []string, err error) { - return this.client.Keys(this.getContext(), pattern).Result() + cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) + this.client.Process(this.getContext(), cmd) + keys, err = cmd.Result() + return } ///获取键类型 diff --git a/lego/sys/redis/cluster/list.go b/lego/sys/redis/cluster/list.go index 9b9981167..9e5a98c7a 100644 --- a/lego/sys/redis/cluster/list.go +++ b/lego/sys/redis/cluster/list.go @@ -1,14 +1,23 @@ package cluster import ( + "fmt" + "reflect" + "github.com/go-redis/redis/v8" ) /* Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { - return this.client.LIndex(this.getContext(), key, index).Scan(v) +func (this *Redis) Lindex(key string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* @@ -16,55 +25,90 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { - op := "BEFORE" - if !isbefore { - op = "AFTER" +func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { + var ( + tagervalue []byte + resultvalue []byte + ) + if tagervalue, err = this.Encode(tager); err == nil { + if resultvalue, err = this.Encode(value); err == nil { + if isbefore { + err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err() + } else { + err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err() + } + } } - return this.client.LInsert(this.getContext(), key, op, pivot, value).Result() + return } /* Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Llen(key string) (int64, error) { - return this.client.LLen(this.getContext(), key).Result() +func (this *Redis) Llen(key string) (result int, err error) { + result, err = this.client.Do(this.getContext(), "LLEN", key).Int() + return } /* Redis Lpop 命令用于移除并返回列表的第一个元素 */ -func (this *Redis) LPop(key string, v interface{}) (err error) { - return this.client.LPop(this.getContext(), key).Scan(v) +func (this *Redis) LPop(key string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 */ -func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { - return this.client.LPush(this.getContext(), key, values...).Result() +func (this *Redis) LPush(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "LPUSH") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 */ -func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { - // agrs := make([]interface{}, 0) - // agrs = append(agrs, "LPUSHX") - // for _, v := range values { - // result, _ := this.Encode(v) - // agrs = append(agrs, result) - // } - // err = this.client.Do(this.getContext(), agrs...).Err() - return this.client.LPushX(this.getContext(), key, values...).Result() +func (this *Redis) LPushX(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "LPUSHX") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd { - return this.client.LRange(this.getContext(), key, start, end) +func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { + var _result []string + cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end) + this.client.Process(this.getContext(), cmd) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* @@ -74,16 +118,24 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素, count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 count = 0 : 移除表中所有与 VALUE 相等的值 */ -func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { - return this.client.LRem(this.getContext(), key, count, v).Result() +func (this *Redis) LRem(key string, count int, target interface{}) (err error) { + var resultvalue []byte + if resultvalue, err = this.Encode(target); err == nil { + err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err() + } + return } /* -Redis LSet 通过索引来设置元素的值。 +Redis Lset 通过索引来设置元素的值。 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 */ -func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { - return this.client.LSet(this.getContext(), key, index, v).Result() +func (this *Redis) LSet(key string, index int, value interface{}) (err error) { + var resultvalue []byte + if resultvalue, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err() + } + return } /* @@ -91,22 +143,35 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) LTrim(key string, start, stop int64) (string, error) { - return this.client.LTrim(this.getContext(), key, start, stop).Result() +func (this *Redis) Ltrim(key string, start, stop int) (err error) { + err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() + return } /* Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 */ -func (this *Redis) RPop(key string, v interface{}) (err error) { - return this.client.RPop(this.getContext(), key).Scan(v) +func (this *Redis) Rpop(key string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 */ -func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) { - return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v) +func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* @@ -114,13 +179,27 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边) 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 */ -func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { - return this.client.RPush(this.getContext(), key, values...).Result() +func (this *Redis) RPush(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "RPUSH") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 */ -func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { - return this.client.RPushX(this.getContext(), key, values...).Result() +func (this *Redis) RPushX(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "RPUSHX") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } diff --git a/lego/sys/redis/cluster/set.go b/lego/sys/redis/cluster/set.go index 64a59cb3e..74d5561f6 100644 --- a/lego/sys/redis/cluster/set.go +++ b/lego/sys/redis/cluster/set.go @@ -1,23 +1,30 @@ package cluster -import ( - "github.com/go-redis/redis/v8" -) +import "reflect" /* Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 当集合 key 不是集合类型时,返回一个错误。 */ -func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { - return this.client.SAdd(this.getContext(), key, values...).Result() +func (this *Redis) SAdd(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "SADD") + agrs = append(agrs, key) + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Scard 命令返回集合中元素的数量 */ -func (this *Redis) SCard(key string) (int64, error) { - return this.client.SCard(this.getContext(), key).Result() +func (this *Redis) SCard(key string) (result int64, err error) { + result, err = this.client.SCard(this.getContext(), key).Result() + return } /* @@ -25,43 +32,79 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 实例: */ -func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { - return this.client.SDiff(this.getContext(), keys...) +func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SDiff(this.getContext(), keys...) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 */ -func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) { - return this.client.SDiffStore(this.getContext(), destination, keys...).Result() +func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { + result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() + return } /* Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 */ -func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { - return this.client.SInter(this.getContext(), keys...) +func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SInter(this.getContext(), keys...) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 */ -func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) { - return this.client.SInterStore(this.getContext(), destination, keys...).Result() +func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { + result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() + return } /* Redis Sismember 命令判断成员元素是否是集合的成员 */ -func (this *Redis) SIsMember(key string, value interface{}) (bool, error) { - return this.client.SIsMember(this.getContext(), key, value).Result() +func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { + iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() + return } /* Redis Smembers 号召返回集合中的所有成员。 */ -func (this *Redis) SMembers(key string) *redis.StringSliceCmd { - return this.client.SMembers(this.getContext(), key) +func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SMembers(this.getContext(), key) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* @@ -71,16 +114,18 @@ SMOVE 是原子性操作。 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 当 source 或 destination 不是集合类型时,返回一个错误。 */ -func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) { - return this.client.SMove(this.getContext(), source, destination, member).Result() +func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { + result, err = this.client.SMove(this.getContext(), source, destination, member).Result() + return } /* Redis Spop命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 */ -func (this *Redis) SPop(key string) (string, error) { - return this.client.SPop(this.getContext(), key).Result() +func (this *Redis) Spop(key string) (result string, err error) { + result, err = this.client.SPop(this.getContext(), key).Result() + return } /* @@ -90,8 +135,9 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 */ -func (this *Redis) SRandMember(key string) (string, error) { - return this.client.SRandMember(this.getContext(), key).Result() +func (this *Redis) Srandmember(key string) (result string, err error) { + result, err = this.client.SRandMember(this.getContext(), key).Result() + return } /* @@ -99,27 +145,41 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在 当键不是集合类型,返回一个错误。 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 */ -func (this *Redis) SRem(key string, members ...interface{}) (int64, error) { - return this.client.SRem(this.getContext(), key, members...).Result() +func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { + result, err = this.client.SRem(this.getContext(), key, members...).Result() + return } /* Redis Sunion 命令返回给定集合的并集。 */ -func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { - return this.client.SUnion(this.getContext(), keys...) +func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SUnion(this.getContext(), keys...) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 */ -func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) { - return this.client.SUnionStore(this.getContext(), destination, keys...).Result() +func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { + result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() + return } /* Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 */ -func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { - return this.client.SScan(this.getContext(), key, _cursor, match, count).Result() +func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { + keys, cursor, err = this.client.SScan(this.getContext(), key, _cursor, match, count).Result() + return } diff --git a/lego/sys/redis/cluster/string.go b/lego/sys/redis/cluster/string.go index 1dee13835..17fc6f1a9 100644 --- a/lego/sys/redis/cluster/string.go +++ b/lego/sys/redis/cluster/string.go @@ -1,6 +1,7 @@ package cluster import ( + "fmt" "time" "github.com/go-redis/redis/v8" @@ -10,39 +11,54 @@ import ( /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 */ -func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { - return this.client.Set(this.getContext(), string(key), v, expiration).Result() +func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { + var result []byte + if result, err = this.Encode(value); err == nil { + err = this.client.Set(this.getContext(), string(key), result, expiration).Err() + } + return } /* 指定的 key 不存在时,为 key 设置指定的值 */ -func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { - return this.client.SetNX(this.getContext(), key, v, expiration).Result() +func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { + // var _value []byte + // if result, err = this.Encode(value); err == nil { + // err = this.client.Do(this.getContext(), "SETNX", key, result).Err() + cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() + // } + return } /* 同时设置一个或多个 key-value 对 */ -func (this *Redis) MSet(keyvalues map[string]interface{}) error { +func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { agrs := make([]interface{}, 0) agrs = append(agrs, "MSET") for k, v := range keyvalues { - agrs = append(agrs, k, v) + result, _ := this.Encode(v) + agrs = append(agrs, k, result) } - return this.client.Do(this.getContext(), agrs...).Err() + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 */ -func (this *Redis) MSetNX(keyvalues map[string]interface{}) error { +func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { agrs := make([]interface{}, 0) agrs = append(agrs, "MSETNX") for k, v := range keyvalues { - agrs = append(agrs, k, v) + result, _ := this.Encode(v) + agrs = append(agrs, k, result) } - return this.client.Do(this.getContext(), agrs...).Err() + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* @@ -51,8 +67,9 @@ Redis Incr 命令将 key 中储存的数字值增一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内。 */ -func (this *Redis) Incr(key string) (int64, error) { - return this.client.Incr(this.getContext(), key).Result() +func (this *Redis) Incr(key string) (err error) { + err = this.client.Do(this.getContext(), "INCR", key).Err() + return } /* @@ -61,16 +78,18 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) IncrBY(key string, value int64) (int64, error) { - return this.client.IncrBy(this.getContext(), key, value).Result() +func (this *Redis) IncrBY(key string, value int) (err error) { + err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() + return } /* Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 */ -func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { - return this.client.IncrByFloat(this.getContext(), key, value).Result() +func (this *Redis) Incrbyfloat(key string, value float32) (err error) { + err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() + return } /* @@ -79,8 +98,9 @@ Redis Decr 命令将 key 中储存的数字值减一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) Decr(key string, value int) (int64, error) { - return this.client.Decr(this.getContext(), key).Result() +func (this *Redis) Decr(key string, value int) (err error) { + err = this.client.Do(this.getContext(), "DECR", key, value).Err() + return } /* @@ -89,8 +109,9 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) DecrBy(key string, value int64) (int64, error) { - return this.client.DecrBy(this.getContext(), key, value).Result() +func (this *Redis) DecrBy(key string, value int) (err error) { + err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() + return } /* @@ -98,28 +119,62 @@ Redis Append 命令用于为指定的 key 追加值。 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 */ -func (this *Redis) Append(key string, value string) (err error) { - this.client.Append(this.getContext(), key, value).Result() +func (this *Redis) Append(key string, value interface{}) (err error) { + var result []byte + if result, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "APPEND", key, result).Err() + } return } /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 */ -func (this *Redis) Get(key string, v interface{}) (err error) { - return this.client.Get(this.getContext(), key).Scan(v) +func (this *Redis) Get(key string, value interface{}) (err error) { + var result []byte + if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil { + err = this.Decode(result, value) + } + return } /* 设置指定 key 的值,并返回 key 的旧值 */ -func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) { - return this.client.GetSet(this.getContext(), key, v).Scan(result) +func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { + var ( + data string + _value []byte + ) + if _value, err = this.Encode(value); err == nil { + if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), result) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + } + return } /* 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil */ -func (this *Redis) MGet(keys ...string) *redis.SliceCmd { - return this.client.MGet(this.getContext(), keys...) +func (this *Redis) MGet(keys ...string) (result []string, err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "MGET") + for _, v := range keys { + agrs = append(agrs, v) + } + cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() + return +} + +///判断是否存在key pattern:key* +func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { + cmd := redis.NewIntCmd(this.getContext(), "INCRBY", key, amount) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() + return } diff --git a/lego/sys/redis/cluster/zset.go b/lego/sys/redis/cluster/zset.go index 9d3de4aab..206710e22 100644 --- a/lego/sys/redis/cluster/zset.go +++ b/lego/sys/redis/cluster/zset.go @@ -1,145 +1,217 @@ package cluster import ( + "reflect" + "github.com/go-redis/redis/v8" ) /* Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 */ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { - return this.client.ZAdd(this.getContext(), key, members...).Result() +func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { + this.client.ZAdd(this.getContext(), key, members...) + return } /* Redis Zcard 用于计算集合中元素的数量。 */ -func (this *Redis) ZCard(key string) (int64, error) { - return this.client.ZCard(this.getContext(), key).Result() +func (this *Redis) ZCard(key string) (result int64, err error) { + result, err = this.client.ZCard(this.getContext(), key).Result() + return } /* Redis ZCount 用于计算集合中指定的范围内的数量 */ -func (this *Redis) ZCount(key string, min string, max string) (int64, error) { - return this.client.ZCount(this.getContext(), key, min, max).Result() +func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZCount(this.getContext(), key, min, max).Result() + return } /* Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment */ -func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) { - return this.client.ZIncrBy(this.getContext(), key, increment, member).Result() +func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { + result, err = this.client.ZIncrBy(this.getContext(), key, increment, member).Result() + return } /* Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 */ -func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) { - return this.client.ZInterStore(this.getContext(), destination, store).Result() +func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { + result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() + return } /* Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 */ -func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) { - return this.client.ZLexCount(this.getContext(), key, min, max).Result() +func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZLexCount(this.getContext(), key, min, max).Result() + return } /* Redis ZRange 通过索引区间返回有序集合指定区间内的成员 */ -func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { - return this.client.ZRange(this.getContext(), key, start, stop) +func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRange(this.getContext(), key, start, stop) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRangeByLex 通过字典区间返回有序集合的成员 */ -func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRangeByLex(this.getContext(), key, opt) +func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRangeByLex(this.getContext(), key, opt) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 */ -func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRangeByScore(this.getContext(), key, opt) +func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRangeByScore(this.getContext(), key, opt) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRank 返回有序集合中指定成员的索引 */ -func (this *Redis) ZRank(key string, member string) (int64, error) { - return this.client.ZRank(this.getContext(), key, member).Result() +func (this *Redis) ZRank(key string, member string) (result int64, err error) { + result, err = this.client.ZRank(this.getContext(), key, member).Result() + return } /* Redis ZRem 移除有序集合中的一个或多个成员 */ -func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) { - return this.client.ZRem(this.getContext(), key, members...).Result() +func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { + result, err = this.client.ZRem(this.getContext(), key, members...).Result() + return } /* Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 */ -func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) { - return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() +func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() + return } /* Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 */ -func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) { - return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() +func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { + result, err = this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() + return } /* Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 */ -func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) { - return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() +func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() + return } /* Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE */ -func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { - return this.client.ZRevRange(this.getContext(), key, start, stop) +func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRevRange(this.getContext(), key, start, stop) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRevRangeByScore(this.getContext(), key, opt) +func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRank(key string, member string) (int64, error) { - return this.client.ZRevRank(this.getContext(), key, member).Result() +func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { + result, err = this.client.ZRevRank(this.getContext(), key, member).Result() + return } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZScore(key string, member string) (float64, error) { - return this.client.ZScore(this.getContext(), key, member).Result() +func (this *Redis) ZScore(key string, member string) (result float64, err error) { + result, err = this.client.ZScore(this.getContext(), key, member).Result() + return } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE */ -func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) { - return this.client.ZUnionStore(this.getContext(), dest, store).Result() +func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { + result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() + return } /* Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) */ -func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { - return this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() +func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { + keys, cursor, err = this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() + return } diff --git a/lego/sys/redis/core.go b/lego/sys/redis/core.go index 74d555214..90af5e2c7 100644 --- a/lego/sys/redis/core.go +++ b/lego/sys/redis/core.go @@ -2,6 +2,7 @@ package redis import ( "context" + "reflect" "time" "github.com/go-redis/redis/v8" @@ -17,382 +18,99 @@ type ( TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) /*Key*/ - ///删除redis key - Delete(key ...string) (int64, error) - ///判断是否存在key - Exists(key ...string) (int64, error) - ///设置key的过期时间 单位以秒级 - Expire(key string, expire time.Duration) (bool, error) - ///设置key的过期时间戳 秒级时间戳 - ExpireAt(key string, expire time.Time) (bool, error) - ///设置key的过期时间 单位以毫秒级 - PExpire(key string, expire time.Duration) (bool, error) - ///设置key的过期时间戳 单位以豪秒级 - PExpireAt(key string, expire time.Time) (bool, error) - ///移除Key的过期时间 - Persist(key string) (bool, error) - ///获取key剩余过期时间 单位毫秒 - PTTL(key string) (surplus time.Duration, err error) - ///获取key剩余过期时间 单位秒 - TTL(key string) (surplus time.Duration, err error) - ///重命名Key - Rename(oldkey string, newkey string) (string, error) - ///重命名key 在新的 key 不存在时修改 key 的名称 - RenameNX(oldkey string, newkey string) (bool, error) - ///判断是否存在key pattern:key* + Delete(key string) (err error) + ExistsKey(key string) (iskeep bool, err error) + ExpireKey(key string, expire int) (err error) + ExpireatKey(key string, expire_unix int64) (err error) + Pexpirekey(key string, expire int) (err error) + PexpireatKey(key string, expire_unix int64) (err error) + PersistKey(key string) (err error) + PttlKey(key string) (leftexpire int64, err error) + TtlKey(key string) (leftexpire int64, err error) + RenameKye(oldkey string, newkey string) (err error) + RenamenxKey(oldkey string, newkey string) (err error) Keys(pattern string) (keys []string, err error) - ///获取键类型 Type(key string) (ty string, err error) /*String*/ - /* - 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 - */ - Set(key string, v interface{}, expiration time.Duration) (string, error) - /* - 指定的 key 不存在时,为 key 设置指定的值 - */ - SetNX(key string, v interface{}, expiration time.Duration) (bool, error) - /* - 同时设置一个或多个 key-value 对 - */ - MSet(keyvalues map[string]interface{}) error - /* - 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 - */ + Set(key string, value interface{}, expiration time.Duration) (err error) + SetNX(key string, value interface{}) (result int64, err error) + MSet(keyvalues map[string]interface{}) (err error) MSetNX(keyvalues map[string]interface{}) (err error) - /* - Redis Incr 命令将 key 中储存的数字值增一。 - 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作。 - 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 - 本操作的值限制在 64 位(bit)有符号数字表示之内。 - */ - Incr(key string) (int64, error) - /* - Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 - 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCRBY 命令。 - 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 - 本操作的值限制在 64 位(bit)有符号数字表示之内 - */ - IncrBY(key string, value int64) (int64, error) - /* - Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 - 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 - */ - Incrbyfloat(key string, value float64) (float64, error) - /* - Redis Decr 命令将 key 中储存的数字值减一。 - 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECR 操作。 - 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 - 本操作的值限制在 64 位(bit)有符号数字表示之内 - */ - Decr(key string, value int) (int64, error) - /* - Redis Decrby 命令将 key 所储存的值减去指定的减量值。 - 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 DECRBY 操作。 - 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 - 本操作的值限制在 64 位(bit)有符号数字表示之内 - */ - DecrBy(key string, value int64) (int64, error) - /* - Redis Append 命令用于为指定的 key 追加值。 - 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 - 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 - */ - Append(key string, value string) (err error) - /* - 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 - */ - Get(key string, v interface{}) (err error) - /* - 设置指定 key 的值,并返回 key 的旧值 - */ - GetSet(key string, v interface{}, result interface{}) (err error) - /* - 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil - */ - MGet(keys ...string) *redis.SliceCmd + Incr(key string) (err error) + IncrBY(key string, value int) (err error) + Incrbyfloat(key string, value float32) (err error) + Decr(key string, value int) (err error) + DecrBy(key string, value int) (err error) + Append(key string, value interface{}) (err error) + Get(key string, value interface{}) (err error) + GetSet(key string, value interface{}, result interface{}) (err error) + MGet(keys ...string) (result []string, err error) + INCRBY(key string, amount int64) (result int64, err error) /*List*/ - /* - Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 - */ - Lindex(key string, index int64, v interface{}) (err error) - /* - Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作。 - 当列表不存在时,被视为空列表,不执行任何操作。 - 如果 key 不是列表类型,返回一个错误 - */ - Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) - /* - Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 - */ - Llen(key string) (int64, error) - /* - Redis Lpop 命令用于移除并返回列表的第一个元素 - */ - LPop(key string, v interface{}) (err error) - /* - Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 - */ - LPush(key string, values ...interface{}) (int64, error) - /* - Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 - */ - LPushX(key string, values ...interface{}) (int64, error) - /* - Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, - 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 - */ - LRange(key string, start, end int64) *redis.StringSliceCmd - /* - Redis Lrem 根据参数 COUNT 的值,移除列表中与参数 VALUE 相等的元素。 - COUNT 的值可以是以下几种: - count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素,数量为 COUNT 。 - count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 - count = 0 : 移除表中所有与 VALUE 相等的值 - */ - LRem(key string, count int64, v interface{}) (int64, error) - /* - Redis Lset 通过索引来设置元素的值。 - 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 - */ - LSet(key string, index int64, v interface{}) (string, error) - /* - Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留指定区间内的元素,不在指定区间之内的元素都将被删除。 - 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, - 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 - */ - LTrim(key string, start, stop int64) (string, error) - /* - Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 - */ - RPop(key string, v interface{}) (err error) - /* - Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 - */ - RPopLPush(oldkey string, newkey string, v interface{}) (err error) - /* - Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边)。 - 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 - 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 - */ - RPush(key string, values ...interface{}) (int64, error) - /* - Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 - */ - RPushX(key string, values ...interface{}) (int64, error) + Lindex(key string, value interface{}) (err error) + Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) + Llen(key string) (result int, err error) + LPop(key string, value interface{}) (err error) + LPush(key string, values ...interface{}) (err error) + LPushX(key string, values ...interface{}) (err error) + LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) + LRem(key string, count int, target interface{}) (err error) + LSet(key string, index int, value interface{}) (err error) + Ltrim(key string, start, stop int) (err error) + Rpop(key string, value interface{}) (err error) + RPopLPush(oldkey string, newkey string, value interface{}) (err error) + RPush(key string, values ...interface{}) (err error) + RPushX(key string, values ...interface{}) (err error) /*Hash*/ - ///Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略 HDel(key string, fields ...string) (err error) - ///Hexists 命令用于查看哈希表的指定字段是否存在 HExists(key string, field string) (result bool, err error) - ///Hget 命令用于返回哈希表中指定字段的值 - HGet(key string, field string, v interface{}) (err error) - ///Hgetall 命令用于返回哈希表中,所有的字段和值。 - ///在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍 - HGetAll(key string, v interface{}) (err error) - /* - Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 - 增量也可以为负数,相当于对指定字段进行减法操作。 - 如果哈希表的 key 不存在,一个新的哈希表被创建并执行 HINCRBY 命令。 - 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 。 - 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 - 本操作的值被限制在 64 位(bit)有符号数字表示之内 - */ - HIncrBy(key string, field string, value int64) (err error) - /* - Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 - 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 - */ - HIncrByFloat(key string, field string, value float64) (err error) - /* - Redis Hkeys 命令用于获取哈希表中的所有域(field) - */ + HGet(key string, field string, value interface{}) (err error) + HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) + HIncrBy(key string, field string, value int) (err error) + HIncrByFloat(key string, field string, value float32) (err error) Hkeys(key string) (result []string, err error) - /* - Redis Hlen 命令用于获取哈希表中字段的数量 - */ - Hlen(key string) (result int64, err error) - /* - Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 - 如果指定的字段不存在于哈希表,那么返回一个 nil 值 - */ - HMGet(key string, fields ...string) *redis.SliceCmd - /* - Redis Hmset 命令用于同时将多个 field-value (字段-值)对设置到哈希表中。 - 此命令会覆盖哈希表中已存在的字段。 - 如果哈希表不存在,会创建一个空哈希表,并执行 HMSET 操作 - */ + Hlen(key string) (result int, err error) + HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) HMSet(key string, value map[string]interface{}) (err error) - /* - Redis Hset 命令用于为哈希表中的字段赋值 - 如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作 - 如果字段已经存在于哈希表中,旧值将被覆盖 - */ HSet(key string, field string, value interface{}) (err error) - /* - Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 - 如果哈希表不存在,一个新的哈希表被创建并进行 HSET 操作 - 如果字段已经存在于哈希表中,操作无效 - 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令 - */ HSetNX(key string, field string, value interface{}) (err error) /*Set*/ - /* - Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 - 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 - 当集合 key 不是集合类型时,返回一个错误。 - */ - SAdd(key string, values ...interface{}) (int64, error) - /* - Redis Scard 命令返回集合中元素的数量 - */ - SCard(key string) (int64, error) - /* - Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可以认为说第一个集合中独有的元素。不存在的集合 key 将视为空集。 - 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 - 实例: - */ - SDiff(keys ...string) *redis.StringSliceCmd - /* - Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 - */ - SDiffStore(destination string, keys ...string) (int64, error) - /* - Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 - */ - SInter(keys ...string) *redis.StringSliceCmd - /* - Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 - */ - SInterStore(destination string, keys ...string) (int64, error) - /* - Redis SIsMember 命令判断成员元素是否是集合的成员 - */ - SIsMember(key string, value interface{}) (bool, error) - /* - Redis Smembers 号召返回集合中的所有成员。 - */ - SMembers(key string) *redis.StringSliceCmd - /* - Redis Smove 命令将指定成员 member 元素从 source 集合移动到 destination 集合。 - SMOVE 是原子性操作。 - 如果 source 集合不存在或不包含指定的 member 元素,则 SMOVE 命令不执行任何操作,仅返回 0 。否则, member 元素从 source 集合中被移除,并添加到 destination 集合中去。 - 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 - 当 source 或 destination 不是集合类型时,返回一个错误。 - */ - SMove(source string, destination string, member interface{}) (bool, error) - /* - Redis SPop 命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 - 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 - */ - SPop(key string) (string, error) - /* - Redis Srandmember 命令用于返回集合中的一个随机元素。 - 从 Redis 2.6 版本开始, Srandmember 命令接受可选的 count 参数: - 如果 count 为正数,且小于集合基数,那么命令返回一个包含 count 个元素的数组,数组中的元素各不相同。如果 count 大于等于集合基数,那么返回整个集合。 - 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 - 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 - */ - SRandMember(key string) (string, error) - /* - Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在的元素元素会被忽略。 - 当键不是集合类型,返回一个错误。 - 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 - */ - SRem(key string, members ...interface{}) (int64, error) - /* - Redis Sunion 命令返回给定集合的并集。 - */ - SUnion(keys ...string) *redis.StringSliceCmd - /* - Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 - */ - SUnionStore(destination string, keys ...string) (int64, error) - /* - Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 - */ - SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) + SAdd(key string, values ...interface{}) (err error) + SCard(key string) (result int64, err error) + SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) + SDiffStore(destination string, keys ...string) (result int64, err error) + SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) + SInterStore(destination string, keys ...string) (result int64, err error) + Sismember(key string, value interface{}) (iskeep bool, err error) + SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) + SMove(source string, destination string, member interface{}) (result bool, err error) + Spop(key string) (result string, err error) + Srandmember(key string) (result string, err error) + SRem(key string, members ...interface{}) (result int64, err error) + SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) + Sunionstore(destination string, keys ...string) (result int64, err error) + Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) /*ZSet*/ - /* - Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 - */ - ZAdd(key string, members ...*redis.Z) (int64, error) - /* - Redis Zcard 用于计算集合中元素的数量。 - */ - ZCard(key string) (int64, error) - /* - Redis ZCount 用于计算集合中指定的范围内的数量 - */ - ZCount(key string, min string, max string) (int64, error) - /* - Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment - */ - ZIncrBy(key string, increment float64, member string) (float64, error) - /* - Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 - */ - ZInterStore(destination string, store *redis.ZStore) (int64, error) - /* - Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 - */ - ZLexCount(key string, min string, max string) (int64, error) - /* - Redis ZRange 通过索引区间返回有序集合指定区间内的成员 - */ - ZRange(key string, start int64, stop int64) *redis.StringSliceCmd - /* - Redis ZRangeByLex 通过字典区间返回有序集合的成员 - */ - ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd - /* - Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 - */ - ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd - /* - Redis ZRank 返回有序集合中指定成员的索引 - */ - ZRank(key string, member string) (int64, error) - /* - Redis ZRem 移除有序集合中的一个或多个成员 - */ - ZRem(key string, members ...interface{}) (int64, error) - /* - Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 - */ - ZRemRangeByLex(key string, min string, max string) (int64, error) - /* - Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 - */ - ZRemRangeByRank(key string, start int64, stop int64) (int64, error) - /* - Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 - */ - ZRemRangeByScore(key string, min string, max string) (int64, error) - /* - Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE - */ - ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd - /* - Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 - */ - ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd - /* - Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 - */ - ZRevRank(key string, member string) (int64, error) - /* - Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 - */ - ZScore(key string, member string) (float64, error) - /* - Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE - */ - ZUnionStore(dest string, store *redis.ZStore) (int64, error) - /* - Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) - */ - ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) + ZAdd(key string, members ...*redis.Z) (err error) + ZCard(key string) (result int64, err error) + ZCount(key string, min string, max string) (result int64, err error) + ZIncrBy(key string, increment float64, member string) (result float64, err error) + ZInterStore(destination string, store *redis.ZStore) (result int64, err error) + ZLexCount(key string, min string, max string) (result int64, err error) + ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) + ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) + ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) + ZRank(key string, member string) (result int64, err error) + ZRem(key string, members ...interface{}) (result int64, err error) + ZRemRangeByLex(key string, min string, max string) (result int64, err error) + ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) + ZRemRangeByScore(key string, min string, max string) (result int64, err error) + ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) + ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) + ZRevRank(key string, member string) (result int64, err error) + ZScore(key string, member string) (result float64, err error) + ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) + ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) } ISys interface { @@ -444,52 +162,55 @@ func Encode(value interface{}) (result []byte, err error) { func Decode(value []byte, result interface{}) (err error) { return defsys.Decode(value, result) } -func Delete(key ...string) (int64, error) { - return defsys.Delete(key...) +func Delete(key string) (err error) { + return defsys.Delete(key) } -func Exists(key ...string) (int64, error) { - return defsys.Exists(key...) +func ExistsKey(key string) (iskeep bool, err error) { + return defsys.ExistsKey(key) + } -func Expire(key string, expire time.Duration) (bool, error) { - return defsys.Expire(key, expire) +func ExpireKey(key string, expire int) (err error) { + return defsys.ExpireKey(key, expire) } -func ExpireAt(key string, expire time.Time) (bool, error) { - return defsys.ExpireAt(key, expire) +func ExpireatKey(key string, expire_unix int64) (err error) { + return defsys.ExpireatKey(key, expire_unix) } -func PExpire(key string, expire time.Duration) (bool, error) { - return defsys.PExpire(key, expire) +func Pexpirekey(key string, expire int) (err error) { + return defsys.Pexpirekey(key, expire) } -func PExpireAt(key string, expire time.Time) (bool, error) { - return defsys.PExpireAt(key, expire) +func PexpireatKey(key string, expire_unix int64) (err error) { + return defsys.PexpireatKey(key, expire_unix) } -func Persist(key string) (bool, error) { - return defsys.Persist(key) +func PersistKey(key string) (err error) { + return defsys.PersistKey(key) } -func PTTL(key string) (surplus time.Duration, err error) { - return defsys.PTTL(key) +func PttlKey(key string) (leftexpire int64, err error) { + return defsys.PttlKey(key) } -func TTL(key string) (surplus time.Duration, err error) { - return defsys.TTL(key) +func TtlKey(key string) (leftexpire int64, err error) { + return defsys.TtlKey(key) } -func Rename(oldkey string, newkey string) (string, error) { - return defsys.Rename(oldkey, newkey) +func RenameKye(oldkey string, newkey string) (err error) { + return defsys.RenameKye(oldkey, newkey) } -func RenameNX(oldkey string, newkey string) (bool, error) { - return defsys.RenameNX(oldkey, newkey) +func RenamenxKey(oldkey string, newkey string) (err error) { + return defsys.RenamenxKey(oldkey, newkey) } func Keys(pattern string) (keys []string, err error) { return defsys.Keys(pattern) } + +///获取键类型 func Type(key string) (ty string, err error) { return defsys.Type(key) } /*String*/ -func Set(key string, v interface{}, expiration time.Duration) (string, error) { - return defsys.Set(key, v, expiration) +func Set(key string, value interface{}, expiration time.Duration) (err error) { + return defsys.Set(key, value, expiration) } -func SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { - return defsys.SetNX(key, v, expiration) +func SetNX(key string, value interface{}) (result int64, err error) { + return defsys.SetNX(key, value) } func MSet(keyvalues map[string]interface{}) (err error) { return defsys.MSet(keyvalues) @@ -497,33 +218,36 @@ func MSet(keyvalues map[string]interface{}) (err error) { func MSetNX(keyvalues map[string]interface{}) (err error) { return defsys.MSetNX(keyvalues) } -func Incr(key string) (int64, error) { +func Incr(key string) (err error) { return defsys.Incr(key) } -func IncrBY(key string, value int64) (int64, error) { +func IncrBY(key string, value int) (err error) { return defsys.IncrBY(key, value) } -func Incrbyfloat(key string, value float64) (float64, error) { +func Incrbyfloat(key string, value float32) (err error) { return defsys.Incrbyfloat(key, value) } -func Decr(key string, value int) (int64, error) { +func Decr(key string, value int) (err error) { return defsys.Decr(key, value) } -func DecrBy(key string, value int64) (int64, error) { +func DecrBy(key string, value int) (err error) { return defsys.DecrBy(key, value) } -func Append(key string, value string) (err error) { +func Append(key string, value interface{}) (err error) { return defsys.Append(key, value) } -func Get(key string, v interface{}) (err error) { - return defsys.Get(key, v) +func Get(key string, value interface{}) (err error) { + return defsys.Get(key, value) } -func GetSet(key string, v interface{}, result interface{}) (err error) { - return defsys.GetSet(key, v, result) +func GetSet(key string, value interface{}, result interface{}) (err error) { + return defsys.GetSet(key, value, result) } -func MGet(keys ...string) *redis.SliceCmd { +func MGet(keys ...string) (result []string, err error) { return defsys.MGet(keys...) } +func INCRBY(key string, amount int64) (result int64, err error) { + return defsys.INCRBY(key, amount) +} /*Lock*/ func NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error) { @@ -538,46 +262,46 @@ func UnLock(key string) (err error) { } /*List*/ -func Lindex(key string, index int64, v interface{}) (err error) { - return defsys.Lindex(key, index, v) +func Lindex(key string, value interface{}) (err error) { + return defsys.Lindex(key, value) } -func Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { - return defsys.Linsert(key, isbefore, pivot, value) +func Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { + return defsys.Linsert(key, isbefore, tager, value) } -func Llen(key string) (int64, error) { +func Llen(key string) (result int, err error) { return defsys.Llen(key) } func LPop(key string, value interface{}) (err error) { return defsys.LPop(key, value) } -func LPush(key string, values ...interface{}) (int64, error) { +func LPush(key string, values ...interface{}) (err error) { return defsys.LPush(key, values...) } -func LPushX(key string, values ...interface{}) (int64, error) { +func LPushX(key string, values ...interface{}) (err error) { return defsys.LPushX(key, values...) } -func LRange(key string, start int64, end int64) *redis.StringSliceCmd { - return defsys.LRange(key, start, end) +func LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { + return defsys.LRange(key, start, end, valuetype) } -func LRem(key string, count int64, v interface{}) (int64, error) { - return defsys.LRem(key, count, v) +func LRem(key string, count int, target interface{}) (err error) { + return defsys.LRem(key, count, target) } -func LSet(key string, index int64, v interface{}) (string, error) { - return defsys.LSet(key, index, v) +func LSet(key string, index int, value interface{}) (err error) { + return defsys.LSet(key, index, value) } -func LTrim(key string, start int64, stop int64) (string, error) { - return defsys.LTrim(key, start, stop) +func Ltrim(key string, start, stop int) (err error) { + return defsys.Ltrim(key, start, stop) } -func RPop(key string, v interface{}) (err error) { - return defsys.RPop(key, v) +func Rpop(key string, value interface{}) (err error) { + return defsys.Rpop(key, value) } func RPopLPush(oldkey string, newkey string, value interface{}) (err error) { return defsys.RPopLPush(oldkey, newkey, value) } -func RPush(key string, values ...interface{}) (int64, error) { +func RPush(key string, values ...interface{}) (err error) { return defsys.RPush(key, values...) } -func RPushX(key string, values ...interface{}) (int64, error) { +func RPushX(key string, values ...interface{}) (err error) { return defsys.RPushX(key, values...) } @@ -591,23 +315,23 @@ func HExists(key string, field string) (result bool, err error) { func HGet(key string, field string, value interface{}) (err error) { return defsys.HGet(key, field, value) } -func HGetAll(key string, v interface{}) (err error) { - return defsys.HGetAll(key, v) +func HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { + return defsys.HGetAll(key, valuetype) } -func HIncrBy(key string, field string, value int64) (err error) { +func HIncrBy(key string, field string, value int) (err error) { return defsys.HIncrBy(key, field, value) } -func HIncrByFloat(key string, field string, value float64) (err error) { +func HIncrByFloat(key string, field string, value float32) (err error) { return defsys.HIncrByFloat(key, field, value) } func Hkeys(key string) (result []string, err error) { return defsys.Hkeys(key) } -func Hlen(key string) (result int64, err error) { +func Hlen(key string) (result int, err error) { return defsys.Hlen(key) } -func HMGet(key string, fields ...string) *redis.SliceCmd { - return defsys.HMGet(key, fields...) +func HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { + return defsys.HMGet(key, valuetype, fields...) } func HMSet(key string, value map[string]interface{}) (err error) { return defsys.HMSet(key, value) @@ -620,54 +344,54 @@ func HSetNX(key string, field string, value interface{}) (err error) { } /*Set*/ -func SAdd(key string, values ...interface{}) (int64, error) { +func SAdd(key string, values ...interface{}) (err error) { return defsys.SAdd(key, values...) } func SCard(key string) (result int64, err error) { return defsys.SCard(key) } -func SDiff(keys ...string) *redis.StringSliceCmd { - return defsys.SDiff(keys...) +func SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + return defsys.SDiff(valuetype, keys...) } func SDiffStore(destination string, keys ...string) (result int64, err error) { return defsys.SDiffStore(destination, keys...) } -func SInter(keys ...string) *redis.StringSliceCmd { - return defsys.SInter(keys...) +func SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + return defsys.SInter(valuetype, keys...) } func SInterStore(destination string, keys ...string) (result int64, err error) { return defsys.SInterStore(destination, keys...) } -func SIsMember(key string, value interface{}) (iskeep bool, err error) { - return defsys.SIsMember(key, value) +func Sismember(key string, value interface{}) (iskeep bool, err error) { + return defsys.Sismember(key, value) } -func SMembers(key string) *redis.StringSliceCmd { - return defsys.SMembers(key) +func SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { + return defsys.SMembers(valuetype, key) } func SMove(source string, destination string, member interface{}) (result bool, err error) { return defsys.SMove(source, destination, member) } -func SPop(key string) (result string, err error) { - return defsys.SPop(key) +func Spop(key string) (result string, err error) { + return defsys.Spop(key) } -func SRandMember(key string) (string, error) { - return defsys.SRandMember(key) +func Srandmember(key string) (result string, err error) { + return defsys.Srandmember(key) } func SRem(key string, members ...interface{}) (result int64, err error) { return defsys.SRem(key, members...) } -func SUnion(keys ...string) *redis.StringSliceCmd { - return defsys.SUnion(keys...) +func SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + return defsys.SUnion(valuetype, keys...) } -func SUnionStore(destination string, keys ...string) (int64, error) { - return defsys.SUnionStore(destination, keys...) +func Sunionstore(destination string, keys ...string) (result int64, err error) { + return defsys.Sunionstore(destination, keys...) } -func SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { - return defsys.SScan(key, _cursor, match, count) +func Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { + return defsys.Sscan(key, _cursor, match, count) } /*ZSet*/ -func ZAdd(key string, members ...*redis.Z) (int64, error) { +func ZAdd(key string, members ...*redis.Z) (err error) { return defsys.ZAdd(key, members...) } func ZCard(key string) (result int64, err error) { @@ -685,14 +409,14 @@ func ZInterStore(destination string, store *redis.ZStore) (result int64, err err func ZLexCount(key string, min string, max string) (result int64, err error) { return defsys.ZLexCount(key, min, max) } -func ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { - return defsys.ZRange(key, start, stop) +func ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + return defsys.ZRange(valuetype, key, start, stop) } -func ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return defsys.ZRangeByLex(key, opt) +func ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + return defsys.ZRangeByLex(valuetype, key, opt) } -func ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return defsys.ZRangeByScore(key, opt) +func ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + return defsys.ZRangeByScore(valuetype, key, opt) } func ZRank(key string, member string) (result int64, err error) { return defsys.ZRank(key, member) @@ -709,11 +433,11 @@ func ZRemRangeByRank(key string, start int64, stop int64) (result int64, err err func ZRemRangeByScore(key string, min string, max string) (result int64, err error) { return defsys.ZRemRangeByScore(key, min, max) } -func ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { - return defsys.ZRevRange(key, start, stop) +func ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + return defsys.ZRevRange(valuetype, key, start, stop) } -func ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return defsys.ZRevRangeByScore(key, opt) +func ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + return defsys.ZRevRangeByScore(valuetype, key, opt) } func ZRevRank(key string, member string) (result int64, err error) { return defsys.ZRevRank(key, member) diff --git a/lego/sys/redis/redis.go b/lego/sys/redis/redis.go index b5f0d791a..82594be04 100644 --- a/lego/sys/redis/redis.go +++ b/lego/sys/redis/redis.go @@ -2,10 +2,12 @@ package redis import ( "context" - "encoding/json" "fmt" + "reflect" "time" + jsoniter "github.com/json-iterator/go" + "go_dreamfactory/lego/sys/redis/cluster" "go_dreamfactory/lego/sys/redis/single" @@ -74,12 +76,12 @@ func (this *Redis) UnLock(key string) (err error) { ///数据编码 func (this *Redis) Encode(value interface{}) (result []byte, err error) { if this.options.RedisStorageType == JsonData { - result, err = json.Marshal(value) + result, err = jsoniter.Marshal(value) } else { if _, ok := value.(proto.Message); ok { result, err = proto.Marshal(value.(proto.Message)) } else { - result, err = json.Marshal(value) + result, err = jsoniter.Marshal(value) } } return @@ -87,51 +89,51 @@ func (this *Redis) Encode(value interface{}) (result []byte, err error) { func (this *Redis) Decode(value []byte, result interface{}) (err error) { if this.options.RedisStorageType == JsonData { - err = json.Unmarshal(value, result) + err = jsoniter.Unmarshal(value, result) } else { if _, ok := result.(proto.Message); ok { err = proto.Unmarshal(value, result.(proto.Message)) } else { - err = json.Unmarshal(value, result) + err = jsoniter.Unmarshal(value, result) } } return } -func (this *Redis) Delete(key ...string) (int64, error) { - return this.client.Delete(key...) +func (this *Redis) Delete(key string) (err error) { + return this.client.Delete(key) } -func (this *Redis) Exists(key ...string) (int64, error) { - return this.client.Exists(key...) +func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { + return this.client.ExistsKey(key) } -func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { - return this.client.Expire(key, expire) +func (this *Redis) ExpireKey(key string, expire int) (err error) { + return this.client.ExpireKey(key, expire) } -func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { - return this.client.ExpireAt(key, expire) +func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { + return this.client.ExpireatKey(key, expire_unix) } -func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { - return this.client.PExpire(key, expire) +func (this *Redis) Pexpirekey(key string, expire int) (err error) { + return this.client.Pexpirekey(key, expire) } -func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { - return this.client.PExpireAt(key, expire) +func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { + return this.client.PexpireatKey(key, expire_unix) } -func (this *Redis) Persist(key string) (bool, error) { - return this.client.Persist(key) +func (this *Redis) PersistKey(key string) (err error) { + return this.client.PersistKey(key) } -func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { - return this.client.PTTL(key) +func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { + return this.client.PttlKey(key) } -func (this *Redis) TTL(key string) (surplus time.Duration, err error) { - return this.client.TTL(key) +func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { + return this.client.TtlKey(key) } -func (this *Redis) Rename(oldkey string, newkey string) (string, error) { - return this.client.Rename(oldkey, newkey) +func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { + return this.client.RenameKye(oldkey, newkey) } -func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { - return this.client.RenameNX(oldkey, newkey) +func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { + return this.client.RenamenxKey(oldkey, newkey) } func (this *Redis) Keys(pattern string) (keys []string, err error) { return this.client.Keys(pattern) @@ -143,11 +145,11 @@ func (this *Redis) Type(key string) (ty string, err error) { } /*String*/ -func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { - return this.client.Set(key, v, expiration) +func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { + return this.client.Set(key, value, expiration) } -func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { - return this.client.SetNX(key, v, expiration) +func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { + return this.client.SetNX(key, value) } func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { return this.client.MSet(keyvalues) @@ -155,22 +157,22 @@ func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { return this.client.MSetNX(keyvalues) } -func (this *Redis) Incr(key string) (int64, error) { +func (this *Redis) Incr(key string) (err error) { return this.client.Incr(key) } -func (this *Redis) IncrBY(key string, value int64) (int64, error) { +func (this *Redis) IncrBY(key string, value int) (err error) { return this.client.IncrBY(key, value) } -func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { +func (this *Redis) Incrbyfloat(key string, value float32) (err error) { return this.client.Incrbyfloat(key, value) } -func (this *Redis) Decr(key string, value int) (int64, error) { +func (this *Redis) Decr(key string, value int) (err error) { return this.client.Decr(key, value) } -func (this *Redis) DecrBy(key string, value int64) (int64, error) { +func (this *Redis) DecrBy(key string, value int) (err error) { return this.client.DecrBy(key, value) } -func (this *Redis) Append(key string, value string) (err error) { +func (this *Redis) Append(key string, value interface{}) (err error) { return this.client.Append(key, value) } func (this *Redis) Get(key string, value interface{}) (err error) { @@ -179,51 +181,54 @@ func (this *Redis) Get(key string, value interface{}) (err error) { 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 { +func (this *Redis) MGet(keys ...string) (result []string, err error) { return this.client.MGet(keys...) } +func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { + return this.client.INCRBY(key, amount) +} /*List*/ -func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { - return this.client.Lindex(key, index, v) +func (this *Redis) Lindex(key string, value interface{}) (err error) { + return this.client.Lindex(key, value) } -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) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { + return this.client.Linsert(key, isbefore, tager, value) } -func (this *Redis) Llen(key string) (int64, error) { +func (this *Redis) Llen(key string) (result int, err 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) { +func (this *Redis) LPush(key string, values ...interface{}) (err error) { return this.client.LPush(key, values...) } -func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { +func (this *Redis) LPushX(key string, values ...interface{}) (err 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) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { + return this.client.LRange(key, start, end, valuetype) } -func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { - return this.client.LRem(key, count, v) +func (this *Redis) LRem(key string, count int, target interface{}) (err error) { + return this.client.LRem(key, count, target) } -func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { - return this.client.LSet(key, index, v) +func (this *Redis) LSet(key string, index int, value interface{}) (err error) { + return this.client.LSet(key, index, value) } -func (this *Redis) LTrim(key string, start int64, stop int64) (string, error) { - return this.client.LTrim(key, start, stop) +func (this *Redis) Ltrim(key string, start, stop int) (err 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) 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) { +func (this *Redis) RPush(key string, values ...interface{}) (err error) { return this.client.RPush(key, values...) } -func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { +func (this *Redis) RPushX(key string, values ...interface{}) (err error) { return this.client.RPushX(key, values...) } @@ -237,23 +242,23 @@ func (this *Redis) HExists(key string, field string) (result bool, err error) { 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) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { + return this.client.HGetAll(key, valuetype) } -func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { +func (this *Redis) HIncrBy(key string, field string, value int) (err error) { return this.client.HIncrBy(key, field, value) } -func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { +func (this *Redis) HIncrByFloat(key string, field string, value float32) (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) { +func (this *Redis) Hlen(key string) (result int, 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) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { + return this.client.HMGet(key, valuetype, fields...) } func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) { return this.client.HMSet(key, value) @@ -266,54 +271,54 @@ func (this *Redis) HSetNX(key string, field string, value interface{}) (err erro } /*Set*/ -func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { +func (this *Redis) SAdd(key string, values ...interface{}) (err 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) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + return this.client.SDiff(valuetype, 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) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + return this.client.SInter(valuetype, 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) 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) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { + return this.client.SMembers(valuetype, 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) Spop(key string) (result string, err error) { + return this.client.Spop(key) } -func (this *Redis) SRandMember(key string) (string, error) { - return this.client.SRandMember(key) +func (this *Redis) Srandmember(key string) (result string, err 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) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + return this.client.SUnion(valuetype, keys...) } -func (this *Redis) SUnionStore(destination string, keys ...string) (result int64, err error) { - return this.client.SUnionStore(destination, 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) +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) { +func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { return this.client.ZAdd(key, members...) } func (this *Redis) ZCard(key string) (result int64, err error) { @@ -331,14 +336,14 @@ func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result 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) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + return this.client.ZRange(valuetype, key, start, stop) } -func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRangeByLex(key, opt) +func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + return this.client.ZRangeByLex(valuetype, key, opt) } -func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRangeByScore(key, opt) +func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + return this.client.ZRangeByScore(valuetype, key, opt) } func (this *Redis) ZRank(key string, member string) (result int64, err error) { return this.client.ZRank(key, member) @@ -355,11 +360,11 @@ func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result 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) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + return this.client.ZRevRange(valuetype, key, start, stop) } -func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRevRangeByScore(key, opt) +func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + return this.client.ZRevRangeByScore(valuetype, key, opt) } func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { return this.client.ZRevRank(key, member) diff --git a/lego/sys/redis/single/core.go b/lego/sys/redis/single/core.go index 18f218336..8ea81ce6e 100644 --- a/lego/sys/redis/single/core.go +++ b/lego/sys/redis/single/core.go @@ -87,6 +87,6 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) { //锁 func (this *Redis) UnLock(key string) (err error) { - _, err = this.Delete(key) + err = this.Delete(key) return } diff --git a/lego/sys/redis/single/hash.go b/lego/sys/redis/single/hash.go index df9e48057..91204da1c 100644 --- a/lego/sys/redis/single/hash.go +++ b/lego/sys/redis/single/hash.go @@ -1,6 +1,8 @@ package single import ( + "reflect" + "github.com/go-redis/redis/v8" ) @@ -8,7 +10,13 @@ import ( Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段,不存在的字段将被忽略 */ func (this *Redis) HDel(key string, fields ...string) (err error) { - err = this.client.HDel(this.getContext(), key, fields...).Err() + agrs := make([]interface{}, 0) + agrs = append(agrs, "HDEL") + agrs = append(agrs, key) + for _, v := range fields { + agrs = append(agrs, v) + } + err = this.client.Do(this.getContext(), agrs...).Err() return } @@ -16,15 +24,18 @@ func (this *Redis) HDel(key string, fields ...string) (err error) { Redis Hexists 命令用于查看哈希表的指定字段是否存在 */ func (this *Redis) HExists(key string, field string) (result bool, err error) { - this.client.HExists(this.getContext(), key, field) + result, err = this.client.Do(this.getContext(), "HEXISTS", key, field).Bool() return } /* Redis Hget 命令用于返回哈希表中指定字段的值 */ -func (this *Redis) HGet(key string, field string, v interface{}) (err error) { - err = this.client.HGet(this.getContext(), key, field).Scan(v) +func (this *Redis) HGet(key string, field string, value interface{}) (err error) { + var resultvalue string + if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) { + err = this.Decode([]byte(resultvalue), value) + } return } @@ -32,8 +43,19 @@ func (this *Redis) HGet(key string, field string, v interface{}) (err error) { Redis Hgetall 命令用于返回哈希表中,所有的字段和值。 在返回值里,紧跟每个字段名(field name)之后是字段的值(value),所以返回值的长度是哈希表大小的两倍 */ -func (this *Redis) HGetAll(key string, v interface{}) (err error) { - err = this.client.HGetAll(this.getContext(), key).Scan(v) +func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { + cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) + this.client.Process(this.getContext(), cmd) + var _result []string + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } return } @@ -45,8 +67,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。 对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误。 本操作的值被限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { - err = this.client.HIncrBy(this.getContext(), key, field, value).Err() +func (this *Redis) HIncrBy(key string, field string, value int) (err error) { + err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() return } @@ -54,8 +76,8 @@ func (this *Redis) HIncrBy(key string, field string, value int64) (err error) { Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值。 如果指定的字段不存在,那么在执行命令前,字段的值被初始化为 0 */ -func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) { - err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err() +func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { + err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() return } @@ -63,21 +85,17 @@ func (this *Redis) HIncrByFloat(key string, field string, value float64) (err er Redis Hkeys 命令用于获取哈希表中的所有域(field) */ func (this *Redis) Hkeys(key string) (result []string, err error) { - var cmd *redis.StringSliceCmd - cmd = this.client.HKeys(this.getContext(), key) - result = cmd.Val() - err = cmd.Err() + cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() return } /* Redis Hlen 命令用于获取哈希表中字段的数量 */ -func (this *Redis) Hlen(key string) (result int64, err error) { - var cmd *redis.IntCmd - cmd = this.client.HLen(this.getContext(), key) - result = cmd.Val() - err = cmd.Err() +func (this *Redis) Hlen(key string) (result int, err error) { + result, err = this.client.Do(this.getContext(), "HLEN", key).Int() return } @@ -85,8 +103,26 @@ func (this *Redis) Hlen(key string) (result int64, err error) { Redis Hmget 命令用于返回哈希表中,一个或多个给定字段的值。 如果指定的字段不存在于哈希表,那么返回一个 nil 值 */ -func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd { - return this.client.HMGet(this.getContext(), key, fields...) +func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "HMGET") + agrs = append(agrs, key) + for _, v := range fields { + agrs = append(agrs, v) + } + cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) + this.client.Process(this.getContext(), cmd) + var _result []string + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* @@ -112,7 +148,10 @@ Redis Hset 命令用于为哈希表中的字段赋值 如果字段已经存在于哈希表中,旧值将被覆盖 */ func (this *Redis) HSet(key string, field string, value interface{}) (err error) { - err = this.client.HSet(this.getContext(), key, field, value).Err() + var resultvalue []byte + if resultvalue, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err() + } return } @@ -123,6 +162,9 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值 如果 key 不存在,一个新哈希表被创建并执行 HSETNX 命令 */ func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { - err = this.client.HSetNX(this.getContext(), key, field, value).Err() + var resultvalue []byte + if resultvalue, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err() + } return } diff --git a/lego/sys/redis/single/key.go b/lego/sys/redis/single/key.go index bc8d07f99..8d6b46ec5 100644 --- a/lego/sys/redis/single/key.go +++ b/lego/sys/redis/single/key.go @@ -1,69 +1,83 @@ package single import ( - "time" + "github.com/go-redis/redis/v8" ) /* Key *******************************************************************************/ ///删除redis key -func (this *Redis) Delete(key ...string) (int64, error) { - return this.client.Del(this.getContext(), key...).Result() +func (this *Redis) Delete(key string) (err error) { + err = this.client.Do(this.getContext(), "DEL", key).Err() + return } ///判断是否存在key -func (this *Redis) Exists(key ...string) (int64, error) { - return this.client.Exists(this.getContext(), key...).Result() +func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { + iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() + return } ///设置key的过期时间 单位以秒级 -func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { - return this.client.Expire(this.getContext(), key, expire).Result() +func (this *Redis) ExpireKey(key string, expire int) (err error) { + err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() + return } ///设置key的过期时间戳 秒级时间戳 -func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { - return this.client.ExpireAt(this.getContext(), key, expire).Result() +func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { + err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() + return } ///设置key的过期时间 单位以毫秒级 -func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { - return this.client.PExpire(this.getContext(), key, expire).Result() +func (this *Redis) Pexpirekey(key string, expire int) (err error) { + err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() + return } ///设置key的过期时间戳 单位以豪秒级 -func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { - return this.client.PExpireAt(this.getContext(), key, expire).Result() +func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { + err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() + return } ///移除Key的过期时间 -func (this *Redis) Persist(key string) (bool, error) { - return this.client.Persist(this.getContext(), key).Result() +func (this *Redis) PersistKey(key string) (err error) { + err = this.client.Do(this.getContext(), "PERSIST", key).Err() + return } ///获取key剩余过期时间 单位毫秒 -func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { - return this.client.PTTL(this.getContext(), key).Result() +func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { + leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() + return } ///获取key剩余过期时间 单位秒 -func (this *Redis) TTL(key string) (surplus time.Duration, err error) { - return this.client.TTL(this.getContext(), key).Result() +func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { + leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() + return } ///重命名Key -func (this *Redis) Rename(oldkey string, newkey string) (string, error) { - return this.client.Rename(this.getContext(), oldkey, newkey).Result() +func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { + err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() + return } ///重命名key 在新的 key 不存在时修改 key 的名称 -func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { - return this.client.RenameNX(this.getContext(), oldkey, newkey).Result() +func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { + err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() + return } ///判断是否存在key pattern:key* func (this *Redis) Keys(pattern string) (keys []string, err error) { - return this.client.Keys(this.getContext(), pattern).Result() + cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) + this.client.Process(this.getContext(), cmd) + keys, err = cmd.Result() + return } ///获取键类型 diff --git a/lego/sys/redis/single/list.go b/lego/sys/redis/single/list.go index 3adfa9907..18a0011d8 100644 --- a/lego/sys/redis/single/list.go +++ b/lego/sys/redis/single/list.go @@ -1,14 +1,23 @@ package single import ( + "fmt" + "reflect" + "github.com/go-redis/redis/v8" ) /* Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { - return this.client.LIndex(this.getContext(), key, index).Scan(v) +func (this *Redis) Lindex(key string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "LINDEX", key).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* @@ -16,55 +25,90 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { - op := "BEFORE" - if !isbefore { - op = "AFTER" +func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { + var ( + tagervalue []byte + resultvalue []byte + ) + if tagervalue, err = this.Encode(tager); err == nil { + if resultvalue, err = this.Encode(value); err == nil { + if isbefore { + err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err() + } else { + err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err() + } + } } - return this.client.LInsert(this.getContext(), key, op, pivot, value).Result() + return } /* Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Llen(key string) (int64, error) { - return this.client.LLen(this.getContext(), key).Result() +func (this *Redis) Llen(key string) (result int, err error) { + result, err = this.client.Do(this.getContext(), "LLEN", key).Int() + return } /* Redis Lpop 命令用于移除并返回列表的第一个元素 */ -func (this *Redis) LPop(key string, v interface{}) (err error) { - return this.client.LPop(this.getContext(), key).Scan(v) +func (this *Redis) LPop(key string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "LPOP", key).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 */ -func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { - return this.client.LPush(this.getContext(), key, values...).Result() +func (this *Redis) LPush(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "LPUSH") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 */ -func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { - // agrs := make([]interface{}, 0) - // agrs = append(agrs, "LPUSHX") - // for _, v := range values { - // result, _ := this.Encode(v) - // agrs = append(agrs, result) - // } - // err = this.client.Do(this.getContext(), agrs...).Err() - return this.client.LPushX(this.getContext(), key, values...).Result() +func (this *Redis) LPushX(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "LPUSHX") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd { - return this.client.LRange(this.getContext(), key, start, end) +func (this *Redis) LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { + var _result []string + cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end) + this.client.Process(this.getContext(), cmd) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* @@ -74,16 +118,24 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素, count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 count = 0 : 移除表中所有与 VALUE 相等的值 */ -func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { - return this.client.LRem(this.getContext(), key, count, v).Result() +func (this *Redis) LRem(key string, count int, target interface{}) (err error) { + var resultvalue []byte + if resultvalue, err = this.Encode(target); err == nil { + err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err() + } + return } /* -Redis LSet 通过索引来设置元素的值。 +Redis Lset 通过索引来设置元素的值。 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 */ -func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { - return this.client.LSet(this.getContext(), key, index, v).Result() +func (this *Redis) LSet(key string, index int, value interface{}) (err error) { + var resultvalue []byte + if resultvalue, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err() + } + return } /* @@ -91,22 +143,35 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) LTrim(key string, start, stop int64) (string, error) { - return this.client.LTrim(this.getContext(), key, start, stop).Result() +func (this *Redis) Ltrim(key string, start, stop int) (err error) { + err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() + return } /* Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 */ -func (this *Redis) RPop(key string, v interface{}) (err error) { - return this.client.RPop(this.getContext(), key).Scan(v) +func (this *Redis) Rpop(key string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "RPOP", key).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 */ -func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) { - return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v) +func (this *Redis) RPopLPush(oldkey string, newkey string, value interface{}) (err error) { + var data string + if data = this.client.Do(this.getContext(), "RPOPLPUSH", oldkey, newkey).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), value) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + return } /* @@ -114,13 +179,27 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边) 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 */ -func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { - return this.client.RPush(this.getContext(), key, values...).Result() +func (this *Redis) RPush(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "RPUSH") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 */ -func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { - return this.client.RPushX(this.getContext(), key, values...).Result() +func (this *Redis) RPushX(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "RPUSHX") + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } diff --git a/lego/sys/redis/single/set.go b/lego/sys/redis/single/set.go index beda0d4b2..2e8c86d43 100644 --- a/lego/sys/redis/single/set.go +++ b/lego/sys/redis/single/set.go @@ -1,23 +1,30 @@ package single -import ( - "github.com/go-redis/redis/v8" -) +import "reflect" /* Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 当集合 key 不是集合类型时,返回一个错误。 */ -func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { - return this.client.SAdd(this.getContext(), key, values...).Result() +func (this *Redis) SAdd(key string, values ...interface{}) (err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "SADD") + agrs = append(agrs, key) + for _, v := range values { + result, _ := this.Encode(v) + agrs = append(agrs, result) + } + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* Redis Scard 命令返回集合中元素的数量 */ -func (this *Redis) SCard(key string) (int64, error) { - return this.client.SCard(this.getContext(), key).Result() +func (this *Redis) SCard(key string) (result int64, err error) { + result, err = this.client.SCard(this.getContext(), key).Result() + return } /* @@ -25,43 +32,79 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 实例: */ -func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { - return this.client.SDiff(this.getContext(), keys...) +func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SDiff(this.getContext(), keys...) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 */ -func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) { - return this.client.SDiffStore(this.getContext(), destination, keys...).Result() +func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { + result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() + return } /* -Redis SInter 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 +Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 */ -func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { - return this.client.SInter(this.getContext(), keys...) +func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SInter(this.getContext(), keys...) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 */ -func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) { - return this.client.SInterStore(this.getContext(), destination, keys...).Result() +func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { + result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() + return } /* Redis Sismember 命令判断成员元素是否是集合的成员 */ -func (this *Redis) SIsMember(key string, value interface{}) (bool, error) { - return this.client.SIsMember(this.getContext(), key, value).Result() +func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { + iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() + return } /* Redis Smembers 号召返回集合中的所有成员。 */ -func (this *Redis) SMembers(key string) *redis.StringSliceCmd { - return this.client.SMembers(this.getContext(), key) +func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SMembers(this.getContext(), key) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* @@ -71,16 +114,18 @@ SMOVE 是原子性操作。 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 当 source 或 destination 不是集合类型时,返回一个错误。 */ -func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) { - return this.client.SMove(this.getContext(), source, destination, member).Result() +func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { + result, err = this.client.SMove(this.getContext(), source, destination, member).Result() + return } /* Redis Spop命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 */ -func (this *Redis) SPop(key string) (string, error) { - return this.client.SPop(this.getContext(), key).Result() +func (this *Redis) Spop(key string) (result string, err error) { + result, err = this.client.SPop(this.getContext(), key).Result() + return } /* @@ -90,8 +135,9 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 */ -func (this *Redis) SRandMember(key string) (string, error) { - return this.client.SRandMember(this.getContext(), key).Result() +func (this *Redis) Srandmember(key string) (result string, err error) { + result, err = this.client.SRandMember(this.getContext(), key).Result() + return } /* @@ -99,27 +145,41 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在 当键不是集合类型,返回一个错误。 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 */ -func (this *Redis) SRem(key string, members ...interface{}) (int64, error) { - return this.client.SRem(this.getContext(), key, members...).Result() +func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { + result, err = this.client.SRem(this.getContext(), key, members...).Result() + return } /* Redis Sunion 命令返回给定集合的并集。 */ -func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { - return this.client.SUnion(this.getContext(), keys...) +func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { + var _result []string + cmd := this.client.SUnion(this.getContext(), keys...) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 */ -func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) { - return this.client.SUnionStore(this.getContext(), destination, keys...).Result() +func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { + result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() + return } /* Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 */ -func (this *Redis) SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { - return this.client.SScan(this.getContext(), key, _cursor, match, count).Result() +func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { + keys, cursor, err = this.client.SScan(this.getContext(), key, _cursor, match, count).Result() + return } diff --git a/lego/sys/redis/single/string.go b/lego/sys/redis/single/string.go index be6957d37..468d6026b 100644 --- a/lego/sys/redis/single/string.go +++ b/lego/sys/redis/single/string.go @@ -1,6 +1,7 @@ package single import ( + "fmt" "time" "github.com/go-redis/redis/v8" @@ -10,39 +11,54 @@ import ( /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 */ -func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { - return this.client.Set(this.getContext(), string(key), v, expiration).Result() +func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { + var result []byte + if result, err = this.Encode(value); err == nil { + err = this.client.Set(this.getContext(), string(key), result, expiration).Err() + } + return } /* 指定的 key 不存在时,为 key 设置指定的值 */ -func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { - return this.client.SetNX(this.getContext(), key, v, expiration).Result() +func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { + // var _value []byte + // if result, err = this.Encode(value); err == nil { + // err = this.client.Do(this.getContext(), "SETNX", key, result).Err() + cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() + // } + return } /* 同时设置一个或多个 key-value 对 */ -func (this *Redis) MSet(keyvalues map[string]interface{}) error { +func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { agrs := make([]interface{}, 0) agrs = append(agrs, "MSET") for k, v := range keyvalues { - agrs = append(agrs, k, v) + result, _ := this.Encode(v) + agrs = append(agrs, k, result) } - return this.client.Do(this.getContext(), agrs...).Err() + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 */ -func (this *Redis) MSetNX(keyvalues map[string]interface{}) error { +func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { agrs := make([]interface{}, 0) agrs = append(agrs, "MSETNX") for k, v := range keyvalues { - agrs = append(agrs, k, v) + result, _ := this.Encode(v) + agrs = append(agrs, k, result) } - return this.client.Do(this.getContext(), agrs...).Err() + err = this.client.Do(this.getContext(), agrs...).Err() + return } /* @@ -51,8 +67,9 @@ Redis Incr 命令将 key 中储存的数字值增一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内。 */ -func (this *Redis) Incr(key string) (int64, error) { - return this.client.Incr(this.getContext(), key).Result() +func (this *Redis) Incr(key string) (err error) { + err = this.client.Do(this.getContext(), "INCR", key).Err() + return } /* @@ -61,16 +78,18 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) IncrBY(key string, value int64) (int64, error) { - return this.client.IncrBy(this.getContext(), key, value).Result() +func (this *Redis) IncrBY(key string, value int) (err error) { + err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() + return } /* Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 */ -func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { - return this.client.IncrByFloat(this.getContext(), key, value).Result() +func (this *Redis) Incrbyfloat(key string, value float32) (err error) { + err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() + return } /* @@ -79,8 +98,9 @@ Redis Decr 命令将 key 中储存的数字值减一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) Decr(key string, value int) (int64, error) { - return this.client.Decr(this.getContext(), key).Result() +func (this *Redis) Decr(key string, value int) (err error) { + err = this.client.Do(this.getContext(), "DECR", key, value).Err() + return } /* @@ -89,8 +109,9 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) DecrBy(key string, value int64) (int64, error) { - return this.client.DecrBy(this.getContext(), key, value).Result() +func (this *Redis) DecrBy(key string, value int) (err error) { + err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() + return } /* @@ -98,28 +119,62 @@ Redis Append 命令用于为指定的 key 追加值。 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 */ -func (this *Redis) Append(key string, value string) (err error) { - this.client.Append(this.getContext(), key, value).Result() +func (this *Redis) Append(key string, value interface{}) (err error) { + var result []byte + if result, err = this.Encode(value); err == nil { + err = this.client.Do(this.getContext(), "APPEND", key, result).Err() + } return } /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 */ -func (this *Redis) Get(key string, v interface{}) (err error) { - return this.client.Get(this.getContext(), key).Scan(v) +func (this *Redis) Get(key string, value interface{}) (err error) { + var result []byte + if result, err = this.client.Get(this.getContext(), key).Bytes(); err == nil { + err = this.Decode(result, value) + } + return } /* 设置指定 key 的值,并返回 key 的旧值 */ -func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) { - return this.client.GetSet(this.getContext(), key, v).Scan(result) +func (this *Redis) GetSet(key string, value interface{}, result interface{}) (err error) { + var ( + data string + _value []byte + ) + if _value, err = this.Encode(value); err == nil { + if data = this.client.Do(this.getContext(), "GETSET", key, _value).String(); data != string(redis.Nil) { + err = this.Decode([]byte(data), result) + } else { + err = fmt.Errorf(string(redis.Nil)) + } + } + return } /* 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil */ -func (this *Redis) MGet(keys ...string) *redis.SliceCmd { - return this.client.MGet(this.getContext(), keys...) +func (this *Redis) MGet(keys ...string) (result []string, err error) { + agrs := make([]interface{}, 0) + agrs = append(agrs, "MGET") + for _, v := range keys { + agrs = append(agrs, v) + } + cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() + return +} + +///判断是否存在key pattern:key* +func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { + cmd := redis.NewIntCmd(this.getContext(), "INCRBY", key, amount) + this.client.Process(this.getContext(), cmd) + result, err = cmd.Result() + return } diff --git a/lego/sys/redis/single/zset.go b/lego/sys/redis/single/zset.go index 1bf044e86..cadf0f13e 100644 --- a/lego/sys/redis/single/zset.go +++ b/lego/sys/redis/single/zset.go @@ -1,145 +1,217 @@ package single import ( + "reflect" + "github.com/go-redis/redis/v8" ) /* Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 */ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { - return this.client.ZAdd(this.getContext(), key, members...).Result() +func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { + this.client.ZAdd(this.getContext(), key, members...) + return } /* Redis Zcard 用于计算集合中元素的数量。 */ -func (this *Redis) ZCard(key string) (int64, error) { - return this.client.ZCard(this.getContext(), key).Result() +func (this *Redis) ZCard(key string) (result int64, err error) { + result, err = this.client.ZCard(this.getContext(), key).Result() + return } /* Redis ZCount 用于计算集合中指定的范围内的数量 */ -func (this *Redis) ZCount(key string, min string, max string) (int64, error) { - return this.client.ZCount(this.getContext(), key, min, max).Result() +func (this *Redis) ZCount(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZCount(this.getContext(), key, min, max).Result() + return } /* Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment */ -func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) { - return this.client.ZIncrBy(this.getContext(), key, increment, member).Result() +func (this *Redis) ZIncrBy(key string, increment float64, member string) (result float64, err error) { + result, err = this.client.ZIncrBy(this.getContext(), key, increment, member).Result() + return } /* Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 */ -func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) { - return this.client.ZInterStore(this.getContext(), destination, store).Result() +func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { + result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() + return } /* Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 */ -func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) { - return this.client.ZLexCount(this.getContext(), key, min, max).Result() +func (this *Redis) ZLexCount(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZLexCount(this.getContext(), key, min, max).Result() + return } /* Redis ZRange 通过索引区间返回有序集合指定区间内的成员 */ -func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { - return this.client.ZRange(this.getContext(), key, start, stop) +func (this *Redis) ZRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRange(this.getContext(), key, start, stop) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRangeByLex 通过字典区间返回有序集合的成员 */ -func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRangeByLex(this.getContext(), key, opt) +func (this *Redis) ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRangeByLex(this.getContext(), key, opt) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 */ -func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRangeByScore(this.getContext(), key, opt) +func (this *Redis) ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRangeByScore(this.getContext(), key, opt) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRank 返回有序集合中指定成员的索引 */ -func (this *Redis) ZRank(key string, member string) (int64, error) { - return this.client.ZRank(this.getContext(), key, member).Result() +func (this *Redis) ZRank(key string, member string) (result int64, err error) { + result, err = this.client.ZRank(this.getContext(), key, member).Result() + return } /* Redis ZRem 移除有序集合中的一个或多个成员 */ -func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) { - return this.client.ZRem(this.getContext(), key, members...).Result() +func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { + result, err = this.client.ZRem(this.getContext(), key, members...).Result() + return } /* Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 */ -func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) { - return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() +func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() + return } /* Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 */ -func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) { - return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() +func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (result int64, err error) { + result, err = this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() + return } /* Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 */ -func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) { - return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() +func (this *Redis) ZRemRangeByScore(key string, min string, max string) (result int64, err error) { + result, err = this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() + return } /* Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE */ -func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { - return this.client.ZRevRange(this.getContext(), key, start, stop) +func (this *Redis) ZRevRange(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRevRange(this.getContext(), key, start, stop) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { - return this.client.ZRevRangeByScore(this.getContext(), key, opt) +func (this *Redis) ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { + var _result []string + cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt) + if _result, err = cmd.Result(); err == nil { + result = make([]interface{}, len(_result)) + for i, v := range _result { + temp := reflect.New(valuetype.Elem()).Interface() + if err = this.Decode([]byte(v), &temp); err == nil { + result[i] = temp + } + } + } + return } /* Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRank(key string, member string) (int64, error) { - return this.client.ZRevRank(this.getContext(), key, member).Result() +func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { + result, err = this.client.ZRevRank(this.getContext(), key, member).Result() + return } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZScore(key string, member string) (float64, error) { - return this.client.ZScore(this.getContext(), key, member).Result() +func (this *Redis) ZScore(key string, member string) (result float64, err error) { + result, err = this.client.ZScore(this.getContext(), key, member).Result() + return } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE */ -func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) { - return this.client.ZUnionStore(this.getContext(), dest, store).Result() +func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { + result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() + return } /* Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) */ -func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { - return this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() +func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { + keys, cursor, err = this.client.ZScan(this.getContext(), key, _cursor, match, count).Result() + return } diff --git a/lego/sys/redis/sys_test.go b/lego/sys/redis/sys_test.go index 9de853ab5..7d924ce30 100644 --- a/lego/sys/redis/sys_test.go +++ b/lego/sys/redis/sys_test.go @@ -21,7 +21,7 @@ func Test_SysIPV6(t *testing.T) { return } fmt.Printf("Redis:succ \n") - if _, err = redis.Set("liwei1dao", 123, -1); err != nil { + if err = redis.Set("liwei1dao", 123, -1); err != nil { fmt.Printf("Redis:err:%v \n", err) } } @@ -37,10 +37,10 @@ func Test_Redis_ExpireatKey(t *testing.T) { return } fmt.Printf("Redis:succ \n") - if _, err = redis.Set("liwei1dao", 123, -1); err != nil { + if err = redis.Set("liwei1dao", 123, -1); err != nil { fmt.Printf("Redis:err:%v \n", err) } - if _, err = redis.Expire("liwei1dao", time.Second*120); err != nil { + if err = redis.ExpireKey("liwei1dao", 120); err != nil { fmt.Printf("Redis:err:%v \n", err) } fmt.Printf("Redis:end \n") @@ -66,8 +66,8 @@ func Test_Redis_SetNX(t *testing.T) { wg.Add(20) for i := 0; i < 20; i++ { go func(index int) { - result, err := redis.SetNX("liwei1dao", index, -1) - fmt.Printf("Redis index:%d result:%v err:%v \n", index, result, err) + result, err := redis.SetNX("liwei1dao", index) + fmt.Printf("Redis index:%d result:%d err:%v \n", index, result, err) wg.Done() }(i) } diff --git a/lego/sys/rpcx/rpcx_test.go b/lego/sys/rpcx/rpcx_test.go index 4d468c986..9893ee54e 100644 --- a/lego/sys/rpcx/rpcx_test.go +++ b/lego/sys/rpcx/rpcx_test.go @@ -184,4 +184,10 @@ func Test_Regular(t *testing.T) { // str3 := "worker/!worker_1" // str4 := "worker/[worker_1,worker_2]" // str5 := "worker/![worker_1,worker_2]" + + // reg1 := regexp.MustCompile(`/`) + //根据规则提取关键信息 + // result1 := reg1.FindAllStringSubmatch(str1, -1) + // fmt.Println("result1 = ", result1) + // strings.Split() } diff --git a/modules/pack/cache_comp.go b/modules/pack/cache_comp.go index d9acd20f2..8000b93bc 100644 --- a/modules/pack/cache_comp.go +++ b/modules/pack/cache_comp.go @@ -7,6 +7,7 @@ import ( "go_dreamfactory/lego/sys/redis" "go_dreamfactory/modules" "go_dreamfactory/pb" + "reflect" "go.mongodb.org/mongo-driver/bson/primitive" ) @@ -30,8 +31,8 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD lists []interface{} temp map[string]interface{} ) - itmes = make([]*pb.DB_UserItemData, 0) - if err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), &itmes); err == nil { + if lists, err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), reflect.TypeOf(pb.DB_UserItemData{})); err == nil { + itmes = make([]*pb.DB_UserItemData, 0, len(lists)) for i, v := range lists { itmes[i] = v.(*pb.DB_UserItemData) } diff --git a/sys/cache/friend.go b/sys/cache/friend.go index ffa3139e1..628096486 100644 --- a/sys/cache/friend.go +++ b/sys/cache/friend.go @@ -26,7 +26,7 @@ type IFriend interface { func (this *Cache) Friend_Update(data *pb.Cache_FriendData) (err error) { if err = db.Defsys.Friend_SaveOrUpdate(data); err == nil { //更新缓存 - _, err = this.redis.Set(getRdsFriendKey(data.UserId), data, 0) + err = this.redis.Set(getRdsFriendKey(data.UserId), data, 0) } return } diff --git a/sys/cache/user.go b/sys/cache/user.go index f28b63fb9..7dc2e9e6f 100644 --- a/sys/cache/user.go +++ b/sys/cache/user.go @@ -20,7 +20,7 @@ type IUser interface { func (this *Cache) Update(data *pb.Cache_UserData) (err error) { err = db.Defsys.User_Update(data.UserData) if err == nil { - _, err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.Uid), data, -1) + err = this.redis.Set(fmt.Sprintf(Redis_UserCache, data.UserData.Uid), data, -1) } return }