From a6e2814016e2b745bcfda34d22c1ebe8ee9729a4 Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 13 Jun 2022 16:45:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96redis=20=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=BA=93=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lego/sys/redis/cluster/core.go | 2 +- lego/sys/redis/cluster/key.go | 63 ++-- lego/sys/redis/cluster/list.go | 157 ++------ lego/sys/redis/cluster/set.go | 126 ++----- lego/sys/redis/cluster/string.go | 111 ++---- lego/sys/redis/cluster/zset.go | 152 ++------ lego/sys/redis/core.go | 608 ++++++++++++++++++++++--------- lego/sys/redis/redis.go | 168 +++++---- lego/sys/redis/single/core.go | 2 +- lego/sys/redis/single/key.go | 62 ++-- lego/sys/redis/single/list.go | 157 ++------ lego/sys/redis/single/set.go | 128 ++----- lego/sys/redis/single/string.go | 111 ++---- lego/sys/redis/single/zset.go | 152 ++------ lego/sys/redis/sys_test.go | 10 +- 15 files changed, 860 insertions(+), 1149 deletions(-) diff --git a/lego/sys/redis/cluster/core.go b/lego/sys/redis/cluster/core.go index 7d1ccdf91..a59688ae9 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/key.go b/lego/sys/redis/cluster/key.go index 2db92cd50..311515e50 100644 --- a/lego/sys/redis/cluster/key.go +++ b/lego/sys/redis/cluster/key.go @@ -1,84 +1,69 @@ package cluster import ( - "github.com/go-redis/redis/v8" + "time" ) /* Key *******************************************************************************/ ///删除redis key -func (this *Redis) Delete(key string) (err error) { - err = this.client.Do(this.getContext(), "DEL", key).Err() - - return +func (this *Redis) Delete(key ...string) (int64, error) { + return this.client.Del(this.getContext(), key...).Result() } ///判断是否存在key -func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { - iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() - return +func (this *Redis) Exists(key ...string) (int64, error) { + return this.client.Exists(this.getContext(), key...).Result() } ///设置key的过期时间 单位以秒级 -func (this *Redis) ExpireKey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() - return +func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { + return this.client.Expire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 秒级时间戳 -func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() - return +func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { + return this.client.ExpireAt(this.getContext(), key, expire).Result() } ///设置key的过期时间 单位以毫秒级 -func (this *Redis) Pexpirekey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() - return +func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { + return this.client.PExpire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 单位以豪秒级 -func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() - return +func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { + return this.client.PExpireAt(this.getContext(), key, expire).Result() } ///移除Key的过期时间 -func (this *Redis) PersistKey(key string) (err error) { - err = this.client.Do(this.getContext(), "PERSIST", key).Err() - return +func (this *Redis) Persist(key string) (bool, error) { + return this.client.Persist(this.getContext(), key).Result() } ///获取key剩余过期时间 单位毫秒 -func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() - return +func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { + return this.client.PTTL(this.getContext(), key).Result() } ///获取key剩余过期时间 单位秒 -func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() - return +func (this *Redis) TTL(key string) (surplus time.Duration, err error) { + return this.client.TTL(this.getContext(), key).Result() } ///重命名Key -func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() - return +func (this *Redis) Rename(oldkey string, newkey string) (string, error) { + return this.client.Rename(this.getContext(), oldkey, newkey).Result() } ///重命名key 在新的 key 不存在时修改 key 的名称 -func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() - return +func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { + return this.client.RenameNX(this.getContext(), oldkey, newkey).Result() } ///判断是否存在key pattern:key* func (this *Redis) Keys(pattern string) (keys []string, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) - this.client.Process(this.getContext(), cmd) - keys, err = cmd.Result() - return + return this.client.Keys(this.getContext(), pattern).Result() } ///获取键类型 diff --git a/lego/sys/redis/cluster/list.go b/lego/sys/redis/cluster/list.go index 9e5a98c7a..9b9981167 100644 --- a/lego/sys/redis/cluster/list.go +++ b/lego/sys/redis/cluster/list.go @@ -1,23 +1,14 @@ package cluster import ( - "fmt" - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -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 +func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { + return this.client.LIndex(this.getContext(), key, index).Scan(v) } /* @@ -25,90 +16,55 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误 */ -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() - } - } +func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + op := "BEFORE" + if !isbefore { + op = "AFTER" } - return + return this.client.LInsert(this.getContext(), key, op, pivot, value).Result() } /* Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Llen(key string) (result int, err error) { - result, err = this.client.Do(this.getContext(), "LLEN", key).Int() - return +func (this *Redis) Llen(key string) (int64, error) { + return this.client.LLen(this.getContext(), key).Result() } /* Redis Lpop 命令用于移除并返回列表的第一个元素 */ -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 +func (this *Redis) LPop(key string, v interface{}) (err error) { + return this.client.LPop(this.getContext(), key).Scan(v) } /* Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 */ -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 +func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { + return this.client.LPush(this.getContext(), key, values...).Result() } /* Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 */ -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 +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() } /* Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -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 +func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd { + return this.client.LRange(this.getContext(), key, start, end) } /* @@ -118,24 +74,16 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素, count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 count = 0 : 移除表中所有与 VALUE 相等的值 */ -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 +func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { + return this.client.LRem(this.getContext(), key, count, v).Result() } /* -Redis Lset 通过索引来设置元素的值。 +Redis LSet 通过索引来设置元素的值。 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 */ -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 +func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { + return this.client.LSet(this.getContext(), key, index, v).Result() } /* @@ -143,35 +91,22 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Ltrim(key string, start, stop int) (err error) { - err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() - return +func (this *Redis) LTrim(key string, start, stop int64) (string, error) { + return this.client.LTrim(this.getContext(), key, start, stop).Result() } /* Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 */ -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 +func (this *Redis) RPop(key string, v interface{}) (err error) { + return this.client.RPop(this.getContext(), key).Scan(v) } /* Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 */ -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 +func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) { + return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v) } /* @@ -179,27 +114,13 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边) 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 */ -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 +func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { + return this.client.RPush(this.getContext(), key, values...).Result() } /* Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 */ -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 +func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { + return this.client.RPushX(this.getContext(), key, values...).Result() } diff --git a/lego/sys/redis/cluster/set.go b/lego/sys/redis/cluster/set.go index 74d5561f6..64a59cb3e 100644 --- a/lego/sys/redis/cluster/set.go +++ b/lego/sys/redis/cluster/set.go @@ -1,30 +1,23 @@ package cluster -import "reflect" +import ( + "github.com/go-redis/redis/v8" +) /* Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 当集合 key 不是集合类型时,返回一个错误。 */ -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 +func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { + return this.client.SAdd(this.getContext(), key, values...).Result() } /* Redis Scard 命令返回集合中元素的数量 */ -func (this *Redis) SCard(key string) (result int64, err error) { - result, err = this.client.SCard(this.getContext(), key).Result() - return +func (this *Redis) SCard(key string) (int64, error) { + return this.client.SCard(this.getContext(), key).Result() } /* @@ -32,79 +25,43 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 实例: */ -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 +func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { + return this.client.SDiff(this.getContext(), keys...) } /* Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 */ -func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) { + return this.client.SDiffStore(this.getContext(), destination, keys...).Result() } /* Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 */ -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 +func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { + return this.client.SInter(this.getContext(), keys...) } /* Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 */ -func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) { + return this.client.SInterStore(this.getContext(), destination, keys...).Result() } /* Redis Sismember 命令判断成员元素是否是集合的成员 */ -func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { - iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() - return +func (this *Redis) SIsMember(key string, value interface{}) (bool, error) { + return this.client.SIsMember(this.getContext(), key, value).Result() } /* Redis Smembers 号召返回集合中的所有成员。 */ -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 +func (this *Redis) SMembers(key string) *redis.StringSliceCmd { + return this.client.SMembers(this.getContext(), key) } /* @@ -114,18 +71,16 @@ SMOVE 是原子性操作。 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 当 source 或 destination 不是集合类型时,返回一个错误。 */ -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 +func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) { + return this.client.SMove(this.getContext(), source, destination, member).Result() } /* Redis Spop命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 */ -func (this *Redis) Spop(key string) (result string, err error) { - result, err = this.client.SPop(this.getContext(), key).Result() - return +func (this *Redis) SPop(key string) (string, error) { + return this.client.SPop(this.getContext(), key).Result() } /* @@ -135,9 +90,8 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 */ -func (this *Redis) Srandmember(key string) (result string, err error) { - result, err = this.client.SRandMember(this.getContext(), key).Result() - return +func (this *Redis) SRandMember(key string) (string, error) { + return this.client.SRandMember(this.getContext(), key).Result() } /* @@ -145,41 +99,27 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在 当键不是集合类型,返回一个错误。 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 */ -func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.SRem(this.getContext(), key, members...).Result() - return +func (this *Redis) SRem(key string, members ...interface{}) (int64, error) { + return this.client.SRem(this.getContext(), key, members...).Result() } /* Redis Sunion 命令返回给定集合的并集。 */ -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 +func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { + return this.client.SUnion(this.getContext(), keys...) } /* Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 */ -func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) { + return this.client.SUnionStore(this.getContext(), destination, keys...).Result() } /* Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 */ -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 +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() } diff --git a/lego/sys/redis/cluster/string.go b/lego/sys/redis/cluster/string.go index 17fc6f1a9..1dee13835 100644 --- a/lego/sys/redis/cluster/string.go +++ b/lego/sys/redis/cluster/string.go @@ -1,7 +1,6 @@ package cluster import ( - "fmt" "time" "github.com/go-redis/redis/v8" @@ -11,54 +10,39 @@ import ( /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 */ -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 +func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { + return this.client.Set(this.getContext(), string(key), v, expiration).Result() } /* 指定的 key 不存在时,为 key 设置指定的值 */ -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 +func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return this.client.SetNX(this.getContext(), key, v, expiration).Result() } /* 同时设置一个或多个 key-value 对 */ -func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSet(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSET") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 */ -func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSetNX(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSETNX") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* @@ -67,9 +51,8 @@ Redis Incr 命令将 key 中储存的数字值增一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内。 */ -func (this *Redis) Incr(key string) (err error) { - err = this.client.Do(this.getContext(), "INCR", key).Err() - return +func (this *Redis) Incr(key string) (int64, error) { + return this.client.Incr(this.getContext(), key).Result() } /* @@ -78,18 +61,16 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) IncrBY(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() - return +func (this *Redis) IncrBY(key string, value int64) (int64, error) { + return this.client.IncrBy(this.getContext(), key, value).Result() } /* Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 */ -func (this *Redis) Incrbyfloat(key string, value float32) (err error) { - err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() - return +func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { + return this.client.IncrByFloat(this.getContext(), key, value).Result() } /* @@ -98,9 +79,8 @@ Redis Decr 命令将 key 中储存的数字值减一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) Decr(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECR", key, value).Err() - return +func (this *Redis) Decr(key string, value int) (int64, error) { + return this.client.Decr(this.getContext(), key).Result() } /* @@ -109,9 +89,8 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) DecrBy(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() - return +func (this *Redis) DecrBy(key string, value int64) (int64, error) { + return this.client.DecrBy(this.getContext(), key, value).Result() } /* @@ -119,62 +98,28 @@ Redis Append 命令用于为指定的 key 追加值。 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 */ -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() - } +func (this *Redis) Append(key string, value string) (err error) { + this.client.Append(this.getContext(), key, value).Result() return } /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 */ -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 +func (this *Redis) Get(key string, v interface{}) (err error) { + return this.client.Get(this.getContext(), key).Scan(v) } /* 设置指定 key 的值,并返回 key 的旧值 */ -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 +func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) { + return this.client.GetSet(this.getContext(), key, v).Scan(result) } /* 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil */ -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 +func (this *Redis) MGet(keys ...string) *redis.SliceCmd { + return this.client.MGet(this.getContext(), keys...) } diff --git a/lego/sys/redis/cluster/zset.go b/lego/sys/redis/cluster/zset.go index 206710e22..9d3de4aab 100644 --- a/lego/sys/redis/cluster/zset.go +++ b/lego/sys/redis/cluster/zset.go @@ -1,217 +1,145 @@ package cluster import ( - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 */ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { - this.client.ZAdd(this.getContext(), key, members...) - return +func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { + return this.client.ZAdd(this.getContext(), key, members...).Result() } /* Redis Zcard 用于计算集合中元素的数量。 */ -func (this *Redis) ZCard(key string) (result int64, err error) { - result, err = this.client.ZCard(this.getContext(), key).Result() - return +func (this *Redis) ZCard(key string) (int64, error) { + return this.client.ZCard(this.getContext(), key).Result() } /* Redis ZCount 用于计算集合中指定的范围内的数量 */ -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 +func (this *Redis) ZCount(key string, min string, max string) (int64, error) { + return this.client.ZCount(this.getContext(), key, min, max).Result() } /* Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment */ -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 +func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) { + return this.client.ZIncrBy(this.getContext(), key, increment, member).Result() } /* Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 */ -func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() - return +func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) { + return this.client.ZInterStore(this.getContext(), destination, store).Result() } /* Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 */ -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 +func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) { + return this.client.ZLexCount(this.getContext(), key, min, max).Result() } /* Redis ZRange 通过索引区间返回有序集合指定区间内的成员 */ -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 +func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRange(this.getContext(), key, start, stop) } /* Redis ZRangeByLex 通过字典区间返回有序集合的成员 */ -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 +func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByLex(this.getContext(), key, opt) } /* Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 */ -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 +func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByScore(this.getContext(), key, opt) } /* Redis ZRank 返回有序集合中指定成员的索引 */ -func (this *Redis) ZRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRank(key string, member string) (int64, error) { + return this.client.ZRank(this.getContext(), key, member).Result() } /* Redis ZRem 移除有序集合中的一个或多个成员 */ -func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.ZRem(this.getContext(), key, members...).Result() - return +func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) { + return this.client.ZRem(this.getContext(), key, members...).Result() } /* Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 */ -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 +func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() } /* Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 */ -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 +func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) { + return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() } /* Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 */ -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 +func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() } /* Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE */ -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 +func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRevRange(this.getContext(), key, start, stop) } /* Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -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 +func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRevRangeByScore(this.getContext(), key, opt) } /* Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRevRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRevRank(key string, member string) (int64, error) { + return this.client.ZRevRank(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZScore(key string, member string) (result float64, err error) { - result, err = this.client.ZScore(this.getContext(), key, member).Result() - return +func (this *Redis) ZScore(key string, member string) (float64, error) { + return this.client.ZScore(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE */ -func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() - return +func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) { + return this.client.ZUnionStore(this.getContext(), dest, store).Result() } /* Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) */ -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 +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() } diff --git a/lego/sys/redis/core.go b/lego/sys/redis/core.go index a388ce2a7..74d555214 100644 --- a/lego/sys/redis/core.go +++ b/lego/sys/redis/core.go @@ -2,7 +2,6 @@ package redis import ( "context" - "reflect" "time" "github.com/go-redis/redis/v8" @@ -18,99 +17,382 @@ 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*/ - 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) + ///删除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* Keys(pattern string) (keys []string, err error) + ///获取键类型 Type(key string) (ty string, err error) /*String*/ - 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) + /* + 命令用于设置给定 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 对 + */ MSetNX(keyvalues map[string]interface{}) (err error) - 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) + /* + 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 /*List*/ - 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) + /* + 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) /*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) + */ 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 操作 + */ 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*/ - 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) + /* + 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) /*ZSet*/ - 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) + /* + 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) } ISys interface { @@ -162,55 +444,52 @@ 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) (err error) { - return defsys.Delete(key) +func Delete(key ...string) (int64, error) { + return defsys.Delete(key...) } -func ExistsKey(key string) (iskeep bool, err error) { - return defsys.ExistsKey(key) - +func Exists(key ...string) (int64, error) { + return defsys.Exists(key...) } -func ExpireKey(key string, expire int) (err error) { - return defsys.ExpireKey(key, expire) +func Expire(key string, expire time.Duration) (bool, error) { + return defsys.Expire(key, expire) } -func ExpireatKey(key string, expire_unix int64) (err error) { - return defsys.ExpireatKey(key, expire_unix) +func ExpireAt(key string, expire time.Time) (bool, error) { + return defsys.ExpireAt(key, expire) } -func Pexpirekey(key string, expire int) (err error) { - return defsys.Pexpirekey(key, expire) +func PExpire(key string, expire time.Duration) (bool, error) { + return defsys.PExpire(key, expire) } -func PexpireatKey(key string, expire_unix int64) (err error) { - return defsys.PexpireatKey(key, expire_unix) +func PExpireAt(key string, expire time.Time) (bool, error) { + return defsys.PExpireAt(key, expire) } -func PersistKey(key string) (err error) { - return defsys.PersistKey(key) +func Persist(key string) (bool, error) { + return defsys.Persist(key) } -func PttlKey(key string) (leftexpire int64, err error) { - return defsys.PttlKey(key) +func PTTL(key string) (surplus time.Duration, err error) { + return defsys.PTTL(key) } -func TtlKey(key string) (leftexpire int64, err error) { - return defsys.TtlKey(key) +func TTL(key string) (surplus time.Duration, err error) { + return defsys.TTL(key) } -func RenameKye(oldkey string, newkey string) (err error) { - return defsys.RenameKye(oldkey, newkey) +func Rename(oldkey string, newkey string) (string, error) { + return defsys.Rename(oldkey, newkey) } -func RenamenxKey(oldkey string, newkey string) (err error) { - return defsys.RenamenxKey(oldkey, newkey) +func RenameNX(oldkey string, newkey string) (bool, error) { + return defsys.RenameNX(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, value interface{}, expiration time.Duration) (err error) { - return defsys.Set(key, value, expiration) +func Set(key string, v interface{}, expiration time.Duration) (string, error) { + return defsys.Set(key, v, expiration) } -func SetNX(key string, value interface{}) (result int64, err error) { - return defsys.SetNX(key, value) +func SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return defsys.SetNX(key, v, expiration) } func MSet(keyvalues map[string]interface{}) (err error) { return defsys.MSet(keyvalues) @@ -218,36 +497,33 @@ func MSet(keyvalues map[string]interface{}) (err error) { func MSetNX(keyvalues map[string]interface{}) (err error) { return defsys.MSetNX(keyvalues) } -func Incr(key string) (err error) { +func Incr(key string) (int64, error) { return defsys.Incr(key) } -func IncrBY(key string, value int) (err error) { +func IncrBY(key string, value int64) (int64, error) { return defsys.IncrBY(key, value) } -func Incrbyfloat(key string, value float32) (err error) { +func Incrbyfloat(key string, value float64) (float64, error) { return defsys.Incrbyfloat(key, value) } -func Decr(key string, value int) (err error) { +func Decr(key string, value int) (int64, error) { return defsys.Decr(key, value) } -func DecrBy(key string, value int) (err error) { +func DecrBy(key string, value int64) (int64, error) { return defsys.DecrBy(key, value) } -func Append(key string, value interface{}) (err error) { +func Append(key string, value string) (err error) { return defsys.Append(key, value) } -func Get(key string, value interface{}) (err error) { - return defsys.Get(key, value) +func Get(key string, v interface{}) (err error) { + return defsys.Get(key, v) } -func GetSet(key string, value interface{}, result interface{}) (err error) { - return defsys.GetSet(key, value, result) +func GetSet(key string, v interface{}, result interface{}) (err error) { + return defsys.GetSet(key, v, result) } -func MGet(keys ...string) (result []string, err error) { +func MGet(keys ...string) *redis.SliceCmd { 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) { @@ -262,46 +538,46 @@ func UnLock(key string) (err error) { } /*List*/ -func Lindex(key string, value interface{}) (err error) { - return defsys.Lindex(key, value) +func Lindex(key string, index int64, v interface{}) (err error) { + return defsys.Lindex(key, index, v) } -func Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { - return defsys.Linsert(key, isbefore, tager, value) +func Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + return defsys.Linsert(key, isbefore, pivot, value) } -func Llen(key string) (result int, err error) { +func Llen(key string) (int64, error) { return defsys.Llen(key) } func LPop(key string, value interface{}) (err error) { return defsys.LPop(key, value) } -func LPush(key string, values ...interface{}) (err error) { +func LPush(key string, values ...interface{}) (int64, error) { return defsys.LPush(key, values...) } -func LPushX(key string, values ...interface{}) (err error) { +func LPushX(key string, values ...interface{}) (int64, error) { return defsys.LPushX(key, values...) } -func LRange(key string, start, end int, valuetype reflect.Type) (result []interface{}, err error) { - return defsys.LRange(key, start, end, valuetype) +func LRange(key string, start int64, end int64) *redis.StringSliceCmd { + return defsys.LRange(key, start, end) } -func LRem(key string, count int, target interface{}) (err error) { - return defsys.LRem(key, count, target) +func LRem(key string, count int64, v interface{}) (int64, error) { + return defsys.LRem(key, count, v) } -func LSet(key string, index int, value interface{}) (err error) { - return defsys.LSet(key, index, value) +func LSet(key string, index int64, v interface{}) (string, error) { + return defsys.LSet(key, index, v) } -func Ltrim(key string, start, stop int) (err error) { - return defsys.Ltrim(key, start, stop) +func LTrim(key string, start int64, stop int64) (string, error) { + return defsys.LTrim(key, start, stop) } -func Rpop(key string, value interface{}) (err error) { - return defsys.Rpop(key, value) +func RPop(key string, v interface{}) (err error) { + return defsys.RPop(key, v) } func RPopLPush(oldkey string, newkey string, value interface{}) (err error) { return defsys.RPopLPush(oldkey, newkey, value) } -func RPush(key string, values ...interface{}) (err error) { +func RPush(key string, values ...interface{}) (int64, error) { return defsys.RPush(key, values...) } -func RPushX(key string, values ...interface{}) (err error) { +func RPushX(key string, values ...interface{}) (int64, error) { return defsys.RPushX(key, values...) } @@ -344,54 +620,54 @@ func HSetNX(key string, field string, value interface{}) (err error) { } /*Set*/ -func SAdd(key string, values ...interface{}) (err error) { +func SAdd(key string, values ...interface{}) (int64, error) { return defsys.SAdd(key, values...) } func SCard(key string) (result int64, err error) { return defsys.SCard(key) } -func SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return defsys.SDiff(valuetype, keys...) +func SDiff(keys ...string) *redis.StringSliceCmd { + return defsys.SDiff(keys...) } func SDiffStore(destination string, keys ...string) (result int64, err error) { return defsys.SDiffStore(destination, keys...) } -func SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return defsys.SInter(valuetype, keys...) +func SInter(keys ...string) *redis.StringSliceCmd { + return defsys.SInter(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(valuetype reflect.Type, key string) (result []interface{}, err error) { - return defsys.SMembers(valuetype, key) +func SMembers(key string) *redis.StringSliceCmd { + return defsys.SMembers(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) (result string, err error) { - return defsys.Srandmember(key) +func SRandMember(key string) (string, error) { + return defsys.SRandMember(key) } func SRem(key string, members ...interface{}) (result int64, err error) { return defsys.SRem(key, members...) } -func SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return defsys.SUnion(valuetype, keys...) +func SUnion(keys ...string) *redis.StringSliceCmd { + return defsys.SUnion(keys...) } -func Sunionstore(destination string, keys ...string) (result int64, err error) { - return defsys.Sunionstore(destination, keys...) +func SUnionStore(destination string, keys ...string) (int64, error) { + return defsys.SUnionStore(destination, keys...) } -func Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { - return defsys.Sscan(key, _cursor, match, count) +func SScan(key string, _cursor uint64, match string, count int64) ([]string, uint64, error) { + return defsys.SScan(key, _cursor, match, count) } /*ZSet*/ -func ZAdd(key string, members ...*redis.Z) (err error) { +func ZAdd(key string, members ...*redis.Z) (int64, error) { return defsys.ZAdd(key, members...) } func ZCard(key string) (result int64, err error) { @@ -409,14 +685,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(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return defsys.ZRange(valuetype, key, start, stop) +func ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return defsys.ZRange(key, start, stop) } -func ZRangeByLex(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return defsys.ZRangeByLex(valuetype, key, opt) +func ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return defsys.ZRangeByLex(key, opt) } -func ZRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return defsys.ZRangeByScore(valuetype, key, opt) +func ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return defsys.ZRangeByScore(key, opt) } func ZRank(key string, member string) (result int64, err error) { return defsys.ZRank(key, member) @@ -433,11 +709,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(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return defsys.ZRevRange(valuetype, key, start, stop) +func ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return defsys.ZRevRange(key, start, stop) } -func ZRevRangeByScore(valuetype reflect.Type, key string, opt *redis.ZRangeBy) (result []interface{}, err error) { - return defsys.ZRevRangeByScore(valuetype, key, opt) +func ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return defsys.ZRevRangeByScore(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 890d42f55..b5f0d791a 100644 --- a/lego/sys/redis/redis.go +++ b/lego/sys/redis/redis.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "reflect" "time" "go_dreamfactory/lego/sys/redis/cluster" @@ -99,40 +98,40 @@ func (this *Redis) Decode(value []byte, result interface{}) (err error) { return } -func (this *Redis) Delete(key string) (err error) { - return this.client.Delete(key) +func (this *Redis) Delete(key ...string) (int64, error) { + return this.client.Delete(key...) } -func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { - return this.client.ExistsKey(key) +func (this *Redis) Exists(key ...string) (int64, error) { + return this.client.Exists(key...) } -func (this *Redis) ExpireKey(key string, expire int) (err error) { - return this.client.ExpireKey(key, expire) +func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { + return this.client.Expire(key, expire) } -func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { - return this.client.ExpireatKey(key, expire_unix) +func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { + return this.client.ExpireAt(key, expire) } -func (this *Redis) Pexpirekey(key string, expire int) (err error) { - return this.client.Pexpirekey(key, expire) +func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { + return this.client.PExpire(key, expire) } -func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { - return this.client.PexpireatKey(key, expire_unix) +func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { + return this.client.PExpireAt(key, expire) } -func (this *Redis) PersistKey(key string) (err error) { - return this.client.PersistKey(key) +func (this *Redis) Persist(key string) (bool, error) { + return this.client.Persist(key) } -func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { - return this.client.PttlKey(key) +func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { + return this.client.PTTL(key) } -func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { - return this.client.TtlKey(key) +func (this *Redis) TTL(key string) (surplus time.Duration, err error) { + return this.client.TTL(key) } -func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { - return this.client.RenameKye(oldkey, newkey) +func (this *Redis) Rename(oldkey string, newkey string) (string, error) { + return this.client.Rename(oldkey, newkey) } -func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { - return this.client.RenamenxKey(oldkey, newkey) +func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { + return this.client.RenameNX(oldkey, newkey) } func (this *Redis) Keys(pattern string) (keys []string, err error) { return this.client.Keys(pattern) @@ -144,11 +143,11 @@ func (this *Redis) Type(key string) (ty string, err error) { } /*String*/ -func (this *Redis) Set(key string, value interface{}, expiration time.Duration) (err error) { - return this.client.Set(key, value, expiration) +func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { + return this.client.Set(key, v, expiration) } -func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { - return this.client.SetNX(key, value) +func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return this.client.SetNX(key, v, expiration) } func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { return this.client.MSet(keyvalues) @@ -156,22 +155,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) (err error) { +func (this *Redis) Incr(key string) (int64, error) { return this.client.Incr(key) } -func (this *Redis) IncrBY(key string, value int) (err error) { +func (this *Redis) IncrBY(key string, value int64) (int64, error) { return this.client.IncrBY(key, value) } -func (this *Redis) Incrbyfloat(key string, value float32) (err error) { +func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { return this.client.Incrbyfloat(key, value) } -func (this *Redis) Decr(key string, value int) (err error) { +func (this *Redis) Decr(key string, value int) (int64, error) { return this.client.Decr(key, value) } -func (this *Redis) DecrBy(key string, value int) (err error) { +func (this *Redis) DecrBy(key string, value int64) (int64, error) { return this.client.DecrBy(key, value) } -func (this *Redis) Append(key string, value interface{}) (err error) { +func (this *Redis) Append(key string, value string) (err error) { return this.client.Append(key, value) } func (this *Redis) Get(key string, value interface{}) (err error) { @@ -180,54 +179,51 @@ 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) (result []string, err error) { +func (this *Redis) MGet(keys ...string) *redis.SliceCmd { 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, value interface{}) (err error) { - return this.client.Lindex(key, value) +func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { + return this.client.Lindex(key, index, v) } -func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value interface{}) (err error) { - return this.client.Linsert(key, isbefore, tager, 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) Llen(key string) (result int, err error) { +func (this *Redis) Llen(key string) (int64, error) { return this.client.Llen(key) } func (this *Redis) LPop(key string, value interface{}) (err error) { return this.client.LPop(key, value) } -func (this *Redis) LPush(key string, values ...interface{}) (err error) { +func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { return this.client.LPush(key, values...) } -func (this *Redis) LPushX(key string, values ...interface{}) (err error) { +func (this *Redis) LPushX(key string, values ...interface{}) (int64, error) { return this.client.LPushX(key, values...) } -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) LRange(key string, start int64, end int64) *redis.StringSliceCmd { + return this.client.LRange(key, start, end) } -func (this *Redis) LRem(key string, count int, target interface{}) (err error) { - return this.client.LRem(key, count, target) +func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { + return this.client.LRem(key, count, v) } -func (this *Redis) LSet(key string, index int, value interface{}) (err error) { - return this.client.LSet(key, index, value) +func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { + return this.client.LSet(key, index, v) } -func (this *Redis) Ltrim(key string, start, stop int) (err error) { - return this.client.Ltrim(key, start, stop) +func (this *Redis) LTrim(key string, start int64, stop int64) (string, error) { + return this.client.LTrim(key, start, stop) } -func (this *Redis) Rpop(key string, value interface{}) (err error) { - return this.client.Rpop(key, value) +func (this *Redis) 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{}) (err error) { +func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { return this.client.RPush(key, values...) } -func (this *Redis) RPushX(key string, values ...interface{}) (err error) { +func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { return this.client.RPushX(key, values...) } @@ -270,54 +266,54 @@ func (this *Redis) HSetNX(key string, field string, value interface{}) (err erro } /*Set*/ -func (this *Redis) SAdd(key string, values ...interface{}) (err error) { +func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { return this.client.SAdd(key, values...) } func (this *Redis) SCard(key string) (result int64, err error) { return this.client.SCard(key) } -func (this *Redis) SDiff(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return this.client.SDiff(valuetype, keys...) +func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { + return this.client.SDiff(keys...) } func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { return this.client.SDiffStore(destination, keys...) } -func (this *Redis) SInter(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return this.client.SInter(valuetype, keys...) +func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { + return this.client.SInter(keys...) } func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { return this.client.SInterStore(destination, keys...) } -func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { - return this.client.Sismember(key, value) +func (this *Redis) SIsMember(key string, value interface{}) (iskeep bool, err error) { + return this.client.SIsMember(key, value) } -func (this *Redis) SMembers(valuetype reflect.Type, key string) (result []interface{}, err error) { - return this.client.SMembers(valuetype, key) +func (this *Redis) SMembers(key string) *redis.StringSliceCmd { + return this.client.SMembers(key) } func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { return this.client.SMove(source, destination, member) } -func (this *Redis) Spop(key string) (result string, err error) { - return this.client.Spop(key) +func (this *Redis) SPop(key string) (string, error) { + return this.client.SPop(key) } -func (this *Redis) Srandmember(key string) (result string, err error) { - return this.client.Srandmember(key) +func (this *Redis) SRandMember(key string) (string, error) { + return this.client.SRandMember(key) } func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { return this.client.SRem(key, members...) } -func (this *Redis) SUnion(valuetype reflect.Type, keys ...string) (result []interface{}, err error) { - return this.client.SUnion(valuetype, keys...) +func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { + return this.client.SUnion(keys...) } -func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { - return this.client.Sunionstore(destination, keys...) +func (this *Redis) 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) (err error) { +func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { return this.client.ZAdd(key, members...) } func (this *Redis) ZCard(key string) (result int64, err error) { @@ -335,14 +331,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(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return this.client.ZRange(valuetype, key, start, stop) +func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRange(key, start, stop) } -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) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByLex(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) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByScore(key, opt) } func (this *Redis) ZRank(key string, member string) (result int64, err error) { return this.client.ZRank(key, member) @@ -359,11 +355,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(valuetype reflect.Type, key string, start int64, stop int64) (result []interface{}, err error) { - return this.client.ZRevRange(valuetype, key, start, stop) +func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRevRange(key, start, stop) } -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) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRevRangeByScore(key, opt) } func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { return this.client.ZRevRank(key, member) diff --git a/lego/sys/redis/single/core.go b/lego/sys/redis/single/core.go index 8ea81ce6e..18f218336 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/key.go b/lego/sys/redis/single/key.go index 8d6b46ec5..bc8d07f99 100644 --- a/lego/sys/redis/single/key.go +++ b/lego/sys/redis/single/key.go @@ -1,83 +1,69 @@ package single import ( - "github.com/go-redis/redis/v8" + "time" ) /* Key *******************************************************************************/ ///删除redis key -func (this *Redis) Delete(key string) (err error) { - err = this.client.Do(this.getContext(), "DEL", key).Err() - return +func (this *Redis) Delete(key ...string) (int64, error) { + return this.client.Del(this.getContext(), key...).Result() } ///判断是否存在key -func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { - iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool() - return +func (this *Redis) Exists(key ...string) (int64, error) { + return this.client.Exists(this.getContext(), key...).Result() } ///设置key的过期时间 单位以秒级 -func (this *Redis) ExpireKey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "EXPIRE", key, expire).Err() - return +func (this *Redis) Expire(key string, expire time.Duration) (bool, error) { + return this.client.Expire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 秒级时间戳 -func (this *Redis) ExpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "EXPIREAT", key, expire_unix).Err() - return +func (this *Redis) ExpireAt(key string, expire time.Time) (bool, error) { + return this.client.ExpireAt(this.getContext(), key, expire).Result() } ///设置key的过期时间 单位以毫秒级 -func (this *Redis) Pexpirekey(key string, expire int) (err error) { - err = this.client.Do(this.getContext(), "PEXPIRE", key, expire).Err() - return +func (this *Redis) PExpire(key string, expire time.Duration) (bool, error) { + return this.client.PExpire(this.getContext(), key, expire).Result() } ///设置key的过期时间戳 单位以豪秒级 -func (this *Redis) PexpireatKey(key string, expire_unix int64) (err error) { - err = this.client.Do(this.getContext(), "PEXPIREAT", key, expire_unix).Err() - return +func (this *Redis) PExpireAt(key string, expire time.Time) (bool, error) { + return this.client.PExpireAt(this.getContext(), key, expire).Result() } ///移除Key的过期时间 -func (this *Redis) PersistKey(key string) (err error) { - err = this.client.Do(this.getContext(), "PERSIST", key).Err() - return +func (this *Redis) Persist(key string) (bool, error) { + return this.client.Persist(this.getContext(), key).Result() } ///获取key剩余过期时间 单位毫秒 -func (this *Redis) PttlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "PTTL", key).Int64() - return +func (this *Redis) PTTL(key string) (surplus time.Duration, err error) { + return this.client.PTTL(this.getContext(), key).Result() } ///获取key剩余过期时间 单位秒 -func (this *Redis) TtlKey(key string) (leftexpire int64, err error) { - leftexpire, err = this.client.Do(this.getContext(), "TTL", key).Int64() - return +func (this *Redis) TTL(key string) (surplus time.Duration, err error) { + return this.client.TTL(this.getContext(), key).Result() } ///重命名Key -func (this *Redis) RenameKye(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAME", oldkey, newkey).Err() - return +func (this *Redis) Rename(oldkey string, newkey string) (string, error) { + return this.client.Rename(this.getContext(), oldkey, newkey).Result() } ///重命名key 在新的 key 不存在时修改 key 的名称 -func (this *Redis) RenamenxKey(oldkey string, newkey string) (err error) { - err = this.client.Do(this.getContext(), "RENAMENX", oldkey, newkey).Err() - return +func (this *Redis) RenameNX(oldkey string, newkey string) (bool, error) { + return this.client.RenameNX(this.getContext(), oldkey, newkey).Result() } ///判断是否存在key pattern:key* func (this *Redis) Keys(pattern string) (keys []string, err error) { - cmd := redis.NewStringSliceCmd(this.getContext(), "KEYS", string(pattern)) - this.client.Process(this.getContext(), cmd) - keys, err = cmd.Result() - return + return this.client.Keys(this.getContext(), pattern).Result() } ///获取键类型 diff --git a/lego/sys/redis/single/list.go b/lego/sys/redis/single/list.go index 18a0011d8..3adfa9907 100644 --- a/lego/sys/redis/single/list.go +++ b/lego/sys/redis/single/list.go @@ -1,23 +1,14 @@ package single import ( - "fmt" - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis Lindex 命令用于通过索引获取列表中的元素。你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -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 +func (this *Redis) Lindex(key string, index int64, v interface{}) (err error) { + return this.client.LIndex(this.getContext(), key, index).Scan(v) } /* @@ -25,90 +16,55 @@ Redis Linsert 命令用于在列表的元素前或者后插入元素。当指定 当列表不存在时,被视为空列表,不执行任何操作。 如果 key 不是列表类型,返回一个错误 */ -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() - } - } +func (this *Redis) Linsert(key string, isbefore bool, pivot interface{}, value interface{}) (int64, error) { + op := "BEFORE" + if !isbefore { + op = "AFTER" } - return + return this.client.LInsert(this.getContext(), key, op, pivot, value).Result() } /* Redis Llen 命令用于返回列表的长度。 如果列表 key 不存在,则 key 被解释为一个空列表,返回 0 。 如果 key 不是列表类型,返回一个错误 */ -func (this *Redis) Llen(key string) (result int, err error) { - result, err = this.client.Do(this.getContext(), "LLEN", key).Int() - return +func (this *Redis) Llen(key string) (int64, error) { + return this.client.LLen(this.getContext(), key).Result() } /* Redis Lpop 命令用于移除并返回列表的第一个元素 */ -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 +func (this *Redis) LPop(key string, v interface{}) (err error) { + return this.client.LPop(this.getContext(), key).Scan(v) } /* Redis Lpush 命令将一个或多个值插入到列表头部。 如果 key 不存在,一个空列表会被创建并执行 LPUSH 操作。 当 key 存在但不是列表类型时,返回一个错误 */ -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 +func (this *Redis) LPush(key string, values ...interface{}) (int64, error) { + return this.client.LPush(this.getContext(), key, values...).Result() } /* Redis Lpushx 将一个值插入到已存在的列表头部,列表不存在时操作无效 */ -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 +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() } /* Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START 和 END 指定。 其中 0 表示列表的第一个元素, 1 表示列表的第二个元素, 以此类推。 你也可以使用负数下标,以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -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 +func (this *Redis) LRange(key string, start, end int64) *redis.StringSliceCmd { + return this.client.LRange(this.getContext(), key, start, end) } /* @@ -118,24 +74,16 @@ count > 0 : 从表头开始向表尾搜索,移除与 VALUE 相等的元素, count < 0 : 从表尾开始向表头搜索,移除与 VALUE 相等的元素,数量为 COUNT 的绝对值。 count = 0 : 移除表中所有与 VALUE 相等的值 */ -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 +func (this *Redis) LRem(key string, count int64, v interface{}) (int64, error) { + return this.client.LRem(this.getContext(), key, count, v).Result() } /* -Redis Lset 通过索引来设置元素的值。 +Redis LSet 通过索引来设置元素的值。 当索引参数超出范围,或对一个空列表进行 LSET 时,返回一个错误 */ -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 +func (this *Redis) LSet(key string, index int64, v interface{}) (string, error) { + return this.client.LSet(this.getContext(), key, index, v).Result() } /* @@ -143,35 +91,22 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留 下标 0 表示列表的第一个元素,以 1 表示列表的第二个元素,以此类推。 你也可以使用负数下标, 以 -1 表示列表的最后一个元素, -2 表示列表的倒数第二个元素,以此类推 */ -func (this *Redis) Ltrim(key string, start, stop int) (err error) { - err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err() - return +func (this *Redis) LTrim(key string, start, stop int64) (string, error) { + return this.client.LTrim(this.getContext(), key, start, stop).Result() } /* Redis Rpop 命令用于移除列表的最后一个元素,返回值为移除的元素 */ -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 +func (this *Redis) RPop(key string, v interface{}) (err error) { + return this.client.RPop(this.getContext(), key).Scan(v) } /* Redis Rpoplpush 命令用于移除列表的最后一个元素,并将该元素添加到另一个列表并返回 */ -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 +func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) { + return this.client.RPopLPush(this.getContext(), oldkey, newkey).Scan(v) } /* @@ -179,27 +114,13 @@ Redis Rpush 命令用于将一个或多个值插入到列表的尾部(最右边) 如果列表不存在,一个空列表会被创建并执行 RPUSH 操作。 当列表存在但不是列表类型时,返回一个错误。 注意:在 Redis 2.4 版本以前的 RPUSH 命令,都只接受单个 value 值 */ -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 +func (this *Redis) RPush(key string, values ...interface{}) (int64, error) { + return this.client.RPush(this.getContext(), key, values...).Result() } /* Redis Rpushx 命令用于将一个值插入到已存在的列表尾部(最右边)。如果列表不存在,操作无效 */ -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 +func (this *Redis) RPushX(key string, values ...interface{}) (int64, error) { + return this.client.RPushX(this.getContext(), key, values...).Result() } diff --git a/lego/sys/redis/single/set.go b/lego/sys/redis/single/set.go index 2e8c86d43..beda0d4b2 100644 --- a/lego/sys/redis/single/set.go +++ b/lego/sys/redis/single/set.go @@ -1,30 +1,23 @@ package single -import "reflect" +import ( + "github.com/go-redis/redis/v8" +) /* Redis Sadd 命令将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略。 假如集合 key 不存在,则创建一个只包含添加的元素作成员的集合。 当集合 key 不是集合类型时,返回一个错误。 */ -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 +func (this *Redis) SAdd(key string, values ...interface{}) (int64, error) { + return this.client.SAdd(this.getContext(), key, values...).Result() } /* Redis Scard 命令返回集合中元素的数量 */ -func (this *Redis) SCard(key string) (result int64, err error) { - result, err = this.client.SCard(this.getContext(), key).Result() - return +func (this *Redis) SCard(key string) (int64, error) { + return this.client.SCard(this.getContext(), key).Result() } /* @@ -32,79 +25,43 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可 差集的结果来自前面的 FIRST_KEY ,而不是后面的 OTHER_KEY1,也不是整个 FIRST_KEY OTHER_KEY1..OTHER_KEYN 的差集。 实例: */ -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 +func (this *Redis) SDiff(keys ...string) *redis.StringSliceCmd { + return this.client.SDiff(this.getContext(), keys...) } /* Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中。 */ -func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SDiffStore(destination string, keys ...string) (int64, error) { + return this.client.SDiffStore(this.getContext(), destination, keys...).Result() } /* -Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 +Redis SInter 命令返回给定所有给定集合的交集。 不存在的集合 key 被视为空集。 当给定集合当中有一个空集时,结果也为空集(根据集合运算定律)。 */ -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 +func (this *Redis) SInter(keys ...string) *redis.StringSliceCmd { + return this.client.SInter(this.getContext(), keys...) } /* Redis Sinterstore 决定将给定集合之间的交集在指定的集合中。如果指定的集合已经存在,则将其覆盖 */ -func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SInterStore(destination string, keys ...string) (int64, error) { + return this.client.SInterStore(this.getContext(), destination, keys...).Result() } /* Redis Sismember 命令判断成员元素是否是集合的成员 */ -func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { - iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result() - return +func (this *Redis) SIsMember(key string, value interface{}) (bool, error) { + return this.client.SIsMember(this.getContext(), key, value).Result() } /* Redis Smembers 号召返回集合中的所有成员。 */ -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 +func (this *Redis) SMembers(key string) *redis.StringSliceCmd { + return this.client.SMembers(this.getContext(), key) } /* @@ -114,18 +71,16 @@ SMOVE 是原子性操作。 当 destination 集合已经包含 member 元素时, SMOVE 命令只是简单地将 source 集合中的 member 元素删除。 当 source 或 destination 不是集合类型时,返回一个错误。 */ -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 +func (this *Redis) SMove(source string, destination string, member interface{}) (bool, error) { + return this.client.SMove(this.getContext(), source, destination, member).Result() } /* Redis Spop命令用于移除集合中的指定键的一个或多个随机元素,移除后会返回移除的元素。 该命令类似于Srandmember命令,但SPOP将随机元素从集合中移除并返回,而Srandmember则返回元素,而不是对集合进行任何改动。 */ -func (this *Redis) Spop(key string) (result string, err error) { - result, err = this.client.SPop(this.getContext(), key).Result() - return +func (this *Redis) SPop(key string) (string, error) { + return this.client.SPop(this.getContext(), key).Result() } /* @@ -135,9 +90,8 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。 如果 count 为负数,那么命令返回一个数组,数组中的元素可能会重复出现多次,而数组的长度为 count 的绝对值。 该操作和 SPOP 相似,但 SPOP 将随机元素从集合中移除并返回,而 Srandmember 则仅仅返回随机元素,而不对集合进行任何改动。 */ -func (this *Redis) Srandmember(key string) (result string, err error) { - result, err = this.client.SRandMember(this.getContext(), key).Result() - return +func (this *Redis) SRandMember(key string) (string, error) { + return this.client.SRandMember(this.getContext(), key).Result() } /* @@ -145,41 +99,27 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在 当键不是集合类型,返回一个错误。 在 Redis 2.4 版本以前,SREM 只接受个别成员值。 */ -func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.SRem(this.getContext(), key, members...).Result() - return +func (this *Redis) SRem(key string, members ...interface{}) (int64, error) { + return this.client.SRem(this.getContext(), key, members...).Result() } /* Redis Sunion 命令返回给定集合的并集。 */ -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 +func (this *Redis) SUnion(keys ...string) *redis.StringSliceCmd { + return this.client.SUnion(this.getContext(), keys...) } /* Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 中。如果 destination 已经存在,则将其覆盖。 */ -func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { - result, err = this.client.SUnionStore(this.getContext(), destination, keys...).Result() - return +func (this *Redis) SUnionStore(destination string, keys ...string) (int64, error) { + return this.client.SUnionStore(this.getContext(), destination, keys...).Result() } /* Redis Sscan 用于继承集合中键的元素,Sscan 继承自Scan。 */ -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 +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() } diff --git a/lego/sys/redis/single/string.go b/lego/sys/redis/single/string.go index 468d6026b..be6957d37 100644 --- a/lego/sys/redis/single/string.go +++ b/lego/sys/redis/single/string.go @@ -1,7 +1,6 @@ package single import ( - "fmt" "time" "github.com/go-redis/redis/v8" @@ -11,54 +10,39 @@ import ( /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型。 */ -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 +func (this *Redis) Set(key string, v interface{}, expiration time.Duration) (string, error) { + return this.client.Set(this.getContext(), string(key), v, expiration).Result() } /* 指定的 key 不存在时,为 key 设置指定的值 */ -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 +func (this *Redis) SetNX(key string, v interface{}, expiration time.Duration) (bool, error) { + return this.client.SetNX(this.getContext(), key, v, expiration).Result() } /* 同时设置一个或多个 key-value 对 */ -func (this *Redis) MSet(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSet(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSET") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* 命令用于所有给定 key 都不存在时,同时设置一个或多个 key-value 对 */ -func (this *Redis) MSetNX(keyvalues map[string]interface{}) (err error) { +func (this *Redis) MSetNX(keyvalues map[string]interface{}) error { agrs := make([]interface{}, 0) agrs = append(agrs, "MSETNX") for k, v := range keyvalues { - result, _ := this.Encode(v) - agrs = append(agrs, k, result) + agrs = append(agrs, k, v) } - err = this.client.Do(this.getContext(), agrs...).Err() - return + return this.client.Do(this.getContext(), agrs...).Err() } /* @@ -67,9 +51,8 @@ Redis Incr 命令将 key 中储存的数字值增一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内。 */ -func (this *Redis) Incr(key string) (err error) { - err = this.client.Do(this.getContext(), "INCR", key).Err() - return +func (this *Redis) Incr(key string) (int64, error) { + return this.client.Incr(this.getContext(), key).Result() } /* @@ -78,18 +61,16 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) IncrBY(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "INCRBY", key, value).Err() - return +func (this *Redis) IncrBY(key string, value int64) (int64, error) { + return this.client.IncrBy(this.getContext(), key, value).Result() } /* Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量值。 如果 key 不存在,那么 INCRBYFLOAT 会先将 key 的值设为 0 ,再执行加法操作 */ -func (this *Redis) Incrbyfloat(key string, value float32) (err error) { - err = this.client.Do(this.getContext(), "INCRBYFLOAT", key, value).Err() - return +func (this *Redis) Incrbyfloat(key string, value float64) (float64, error) { + return this.client.IncrByFloat(this.getContext(), key, value).Result() } /* @@ -98,9 +79,8 @@ Redis Decr 命令将 key 中储存的数字值减一。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) Decr(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECR", key, value).Err() - return +func (this *Redis) Decr(key string, value int) (int64, error) { + return this.client.Decr(this.getContext(), key).Result() } /* @@ -109,9 +89,8 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。 如果值包含错误的类型,或字符串类型的值不能表示为数字,那么返回一个错误。 本操作的值限制在 64 位(bit)有符号数字表示之内 */ -func (this *Redis) DecrBy(key string, value int) (err error) { - err = this.client.Do(this.getContext(), "DECRBY", key, value).Err() - return +func (this *Redis) DecrBy(key string, value int64) (int64, error) { + return this.client.DecrBy(this.getContext(), key, value).Result() } /* @@ -119,62 +98,28 @@ Redis Append 命令用于为指定的 key 追加值。 如果 key 已经存在并且是一个字符串, APPEND 命令将 value 追加到 key 原来的值的末尾。 如果 key 不存在, APPEND 就简单地将给定 key 设为 value ,就像执行 SET key value 一样。 */ -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() - } +func (this *Redis) Append(key string, value string) (err error) { + this.client.Append(this.getContext(), key, value).Result() return } /* 命令用于设置给定 key 的值。如果 key 已经存储其他值, SET 就覆写旧值,且无视类型 */ -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 +func (this *Redis) Get(key string, v interface{}) (err error) { + return this.client.Get(this.getContext(), key).Scan(v) } /* 设置指定 key 的值,并返回 key 的旧值 */ -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 +func (this *Redis) GetSet(key string, v interface{}, result interface{}) (err error) { + return this.client.GetSet(this.getContext(), key, v).Scan(result) } /* 返回所有(一个或多个)给定 key 的值。 如果给定的 key 里面,有某个 key 不存在,那么这个 key 返回特殊值 nil */ -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 +func (this *Redis) MGet(keys ...string) *redis.SliceCmd { + return this.client.MGet(this.getContext(), keys...) } diff --git a/lego/sys/redis/single/zset.go b/lego/sys/redis/single/zset.go index cadf0f13e..1bf044e86 100644 --- a/lego/sys/redis/single/zset.go +++ b/lego/sys/redis/single/zset.go @@ -1,217 +1,145 @@ package single import ( - "reflect" - "github.com/go-redis/redis/v8" ) /* Redis ZAdd 向有序集合添加一个或多个成员,或者更新已存在成员的分数 */ -func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) { - this.client.ZAdd(this.getContext(), key, members...) - return +func (this *Redis) ZAdd(key string, members ...*redis.Z) (int64, error) { + return this.client.ZAdd(this.getContext(), key, members...).Result() } /* Redis Zcard 用于计算集合中元素的数量。 */ -func (this *Redis) ZCard(key string) (result int64, err error) { - result, err = this.client.ZCard(this.getContext(), key).Result() - return +func (this *Redis) ZCard(key string) (int64, error) { + return this.client.ZCard(this.getContext(), key).Result() } /* Redis ZCount 用于计算集合中指定的范围内的数量 */ -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 +func (this *Redis) ZCount(key string, min string, max string) (int64, error) { + return this.client.ZCount(this.getContext(), key, min, max).Result() } /* Redis ZIncrBy 有序集合中对指定成员的分数加上增量 increment */ -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 +func (this *Redis) ZIncrBy(key string, increment float64, member string) (float64, error) { + return this.client.ZIncrBy(this.getContext(), key, increment, member).Result() } /* Redis ZInterStore 计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 destination 中 */ -func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZInterStore(this.getContext(), destination, store).Result() - return +func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (int64, error) { + return this.client.ZInterStore(this.getContext(), destination, store).Result() } /* Redis ZLexCount 在有序集合中计算指定字典区间内成员数量 */ -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 +func (this *Redis) ZLexCount(key string, min string, max string) (int64, error) { + return this.client.ZLexCount(this.getContext(), key, min, max).Result() } /* Redis ZRange 通过索引区间返回有序集合指定区间内的成员 */ -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 +func (this *Redis) ZRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRange(this.getContext(), key, start, stop) } /* Redis ZRangeByLex 通过字典区间返回有序集合的成员 */ -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 +func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByLex(this.getContext(), key, opt) } /* Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员 */ -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 +func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRangeByScore(this.getContext(), key, opt) } /* Redis ZRank 返回有序集合中指定成员的索引 */ -func (this *Redis) ZRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRank(key string, member string) (int64, error) { + return this.client.ZRank(this.getContext(), key, member).Result() } /* Redis ZRem 移除有序集合中的一个或多个成员 */ -func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) { - result, err = this.client.ZRem(this.getContext(), key, members...).Result() - return +func (this *Redis) ZRem(key string, members ...interface{}) (int64, error) { + return this.client.ZRem(this.getContext(), key, members...).Result() } /* Redis ZRemRangeByLex 移除有序集合中给定的字典区间的所有成员 */ -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 +func (this *Redis) ZRemRangeByLex(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByLex(this.getContext(), key, min, max).Result() } /* Redis ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员 */ -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 +func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (int64, error) { + return this.client.ZRemRangeByRank(this.getContext(), key, start, stop).Result() } /* Redis ZRemRangeByScore 移除有序集合中给定的分数区间的所有成员 */ -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 +func (this *Redis) ZRemRangeByScore(key string, min string, max string) (int64, error) { + return this.client.ZRemRangeByScore(this.getContext(), key, min, max).Result() } /* Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分数从高到低 ZREVRANGE */ -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 +func (this *Redis) ZRevRange(key string, start int64, stop int64) *redis.StringSliceCmd { + return this.client.ZRevRange(this.getContext(), key, start, stop) } /* Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -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 +func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy) *redis.StringSliceCmd { + return this.client.ZRevRangeByScore(this.getContext(), key, opt) } /* Redis ZRevRank 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZRevRank(key string, member string) (result int64, err error) { - result, err = this.client.ZRevRank(this.getContext(), key, member).Result() - return +func (this *Redis) ZRevRank(key string, member string) (int64, error) { + return this.client.ZRevRank(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 */ -func (this *Redis) ZScore(key string, member string) (result float64, err error) { - result, err = this.client.ZScore(this.getContext(), key, member).Result() - return +func (this *Redis) ZScore(key string, member string) (float64, error) { + return this.client.ZScore(this.getContext(), key, member).Result() } /* Redis ZScore 返回有序集中指定分数区间内的成员,分数从高到低排序 ZUNIONSTORE */ -func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) { - result, err = this.client.ZUnionStore(this.getContext(), dest, store).Result() - return +func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (int64, error) { + return this.client.ZUnionStore(this.getContext(), dest, store).Result() } /* Redis ZScan 迭代有序集合中的元素(包括元素成员和元素分值) */ -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 +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() } diff --git a/lego/sys/redis/sys_test.go b/lego/sys/redis/sys_test.go index 7d924ce30..9de853ab5 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.ExpireKey("liwei1dao", 120); err != nil { + if _, err = redis.Expire("liwei1dao", time.Second*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) - fmt.Printf("Redis index:%d result:%d err:%v \n", index, result, err) + result, err := redis.SetNX("liwei1dao", index, -1) + fmt.Printf("Redis index:%d result:%v err:%v \n", index, result, err) wg.Done() }(i) }