This commit is contained in:
zhaocy 2022-07-15 12:01:27 +08:00
commit 909fe0f9f4
18 changed files with 327 additions and 390 deletions

View File

@ -17,6 +17,9 @@ func NewSys(RedisUrl []string, RedisPassword string, timeOut time.Duration,
client = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: RedisUrl,
Password: RedisPassword,
DialTimeout: timeOut,
ReadTimeout: timeOut,
WriteTimeout: timeOut,
})
sys = &Redis{
client: client,
@ -33,20 +36,20 @@ type Redis struct {
codec core.ICodec
}
func (this *Redis) getContext() (ctx context.Context) {
ctx, _ = context.WithTimeout(context.Background(), this.timeOut)
return
}
///事务
func (this *Redis) Close() (err error) {
err = this.client.Close()
return
}
/// Context
func (this *Redis) Context() context.Context {
return this.client.Context()
}
/// Ping
func (this *Redis) Ping() (string, error) {
return this.client.Ping(this.getContext()).Result()
return this.client.Ping(this.client.Context()).Result()
}
/// 命令接口
@ -78,8 +81,8 @@ func (this *Redis) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...
//锁
func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
cmd := redis.NewBoolCmd(this.getContext(), "set", key, 1, "ex", outTime, "nx")
this.client.Process(this.getContext(), cmd)
cmd := redis.NewBoolCmd(this.client.Context(), "set", key, 1, "ex", outTime, "nx")
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
return
}
@ -93,17 +96,18 @@ func (this *Redis) UnLock(key string) (err error) {
//lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd {
script := redis.NewScript(src)
return script.Load(this.getContext(), this.client)
return script.Load(this.Context(), this.client)
}
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(this.getContext(), script, keys, args...)
func (this *Redis) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(ctx, script, keys, args...)
}
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(this.getContext(), sha1, keys, args...)
func (this *Redis) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(ctx, sha1, keys, args...)
}
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(this.getContext(), hashes...)
}
func (this *Redis) ScriptLoad(script string) *redis.StringCmd {
return this.client.ScriptLoad(this.getContext(), script)
func (this *Redis) ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(ctx, hashes...)
}
// func (this *Redis) ScriptLoad(ctx context.Context, script string) *redis.StringCmd {
// return this.client.ScriptLoad(ctx, script)
// }

View File

@ -14,7 +14,7 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
for _, v := range fields {
agrs = append(agrs, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -22,7 +22,7 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
Redis Hexists 命令用于查看哈希表的指定字段是否存在
*/
func (this *Redis) HExists(key string, field string) (result bool, err error) {
result, err = this.client.Do(this.getContext(), "HEXISTS", key, field).Bool()
result, err = this.client.Do(this.client.Context(), "HEXISTS", key, field).Bool()
return
}
@ -42,7 +42,7 @@ func (this *Redis) HMSet(key string, v interface{}) (err error) {
for k, v := range data {
agrs = append(agrs, k, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -53,7 +53,7 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
for k, v := range v {
agrs = append(agrs, k, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -61,8 +61,8 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
Redis Hget 命令用于返回哈希表中指定字段的值
*/
func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "HGET", key, field)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "HGET", key, field)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
if len(_result) == 0 {
@ -79,8 +79,8 @@ Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/
func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.client.Context(), cmd)
var _result map[string]string
if _result, err = cmd.Result(); err == nil {
if len(_result) == 0 {
@ -96,8 +96,8 @@ func (this *Redis) HGetAll(key string, v interface{}) (err error) {
读取全部hash集合数据到map中
*/
func (this *Redis) HGetAllToMapString(key string) (result map[string]string, err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.client.Context(), cmd)
if result, err = cmd.Result(); err == nil {
if len(result) == 0 {
err = redis.Nil
@ -116,7 +116,7 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
本操作的值被限制在 64 (bit)有符号数字表示之内
*/
func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err()
err = this.client.Do(this.client.Context(), "HINCRBY", key, field, value).Err()
return
}
@ -125,7 +125,7 @@ Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数
如果指定的字段不存在那么在执行命令前字段的值被初始化为 0
*/
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) {
err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err()
err = this.client.Do(this.client.Context(), "HINCRBYFLOAT", key, field, value).Err()
return
}
@ -133,8 +133,8 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er
Redis Hkeys 命令用于获取哈希表中的所有域(field)
*/
func (this *Redis) Hkeys(key string) (result []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringSliceCmd(this.client.Context(), "HKEYS", key)
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
return
}
@ -143,7 +143,7 @@ func (this *Redis) Hkeys(key string) (result []string, err error) {
Redis Hlen 命令用于获取哈希表中字段的数量
*/
func (this *Redis) Hlen(key string) (result int, err error) {
result, err = this.client.Do(this.getContext(), "HLEN", key).Int()
result, err = this.client.Do(this.client.Context(), "HLEN", key).Int()
return
}
@ -158,8 +158,8 @@ func (this *Redis) HMGet(key string, v interface{}, fields ...string) (err error
for _, v := range fields {
agrs = append(agrs, v)
}
cmd := redis.NewStringStringMapCmd(this.getContext(), agrs...)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringStringMapCmd(this.client.Context(), agrs...)
this.client.Process(this.client.Context(), cmd)
var _result map[string]string
if _result, err = cmd.Result(); err == nil {
if len(_result) == 0 {
@ -179,7 +179,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil {
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
err = this.client.Do(this.client.Context(), "HSET", key, field, resultvalue).Err()
}
return
}
@ -193,7 +193,7 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil {
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
err = this.client.Do(this.client.Context(), "HSETNX", key, field, resultvalue).Err()
}
return
}

View File

@ -8,78 +8,78 @@ import (
///删除redis key
func (this *Redis) Delete(key string) (err error) {
err = this.client.Do(this.getContext(), "DEL", key).Err()
err = this.client.Do(this.client.Context(), "DEL", key).Err()
return
}
///判断是否存在key
func (this *Redis) ExistsKey(key string) (iskeep bool, err error) {
iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool()
iskeep, err = this.client.Do(this.client.Context(), "EXISTS", key).Bool()
return
}
///设置key的过期时间 单位以秒级
func (this *Redis) Expire(key string, expiration time.Duration) (err error) {
this.client.Expire(this.getContext(), key, expiration)
this.client.Expire(this.client.Context(), key, expiration)
return
}
///设置key的过期时间戳 秒级时间戳
func (this *Redis) ExpireAt(key string, tm time.Time) (err error) {
err = this.client.ExpireAt(this.getContext(), key, tm).Err()
err = this.client.ExpireAt(this.client.Context(), key, tm).Err()
return
}
///设置key的过期时间 单位以毫秒级
func (this *Redis) PExpire(key string, expiration time.Duration) (err error) {
err = this.client.PExpire(this.getContext(), key, expiration).Err()
err = this.client.PExpire(this.client.Context(), key, expiration).Err()
return
}
///设置key的过期时间戳 单位以豪秒级
func (this *Redis) PExpireAt(key string, tm time.Time) (err error) {
err = this.client.PExpireAt(this.getContext(), key, tm).Err()
err = this.client.PExpireAt(this.client.Context(), key, tm).Err()
return
}
///移除Key的过期时间
func (this *Redis) Persist(key string) (err error) {
err = this.client.Persist(this.getContext(), key).Err()
err = this.client.Persist(this.client.Context(), key).Err()
return
}
///获取key剩余过期时间 单位毫秒
func (this *Redis) PTTL(key string) (leftexpire time.Duration, err error) {
leftexpire, err = this.client.PTTL(this.getContext(), key).Result()
leftexpire, err = this.client.PTTL(this.client.Context(), key).Result()
return
}
///获取key剩余过期时间 单位秒
func (this *Redis) TTL(key string) (leftexpire time.Duration, err error) {
leftexpire, err = this.client.TTL(this.getContext(), key).Result()
leftexpire, err = this.client.TTL(this.client.Context(), key).Result()
return
}
///重命名Key
func (this *Redis) Rename(oldkey string, newkey string) (err error) {
err = this.client.Rename(this.getContext(), oldkey, newkey).Err()
err = this.client.Rename(this.client.Context(), oldkey, newkey).Err()
return
}
///重命名key 在新的 key 不存在时修改 key 的名称
func (this *Redis) RenameNX(oldkey string, newkey string) (err error) {
err = this.client.RenameNX(this.getContext(), oldkey, newkey).Err()
err = this.client.RenameNX(this.client.Context(), oldkey, newkey).Err()
return
}
///判断是否存在key pattern:key*
func (this *Redis) Keys(pattern string) (keys []string, err error) {
keys, err = this.client.Keys(this.getContext(), pattern).Result()
keys, err = this.client.Keys(this.client.Context(), pattern).Result()
return
}
///获取键类型
func (this *Redis) Type(key string) (ty string, err error) {
ty, err = this.client.Type(this.getContext(), key).Result()
ty, err = this.client.Type(this.client.Context(), key).Result()
return
}

View File

@ -8,8 +8,8 @@ import (
Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/
func (this *Redis) Lindex(key string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "LINDEX", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "LINDEX", key)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
if len(_result) == 0 {
@ -38,9 +38,9 @@ func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value i
return
}
if isbefore {
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
} else {
err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err()
}
return
}
@ -49,7 +49,7 @@ func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value i
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()
result, err = this.client.Do(this.client.Context(), "LLEN", key).Int()
return
}
@ -57,8 +57,8 @@ func (this *Redis) Llen(key string) (result int, err error) {
Redis Lpop 命令用于移除并返回列表的第一个元素
*/
func (this *Redis) LPop(key string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "LPOP", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "LPOP", key)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -76,7 +76,7 @@ func (this *Redis) LPush(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -90,7 +90,7 @@ func (this *Redis) LPushX(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -100,8 +100,8 @@ Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START
*/
func (this *Redis) LRange(key string, start, end int, v interface{}) (err error) {
var _result []string
cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringSliceCmd(this.client.Context(), "LRANGE", key, start, end)
this.client.Process(this.client.Context(), cmd)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -120,7 +120,7 @@ func (this *Redis) LRem(key string, count int, target interface{}) (err error) {
if resultvalue, err = this.codec.Marshal(target); err != nil {
return
}
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LREM", key, count, resultvalue).Err()
return
}
@ -133,7 +133,7 @@ func (this *Redis) LSet(key string, index int, value interface{}) (err error) {
if resultvalue, err = this.codec.Marshal(value); err == nil {
return
}
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LSET", key, index, resultvalue).Err()
return
}
@ -143,7 +143,7 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留
-1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/
func (this *Redis) Ltrim(key string, start, stop int) (err error) {
err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err()
err = this.client.Do(this.client.Context(), "LTRIM", key, start, stop).Err()
return
}
@ -151,8 +151,8 @@ func (this *Redis) Ltrim(key string, start, stop int) (err error) {
Redis Rpop 命令用于移除列表的最后一个元素返回值为移除的元素
*/
func (this *Redis) Rpop(key string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "RPOP", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "RPOP", key)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -164,8 +164,8 @@ func (this *Redis) Rpop(key string, v interface{}) (err error) {
Redis Rpoplpush 命令用于移除列表的最后一个元素并将该元素添加到另一个列表并返回
*/
func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "RPOPLPUSH", oldkey, newkey)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "RPOPLPUSH", oldkey, newkey)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -185,7 +185,7 @@ func (this *Redis) RPush(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -199,6 +199,6 @@ func (this *Redis) RPushX(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}

View File

@ -13,7 +13,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -21,7 +21,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
Redis Scard 命令返回集合中元素的数量
*/
func (this *Redis) SCard(key string) (result int64, err error) {
result, err = this.client.SCard(this.getContext(), key).Result()
result, err = this.client.SCard(this.client.Context(), key).Result()
return
}
@ -32,7 +32,7 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可
*/
func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
var _result []string
cmd := this.client.SDiff(this.getContext(), keys...)
cmd := this.client.SDiff(this.client.Context(), keys...)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -43,7 +43,7 @@ func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中
*/
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) {
result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result()
result, err = this.client.SDiffStore(this.client.Context(), destination, keys...).Result()
return
}
@ -52,7 +52,7 @@ Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集
*/
func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
var _result []string
cmd := this.client.SInter(this.getContext(), keys...)
cmd := this.client.SInter(this.client.Context(), keys...)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -63,7 +63,7 @@ func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖
*/
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) {
result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result()
result, err = this.client.SInterStore(this.client.Context(), destination, keys...).Result()
return
}
@ -71,7 +71,7 @@ func (this *Redis) SInterStore(destination string, keys ...string) (result int64
Redis Sismember 命令判断成员元素是否是集合的成员
*/
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) {
iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result()
iskeep, err = this.client.SIsMember(this.client.Context(), key, value).Result()
return
}
@ -80,7 +80,7 @@ Redis Smembers 号召返回集合中的所有成员。
*/
func (this *Redis) SMembers(v interface{}, key string) (err error) {
var _result []string
cmd := this.client.SMembers(this.getContext(), key)
cmd := this.client.SMembers(this.client.Context(), key)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -95,7 +95,7 @@ SMOVE 是原子性操作。
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()
result, err = this.client.SMove(this.client.Context(), source, destination, member).Result()
return
}
@ -104,7 +104,7 @@ Redis Spop命令用于移除集合中的指定键的一个或多个随机元素
该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动
*/
func (this *Redis) Spop(key string) (result string, err error) {
result, err = this.client.SPop(this.getContext(), key).Result()
result, err = this.client.SPop(this.client.Context(), key).Result()
return
}
@ -116,7 +116,7 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。
该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动
*/
func (this *Redis) Srandmember(key string) (result string, err error) {
result, err = this.client.SRandMember(this.getContext(), key).Result()
result, err = this.client.SRandMember(this.client.Context(), key).Result()
return
}
@ -126,7 +126,7 @@ 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()
result, err = this.client.SRem(this.client.Context(), key, members...).Result()
return
}
@ -135,7 +135,7 @@ Redis Sunion 命令返回给定集合的并集。
*/
func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
var _result []string
cmd := this.client.SUnion(this.getContext(), keys...)
cmd := this.client.SUnion(this.client.Context(), keys...)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -146,7 +146,7 @@ func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
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()
result, err = this.client.SUnionStore(this.client.Context(), destination, keys...).Result()
return
}
@ -154,6 +154,6 @@ func (this *Redis) Sunionstore(destination string, keys ...string) (result int64
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()
keys, cursor, err = this.client.SScan(this.client.Context(), key, _cursor, match, count).Result()
return
}

View File

@ -15,7 +15,7 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
if result, err = this.codec.Marshal(value); err != nil {
return
}
err = this.client.Set(this.getContext(), key, result, expiration).Err()
err = this.client.Set(this.client.Context(), key, result, expiration).Err()
return
}
@ -23,8 +23,8 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
指定的 key 不存在时 key 设置指定的值
*/
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewIntCmd(this.client.Context(), "SETNX", key, value)
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
// }
return
@ -40,7 +40,7 @@ func (this *Redis) MSet(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -54,7 +54,7 @@ func (this *Redis) MSetNX(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -65,7 +65,7 @@ Redis Incr 命令将 key 中储存的数字值增一。
本操作的值限制在 64 (bit)有符号数字表示之内
*/
func (this *Redis) Incr(key string) (err error) {
err = this.client.Do(this.getContext(), "INCR", key).Err()
err = this.client.Do(this.client.Context(), "INCR", key).Err()
return
}
@ -76,7 +76,7 @@ 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()
err = this.client.Do(this.client.Context(), "INCRBY", key, value).Err()
return
}
@ -85,7 +85,7 @@ 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()
err = this.client.Do(this.client.Context(), "INCRBYFLOAT", key, value).Err()
return
}
@ -96,7 +96,7 @@ 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()
err = this.client.Do(this.client.Context(), "DECR", key, value).Err()
return
}
@ -107,7 +107,7 @@ 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()
err = this.client.Do(this.client.Context(), "DECRBY", key, value).Err()
return
}
@ -121,7 +121,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
if result, err = this.codec.Marshal(value); err != nil {
return
}
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
err = this.client.Do(this.client.Context(), "APPEND", key, result).Err()
return
}
@ -130,7 +130,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
*/
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 {
if result, err = this.client.Get(this.client.Context(), key).Bytes(); err == nil {
err = this.codec.Unmarshal(result, value)
}
return
@ -144,8 +144,8 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
_value []byte
)
if _value, err = this.codec.Marshal(value); err == nil {
cmd := redis.NewStringCmd(this.getContext(), "GETSET", key, _value)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "GETSET", key, _value)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, result)
@ -163,8 +163,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
for _, v := range keys {
agrs = append(agrs, v)
}
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringSliceCmd(this.client.Context(), agrs...)
this.client.Process(this.client.Context(), cmd)
var result []string
if result, err = cmd.Result(); err != nil {
return
@ -175,8 +175,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
///判断是否存在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)
cmd := redis.NewIntCmd(this.client.Context(), "INCRBY", key, amount)
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
return
}

View File

@ -8,7 +8,7 @@ import (
Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数
*/
func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) {
this.client.ZAdd(this.getContext(), key, members...)
this.client.ZAdd(this.client.Context(), key, members...)
return
}
@ -16,7 +16,7 @@ func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) {
Redis Zcard 用于计算集合中元素的数量
*/
func (this *Redis) ZCard(key string) (result int64, err error) {
result, err = this.client.ZCard(this.getContext(), key).Result()
result, err = this.client.ZCard(this.client.Context(), key).Result()
return
}
@ -24,7 +24,7 @@ func (this *Redis) ZCard(key string) (result int64, err error) {
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()
result, err = this.client.ZCount(this.client.Context(), key, min, max).Result()
return
}
@ -32,7 +32,7 @@ func (this *Redis) ZCount(key string, min string, max string) (result int64, err
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()
result, err = this.client.ZIncrBy(this.client.Context(), key, increment, member).Result()
return
}
@ -40,7 +40,7 @@ func (this *Redis) ZIncrBy(key string, increment float64, member string) (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()
result, err = this.client.ZInterStore(this.client.Context(), destination, store).Result()
return
}
@ -48,7 +48,7 @@ func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (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()
result, err = this.client.ZLexCount(this.client.Context(), key, min, max).Result()
return
}
@ -57,7 +57,7 @@ Redis ZRange 通过索引区间返回有序集合指定区间内的成员
*/
func (this *Redis) ZRange(key string, start int64, stop int64, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRange(this.getContext(), key, start, stop)
cmd := this.client.ZRange(this.client.Context(), key, start, stop)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -69,7 +69,7 @@ Redis ZRangeByLex 通过字典区间返回有序集合的成员
*/
func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRangeByLex(this.getContext(), key, opt)
cmd := this.client.ZRangeByLex(this.client.Context(), key, opt)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -81,7 +81,7 @@ Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员
*/
func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRangeByScore(this.getContext(), key, opt)
cmd := this.client.ZRangeByScore(this.client.Context(), key, opt)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -92,7 +92,7 @@ func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy, v interface{})
Redis ZRank 返回有序集合中指定成员的索引
*/
func (this *Redis) ZRank(key string, member string) (result int64, err error) {
result, err = this.client.ZRank(this.getContext(), key, member).Result()
result, err = this.client.ZRank(this.client.Context(), key, member).Result()
return
}
@ -100,7 +100,7 @@ func (this *Redis) ZRank(key string, member string) (result int64, err error) {
Redis ZRem 移除有序集合中的一个或多个成员
*/
func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) {
result, err = this.client.ZRem(this.getContext(), key, members...).Result()
result, err = this.client.ZRem(this.client.Context(), key, members...).Result()
return
}
@ -108,7 +108,7 @@ func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err e
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()
result, err = this.client.ZRemRangeByLex(this.client.Context(), key, min, max).Result()
return
}
@ -116,7 +116,7 @@ func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result in
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()
result, err = this.client.ZRemRangeByRank(this.client.Context(), key, start, stop).Result()
return
}
@ -124,7 +124,7 @@ func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (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()
result, err = this.client.ZRemRangeByScore(this.client.Context(), key, min, max).Result()
return
}
@ -133,7 +133,7 @@ Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分
*/
func (this *Redis) ZRevRange(key string, start int64, stop int64, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRevRange(this.getContext(), key, start, stop)
cmd := this.client.ZRevRange(this.client.Context(), key, start, stop)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -145,7 +145,7 @@ Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数
*/
func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt)
cmd := this.client.ZRevRangeByScore(this.client.Context(), key, opt)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -156,7 +156,7 @@ func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy, v interface
Redis ZRevRank 返回有序集中指定分数区间内的成员分数从高到低排序
*/
func (this *Redis) ZRevRank(key string, member string) (result int64, err error) {
result, err = this.client.ZRevRank(this.getContext(), key, member).Result()
result, err = this.client.ZRevRank(this.client.Context(), key, member).Result()
return
}
@ -164,7 +164,7 @@ func (this *Redis) ZRevRank(key string, member string) (result int64, err error)
Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序
*/
func (this *Redis) ZScore(key string, member string) (result float64, err error) {
result, err = this.client.ZScore(this.getContext(), key, member).Result()
result, err = this.client.ZScore(this.client.Context(), key, member).Result()
return
}
@ -172,7 +172,7 @@ func (this *Redis) ZScore(key string, member string) (result float64, err error)
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()
result, err = this.client.ZUnionStore(this.client.Context(), dest, store).Result()
return
}
@ -180,6 +180,6 @@ func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64,
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()
keys, cursor, err = this.client.ZScan(this.client.Context(), key, _cursor, match, count).Result()
return
}

View File

@ -10,6 +10,7 @@ import (
type (
IRedis interface {
Close() (err error)
Context() context.Context
Do(ctx context.Context, args ...interface{}) *redis.Cmd
Lock(key string, outTime int) (result bool, err error)
UnLock(key string) (err error)
@ -116,13 +117,15 @@ type (
/*Lua Script*/
NewScript(src string) *redis.StringCmd
Eval(script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(hashes ...string) *redis.BoolSliceCmd
Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
// ScriptLoad(ctx context.Context, script string) *redis.StringCmd
}
ISys interface {
IRedis
GetClient() IRedis
/*Lock*/
NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error)
}
@ -148,6 +151,15 @@ func NewSys(option ...Option) (sys ISys, err error) {
func Close() (err error) {
return defsys.Close()
}
func GetClient() IRedis {
return defsys.GetClient()
}
func Context() context.Context {
return defsys.Context()
}
func Do(ctx context.Context, args ...interface{}) *redis.Cmd {
return defsys.Do(ctx, args...)
}
@ -457,12 +469,16 @@ func ZScan(key string, _cursor uint64, match string, count int64) (keys []string
func NewScript(src string) *redis.StringCmd {
return defsys.NewScript(src)
}
func Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(script, keys, args...)
func Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(ctx, script, keys, args...)
}
func EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(sha1, keys, args...)
func EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.EvalSha(ctx, sha1, keys, args...)
}
func ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return defsys.ScriptExists(hashes...)
func ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return defsys.ScriptExists(ctx, hashes...)
}
// func ScriptLoad(ctx context.Context, script string) *redis.StringCmd {
// return defsys.ScriptLoad(ctx, script)
// }

View File

@ -20,7 +20,6 @@ type Options struct {
Redis_Single_Addr string
Redis_Single_Password string
Redis_Single_DB int
Redis_Single_PoolSize int
Redis_Cluster_Addr []string
Redis_Cluster_Password string
TimeOut time.Duration
@ -51,11 +50,6 @@ func SetRedis_Single_DB(v int) Option {
}
}
func SetRedis_Single_PoolSize(v int) Option {
return func(o *Options) {
o.Redis_Single_PoolSize = v
}
}
func SetRedis_Cluster_Addr(v []string) Option {
return func(o *Options) {
o.Redis_Cluster_Addr = v
@ -88,7 +82,6 @@ func newOptions(config map[string]interface{}, opts ...Option) Options {
Redis_Cluster_Addr: []string{"127.0.0.1:6379"},
Redis_Cluster_Password: "",
TimeOut: time.Second * 3,
Redis_Single_PoolSize: 100,
}
if config != nil {
mapstructure.Decode(config, &options)
@ -107,7 +100,6 @@ func newOptionsByOption(opts ...Option) Options {
Redis_Cluster_Addr: []string{"127.0.0.1:6379"},
Redis_Cluster_Password: "",
TimeOut: time.Second * 3,
Redis_Single_PoolSize: 100,
}
for _, o := range opts {
o(&options)

View File

@ -29,7 +29,6 @@ func (this *Redis) init() (err error) {
this.options.Redis_Single_Addr,
this.options.Redis_Single_Password,
this.options.Redis_Single_DB,
this.options.Redis_Single_PoolSize,
this.options.TimeOut,
this,
)
@ -48,6 +47,14 @@ func (this *Redis) init() (err error) {
func (this *Redis) Close() (err error) {
return this.client.Close()
}
func (this *Redis) GetClient() IRedis {
return this.client
}
func (this *Redis) Context() context.Context {
return this.client.Context()
}
func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd {
return this.client.Do(ctx, args...)
}
@ -353,16 +360,20 @@ func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64)
func (this *Redis) NewScript(src string) *redis.StringCmd {
return this.client.NewScript(src)
}
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(script, keys, args...)
func (this *Redis) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(ctx, script, keys, args...)
}
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(sha1, keys, args...)
func (this *Redis) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(ctx, sha1, keys, args...)
}
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(hashes...)
func (this *Redis) ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(ctx, hashes...)
}
// func (this *Redis) ScriptLoad(ctx context.Context, script string) *redis.StringCmd {
// return this.client.ScriptLoad(ctx, script)
// }
//Codec---------------------------------------------------------------------------------------------------------------------------------------
func (this *Redis) Marshal(v interface{}) ([]byte, error) {
if this.options.Codec != nil {

View File

@ -8,7 +8,7 @@ import (
"github.com/go-redis/redis/v8"
)
func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.Duration,
func NewSys(RedisUrl, RedisPassword string, RedisDB int, timeOut time.Duration,
codec core.ICodec,
) (sys *Redis, err error) {
var (
@ -18,11 +18,12 @@ func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.
Addr: RedisUrl,
Password: RedisPassword,
DB: RedisDB,
PoolSize: PoolSize, // 连接池大小
DialTimeout: timeOut,
WriteTimeout: timeOut,
ReadTimeout: timeOut,
})
sys = &Redis{
client: client,
timeOut: timeOut,
codec: codec,
}
_, err = sys.Ping()
@ -31,21 +32,21 @@ func NewSys(RedisUrl, RedisPassword string, RedisDB, PoolSize int, timeOut time.
type Redis struct {
client *redis.Client
timeOut time.Duration
codec core.ICodec
}
func (this *Redis) getContext() (ctx context.Context) {
ctx, _ = context.WithTimeout(context.Background(), this.timeOut)
return
}
func (this *Redis) Close() (err error) {
return this.client.Close()
}
/// Context
func (this *Redis) Context() context.Context {
return this.client.Context()
}
/// Ping
func (this *Redis) Ping() (string, error) {
return this.client.Ping(this.getContext()).Result()
return this.client.Ping(this.client.Context()).Result()
}
/// 命令接口
@ -77,8 +78,8 @@ func (this *Redis) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...
//锁
func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
cmd := redis.NewBoolCmd(this.getContext(), "set", key, 1, "ex", outTime, "nx")
this.client.Process(this.getContext(), cmd)
cmd := redis.NewBoolCmd(this.client.Context(), "set", key, 1, "ex", outTime, "nx")
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
return
}
@ -92,17 +93,18 @@ func (this *Redis) UnLock(key string) (err error) {
//lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd {
script := redis.NewScript(src)
return script.Load(this.getContext(), this.client)
return script.Load(this.Context(), this.client)
}
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(this.getContext(), script, keys, args...)
func (this *Redis) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(ctx, script, keys, args...)
}
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(this.getContext(), sha1, keys, args...)
func (this *Redis) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(ctx, sha1, keys, args...)
}
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(this.getContext(), hashes...)
}
func (this *Redis) ScriptLoad(script string) *redis.StringCmd {
return this.client.ScriptLoad(this.getContext(), script)
func (this *Redis) ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(ctx, hashes...)
}
// func (this *Redis) ScriptLoad(ctx context.Context, script string) *redis.StringCmd {
// return this.client.ScriptLoad(ctx, script)
// }

View File

@ -14,7 +14,7 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
for _, v := range fields {
agrs = append(agrs, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -22,7 +22,7 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
Redis Hexists 命令用于查看哈希表的指定字段是否存在
*/
func (this *Redis) HExists(key string, field string) (result bool, err error) {
result, err = this.client.Do(this.getContext(), "HEXISTS", key, field).Bool()
result, err = this.client.Do(this.client.Context(), "HEXISTS", key, field).Bool()
return
}
@ -42,7 +42,7 @@ func (this *Redis) HMSet(key string, v interface{}) (err error) {
for k, v := range data {
agrs = append(agrs, k, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
@ -52,7 +52,7 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
for k, v := range v {
agrs = append(agrs, k, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -60,8 +60,8 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
Redis Hget 命令用于返回哈希表中指定字段的值
*/
func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "HGET", key, field)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "HGET", key, field)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -74,8 +74,8 @@ Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/
func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.client.Context(), cmd)
var _result map[string]string
if _result, err = cmd.Result(); err == nil {
if len(_result) == 0 {
@ -90,8 +90,8 @@ func (this *Redis) HGetAll(key string, v interface{}) (err error) {
读取全部hash集合数据到map中
*/
func (this *Redis) HGetAllToMapString(key string) (result map[string]string, err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.client.Context(), cmd)
if result, err = cmd.Result(); err == nil {
if len(result) == 0 {
err = redis.Nil
@ -110,7 +110,7 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
本操作的值被限制在 64 (bit)有符号数字表示之内
*/
func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err()
err = this.client.Do(this.client.Context(), "HINCRBY", key, field, value).Err()
return
}
@ -119,7 +119,7 @@ Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数
如果指定的字段不存在那么在执行命令前字段的值被初始化为 0
*/
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) {
err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err()
err = this.client.Do(this.client.Context(), "HINCRBYFLOAT", key, field, value).Err()
return
}
@ -127,8 +127,8 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er
Redis Hkeys 命令用于获取哈希表中的所有域(field)
*/
func (this *Redis) Hkeys(key string) (result []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringSliceCmd(this.client.Context(), "HKEYS", key)
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
return
}
@ -137,7 +137,7 @@ func (this *Redis) Hkeys(key string) (result []string, err error) {
Redis Hlen 命令用于获取哈希表中字段的数量
*/
func (this *Redis) Hlen(key string) (result int, err error) {
result, err = this.client.Do(this.getContext(), "HLEN", key).Int()
result, err = this.client.Do(this.client.Context(), "HLEN", key).Int()
return
}
@ -152,8 +152,8 @@ func (this *Redis) HMGet(key string, v interface{}, fields ...string) (err error
for _, v := range fields {
agrs = append(agrs, v)
}
cmd := redis.NewStringStringMapCmd(this.getContext(), agrs...)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringStringMapCmd(this.client.Context(), agrs...)
this.client.Process(this.client.Context(), cmd)
var _result map[string]string
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalMap(_result, v)
@ -169,7 +169,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil {
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
err = this.client.Do(this.client.Context(), "HSET", key, field, resultvalue).Err()
}
return
}
@ -183,7 +183,7 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil {
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
err = this.client.Do(this.client.Context(), "HSETNX", key, field, resultvalue).Err()
}
return
}

View File

@ -6,78 +6,78 @@ import "time"
///删除redis key
func (this *Redis) Delete(key string) (err error) {
err = this.client.Do(this.getContext(), "DEL", key).Err()
err = this.client.Do(this.client.Context(), "DEL", key).Err()
return
}
///判断是否存在key
func (this *Redis) ExistsKey(key string) (iskeep bool, err error) {
iskeep, err = this.client.Do(this.getContext(), "EXISTS", key).Bool()
iskeep, err = this.client.Do(this.client.Context(), "EXISTS", key).Bool()
return
}
///设置key的过期时间 单位以秒级
func (this *Redis) Expire(key string, expiration time.Duration) (err error) {
this.client.Expire(this.getContext(), key, expiration)
this.client.Expire(this.client.Context(), key, expiration)
return
}
///设置key的过期时间戳 秒级时间戳
func (this *Redis) ExpireAt(key string, tm time.Time) (err error) {
err = this.client.ExpireAt(this.getContext(), key, tm).Err()
err = this.client.ExpireAt(this.client.Context(), key, tm).Err()
return
}
///设置key的过期时间 单位以毫秒级
func (this *Redis) PExpire(key string, expiration time.Duration) (err error) {
err = this.client.PExpire(this.getContext(), key, expiration).Err()
err = this.client.PExpire(this.client.Context(), key, expiration).Err()
return
}
///设置key的过期时间戳 单位以豪秒级
func (this *Redis) PExpireAt(key string, tm time.Time) (err error) {
err = this.client.PExpireAt(this.getContext(), key, tm).Err()
err = this.client.PExpireAt(this.client.Context(), key, tm).Err()
return
}
///移除Key的过期时间
func (this *Redis) Persist(key string) (err error) {
err = this.client.Persist(this.getContext(), key).Err()
err = this.client.Persist(this.client.Context(), key).Err()
return
}
///获取key剩余过期时间 单位毫秒
func (this *Redis) PTTL(key string) (leftexpire time.Duration, err error) {
leftexpire, err = this.client.PTTL(this.getContext(), key).Result()
leftexpire, err = this.client.PTTL(this.client.Context(), key).Result()
return
}
///获取key剩余过期时间 单位秒
func (this *Redis) TTL(key string) (leftexpire time.Duration, err error) {
leftexpire, err = this.client.TTL(this.getContext(), key).Result()
leftexpire, err = this.client.TTL(this.client.Context(), key).Result()
return
}
///重命名Key
func (this *Redis) Rename(oldkey string, newkey string) (err error) {
err = this.client.Rename(this.getContext(), oldkey, newkey).Err()
err = this.client.Rename(this.client.Context(), oldkey, newkey).Err()
return
}
///重命名key 在新的 key 不存在时修改 key 的名称
func (this *Redis) RenameNX(oldkey string, newkey string) (err error) {
err = this.client.RenameNX(this.getContext(), oldkey, newkey).Err()
err = this.client.RenameNX(this.client.Context(), oldkey, newkey).Err()
return
}
///判断是否存在key pattern:key*
func (this *Redis) Keys(pattern string) (keys []string, err error) {
keys, err = this.client.Keys(this.getContext(), pattern).Result()
keys, err = this.client.Keys(this.client.Context(), pattern).Result()
return
}
///获取键类型
func (this *Redis) Type(key string) (ty string, err error) {
ty, err = this.client.Type(this.getContext(), key).Result()
ty, err = this.client.Type(this.client.Context(), key).Result()
return
}

View File

@ -8,8 +8,8 @@ import (
Redis Lindex 命令用于通过索引获取列表中的元素你也可以使用负数下标 -1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/
func (this *Redis) Lindex(key string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "LINDEX", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "LINDEX", key)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -34,9 +34,9 @@ func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value i
return
}
if isbefore {
err = this.client.Do(this.getContext(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LINSERT", key, "BEFORE", tagervalue, resultvalue).Err()
} else {
err = this.client.Do(this.getContext(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LINSERT", key, "AFTER", tagervalue, resultvalue).Err()
}
return
}
@ -45,7 +45,7 @@ func (this *Redis) Linsert(key string, isbefore bool, tager interface{}, value i
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()
result, err = this.client.Do(this.client.Context(), "LLEN", key).Int()
return
}
@ -53,8 +53,8 @@ func (this *Redis) Llen(key string) (result int, err error) {
Redis Lpop 命令用于移除并返回列表的第一个元素
*/
func (this *Redis) LPop(key string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "LPOP", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "LPOP", key)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -72,7 +72,7 @@ func (this *Redis) LPush(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -86,7 +86,7 @@ func (this *Redis) LPushX(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -96,8 +96,8 @@ Redis Lrange 返回列表中指定区间内的元素,区间以偏移量 START
*/
func (this *Redis) LRange(key string, start, end int, v interface{}) (err error) {
var _result []string
cmd := redis.NewStringSliceCmd(this.getContext(), "LRANGE", key, start, end)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringSliceCmd(this.client.Context(), "LRANGE", key, start, end)
this.client.Process(this.client.Context(), cmd)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -116,7 +116,7 @@ func (this *Redis) LRem(key string, count int, target interface{}) (err error) {
if resultvalue, err = this.codec.Marshal(target); err != nil {
return
}
err = this.client.Do(this.getContext(), "LREM", key, count, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LREM", key, count, resultvalue).Err()
return
}
@ -129,7 +129,7 @@ func (this *Redis) LSet(key string, index int, value interface{}) (err error) {
if resultvalue, err = this.codec.Marshal(value); err == nil {
return
}
err = this.client.Do(this.getContext(), "LSET", key, index, resultvalue).Err()
err = this.client.Do(this.client.Context(), "LSET", key, index, resultvalue).Err()
return
}
@ -139,7 +139,7 @@ Redis Ltrim 对一个列表进行修剪(trim),就是说,让列表只保留
-1 表示列表的最后一个元素 -2 表示列表的倒数第二个元素以此类推
*/
func (this *Redis) Ltrim(key string, start, stop int) (err error) {
err = this.client.Do(this.getContext(), "LTRIM", key, start, stop).Err()
err = this.client.Do(this.client.Context(), "LTRIM", key, start, stop).Err()
return
}
@ -147,8 +147,8 @@ func (this *Redis) Ltrim(key string, start, stop int) (err error) {
Redis Rpop 命令用于移除列表的最后一个元素返回值为移除的元素
*/
func (this *Redis) Rpop(key string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "RPOP", key)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "RPOP", key)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -160,8 +160,8 @@ func (this *Redis) Rpop(key string, v interface{}) (err error) {
Redis Rpoplpush 命令用于移除列表的最后一个元素并将该元素添加到另一个列表并返回
*/
func (this *Redis) RPopLPush(oldkey string, newkey string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "RPOPLPUSH", oldkey, newkey)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "RPOPLPUSH", oldkey, newkey)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v)
@ -181,7 +181,7 @@ func (this *Redis) RPush(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -195,6 +195,6 @@ func (this *Redis) RPushX(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}

View File

@ -13,7 +13,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -21,7 +21,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
Redis Scard 命令返回集合中元素的数量
*/
func (this *Redis) SCard(key string) (result int64, err error) {
result, err = this.client.SCard(this.getContext(), key).Result()
result, err = this.client.SCard(this.client.Context(), key).Result()
return
}
@ -32,7 +32,7 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可
*/
func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
var _result []string
cmd := this.client.SDiff(this.getContext(), keys...)
cmd := this.client.SDiff(this.client.Context(), keys...)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -43,7 +43,7 @@ func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中
*/
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) {
result, err = this.client.SDiffStore(this.getContext(), destination, keys...).Result()
result, err = this.client.SDiffStore(this.client.Context(), destination, keys...).Result()
return
}
@ -52,7 +52,7 @@ Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集
*/
func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
var _result []string
cmd := this.client.SInter(this.getContext(), keys...)
cmd := this.client.SInter(this.client.Context(), keys...)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -63,7 +63,7 @@ func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖
*/
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) {
result, err = this.client.SInterStore(this.getContext(), destination, keys...).Result()
result, err = this.client.SInterStore(this.client.Context(), destination, keys...).Result()
return
}
@ -71,7 +71,7 @@ func (this *Redis) SInterStore(destination string, keys ...string) (result int64
Redis Sismember 命令判断成员元素是否是集合的成员
*/
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) {
iskeep, err = this.client.SIsMember(this.getContext(), key, value).Result()
iskeep, err = this.client.SIsMember(this.client.Context(), key, value).Result()
return
}
@ -80,7 +80,7 @@ Redis Smembers 号召返回集合中的所有成员。
*/
func (this *Redis) SMembers(v interface{}, key string) (err error) {
var _result []string
cmd := this.client.SMembers(this.getContext(), key)
cmd := this.client.SMembers(this.client.Context(), key)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -95,7 +95,7 @@ SMOVE 是原子性操作。
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()
result, err = this.client.SMove(this.client.Context(), source, destination, member).Result()
return
}
@ -104,7 +104,7 @@ Redis Spop命令用于移除集合中的指定键的一个或多个随机元素
该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动
*/
func (this *Redis) Spop(key string) (result string, err error) {
result, err = this.client.SPop(this.getContext(), key).Result()
result, err = this.client.SPop(this.client.Context(), key).Result()
return
}
@ -116,7 +116,7 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。
该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动
*/
func (this *Redis) Srandmember(key string) (result string, err error) {
result, err = this.client.SRandMember(this.getContext(), key).Result()
result, err = this.client.SRandMember(this.client.Context(), key).Result()
return
}
@ -126,7 +126,7 @@ 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()
result, err = this.client.SRem(this.client.Context(), key, members...).Result()
return
}
@ -135,7 +135,7 @@ Redis Sunion 命令返回给定集合的并集。
*/
func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
var _result []string
cmd := this.client.SUnion(this.getContext(), keys...)
cmd := this.client.SUnion(this.client.Context(), keys...)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -146,7 +146,7 @@ func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
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()
result, err = this.client.SUnionStore(this.client.Context(), destination, keys...).Result()
return
}
@ -154,6 +154,6 @@ func (this *Redis) Sunionstore(destination string, keys ...string) (result int64
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()
keys, cursor, err = this.client.SScan(this.client.Context(), key, _cursor, match, count).Result()
return
}

View File

@ -15,7 +15,7 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
if result, err = this.codec.Marshal(value); err != nil {
return
}
err = this.client.Set(this.getContext(), key, result, expiration).Err()
err = this.client.Set(this.client.Context(), key, result, expiration).Err()
return
}
@ -23,8 +23,8 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
指定的 key 不存在时 key 设置指定的值
*/
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewIntCmd(this.client.Context(), "SETNX", key, value)
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
// }
return
@ -40,7 +40,7 @@ func (this *Redis) MSet(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -54,7 +54,7 @@ func (this *Redis) MSetNX(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result)
}
err = this.client.Do(this.getContext(), agrs...).Err()
err = this.client.Do(this.client.Context(), agrs...).Err()
return
}
@ -65,7 +65,7 @@ Redis Incr 命令将 key 中储存的数字值增一。
本操作的值限制在 64 (bit)有符号数字表示之内
*/
func (this *Redis) Incr(key string) (err error) {
err = this.client.Do(this.getContext(), "INCR", key).Err()
err = this.client.Do(this.client.Context(), "INCR", key).Err()
return
}
@ -76,7 +76,7 @@ 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()
err = this.client.Do(this.client.Context(), "INCRBY", key, value).Err()
return
}
@ -85,7 +85,7 @@ 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()
err = this.client.Do(this.client.Context(), "INCRBYFLOAT", key, value).Err()
return
}
@ -96,7 +96,7 @@ 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()
err = this.client.Do(this.client.Context(), "DECR", key, value).Err()
return
}
@ -107,7 +107,7 @@ 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()
err = this.client.Do(this.client.Context(), "DECRBY", key, value).Err()
return
}
@ -121,7 +121,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
if result, err = this.codec.Marshal(value); err != nil {
return
}
err = this.client.Do(this.getContext(), "APPEND", key, result).Err()
err = this.client.Do(this.client.Context(), "APPEND", key, result).Err()
return
}
@ -130,7 +130,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
*/
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 {
if result, err = this.client.Get(this.client.Context(), key).Bytes(); err == nil {
err = this.codec.Unmarshal(result, value)
}
return
@ -144,8 +144,8 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
_value []byte
)
if _value, err = this.codec.Marshal(value); err == nil {
cmd := redis.NewStringCmd(this.getContext(), "GETSET", key, _value)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringCmd(this.client.Context(), "GETSET", key, _value)
this.client.Process(this.client.Context(), cmd)
var _result []byte
if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, result)
@ -163,8 +163,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
for _, v := range keys {
agrs = append(agrs, v)
}
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
this.client.Process(this.getContext(), cmd)
cmd := redis.NewStringSliceCmd(this.client.Context(), agrs...)
this.client.Process(this.client.Context(), cmd)
var result []string
if result, err = cmd.Result(); err != nil {
return
@ -175,8 +175,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
///判断是否存在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)
cmd := redis.NewIntCmd(this.client.Context(), "INCRBY", key, amount)
this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result()
return
}

View File

@ -8,7 +8,7 @@ import (
Redis ZAdd 向有序集合添加一个或多个成员或者更新已存在成员的分数
*/
func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) {
this.client.ZAdd(this.getContext(), key, members...)
this.client.ZAdd(this.client.Context(), key, members...)
return
}
@ -16,7 +16,7 @@ func (this *Redis) ZAdd(key string, members ...*redis.Z) (err error) {
Redis Zcard 用于计算集合中元素的数量
*/
func (this *Redis) ZCard(key string) (result int64, err error) {
result, err = this.client.ZCard(this.getContext(), key).Result()
result, err = this.client.ZCard(this.client.Context(), key).Result()
return
}
@ -24,7 +24,7 @@ func (this *Redis) ZCard(key string) (result int64, err error) {
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()
result, err = this.client.ZCount(this.client.Context(), key, min, max).Result()
return
}
@ -32,7 +32,7 @@ func (this *Redis) ZCount(key string, min string, max string) (result int64, err
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()
result, err = this.client.ZIncrBy(this.client.Context(), key, increment, member).Result()
return
}
@ -40,7 +40,7 @@ func (this *Redis) ZIncrBy(key string, increment float64, member string) (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()
result, err = this.client.ZInterStore(this.client.Context(), destination, store).Result()
return
}
@ -48,7 +48,7 @@ func (this *Redis) ZInterStore(destination string, store *redis.ZStore) (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()
result, err = this.client.ZLexCount(this.client.Context(), key, min, max).Result()
return
}
@ -57,7 +57,7 @@ Redis ZRange 通过索引区间返回有序集合指定区间内的成员
*/
func (this *Redis) ZRange(key string, start int64, stop int64, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRange(this.getContext(), key, start, stop)
cmd := this.client.ZRange(this.client.Context(), key, start, stop)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -69,7 +69,7 @@ Redis ZRangeByLex 通过字典区间返回有序集合的成员
*/
func (this *Redis) ZRangeByLex(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRangeByLex(this.getContext(), key, opt)
cmd := this.client.ZRangeByLex(this.client.Context(), key, opt)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -81,7 +81,7 @@ Redis ZRangeByScore 通过分数返回有序集合指定区间内的成员
*/
func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRangeByScore(this.getContext(), key, opt)
cmd := this.client.ZRangeByScore(this.client.Context(), key, opt)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -92,7 +92,7 @@ func (this *Redis) ZRangeByScore(key string, opt *redis.ZRangeBy, v interface{})
Redis ZRank 返回有序集合中指定成员的索引
*/
func (this *Redis) ZRank(key string, member string) (result int64, err error) {
result, err = this.client.ZRank(this.getContext(), key, member).Result()
result, err = this.client.ZRank(this.client.Context(), key, member).Result()
return
}
@ -100,7 +100,7 @@ func (this *Redis) ZRank(key string, member string) (result int64, err error) {
Redis ZRem 移除有序集合中的一个或多个成员
*/
func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err error) {
result, err = this.client.ZRem(this.getContext(), key, members...).Result()
result, err = this.client.ZRem(this.client.Context(), key, members...).Result()
return
}
@ -108,7 +108,7 @@ func (this *Redis) ZRem(key string, members ...interface{}) (result int64, err e
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()
result, err = this.client.ZRemRangeByLex(this.client.Context(), key, min, max).Result()
return
}
@ -116,7 +116,7 @@ func (this *Redis) ZRemRangeByLex(key string, min string, max string) (result in
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()
result, err = this.client.ZRemRangeByRank(this.client.Context(), key, start, stop).Result()
return
}
@ -124,7 +124,7 @@ func (this *Redis) ZRemRangeByRank(key string, start int64, stop int64) (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()
result, err = this.client.ZRemRangeByScore(this.client.Context(), key, min, max).Result()
return
}
@ -133,7 +133,7 @@ Redis ZRevRange 返回有序集中指定区间内的成员,通过索引,分
*/
func (this *Redis) ZRevRange(key string, start int64, stop int64, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRevRange(this.getContext(), key, start, stop)
cmd := this.client.ZRevRange(this.client.Context(), key, start, stop)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -145,7 +145,7 @@ Redis ZRevRangeByScore 返回有序集中指定分数区间内的成员,分数
*/
func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy, v interface{}) (err error) {
var _result []string
cmd := this.client.ZRevRangeByScore(this.getContext(), key, opt)
cmd := this.client.ZRevRangeByScore(this.client.Context(), key, opt)
if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v)
}
@ -156,7 +156,7 @@ func (this *Redis) ZRevRangeByScore(key string, opt *redis.ZRangeBy, v interface
Redis ZRevRank 返回有序集中指定分数区间内的成员分数从高到低排序
*/
func (this *Redis) ZRevRank(key string, member string) (result int64, err error) {
result, err = this.client.ZRevRank(this.getContext(), key, member).Result()
result, err = this.client.ZRevRank(this.client.Context(), key, member).Result()
return
}
@ -164,7 +164,7 @@ func (this *Redis) ZRevRank(key string, member string) (result int64, err error)
Redis ZScore 返回有序集中指定分数区间内的成员分数从高到低排序
*/
func (this *Redis) ZScore(key string, member string) (result float64, err error) {
result, err = this.client.ZScore(this.getContext(), key, member).Result()
result, err = this.client.ZScore(this.client.Context(), key, member).Result()
return
}
@ -172,7 +172,7 @@ func (this *Redis) ZScore(key string, member string) (result float64, err error)
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()
result, err = this.client.ZUnionStore(this.client.Context(), dest, store).Result()
return
}
@ -180,6 +180,6 @@ func (this *Redis) ZUnionStore(dest string, store *redis.ZStore) (result int64,
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()
keys, cursor, err = this.client.ZScan(this.client.Context(), key, _cursor, match, count).Result()
return
}

View File

@ -3,7 +3,6 @@ package redis_test
import (
"encoding/json"
"fmt"
"log"
"os"
"sync"
"testing"
@ -14,47 +13,19 @@ import (
func TestMain(m *testing.M) {
if err := redis.OnInit(nil,
redis.SetRedisType(redis.Redis_Cluster),
redis.SetRedis_Cluster_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}),
redis.SetRedis_Cluster_Password(""),
redis.SetRedisType(redis.Redis_Single),
redis.SetRedis_Single_Addr("10.0.0.9:6986"),
redis.SetRedis_Single_Password("li13451234"),
redis.SetRedis_Single_DB(6),
); err != nil {
fmt.Println("err:", err)
return
}
defer os.Exit(m.Run())
// if err := cache.OnInit(nil, cache.Set_Redis_Addr([]string{"10.0.0.9:9001", "10.0.0.9:9002", "10.0.0.9:9003", "10.0.1.45:9004", "10.0.1.45:9005", "10.0.1.45:9006"}), cache.Set_Redis_Password("")); err != nil {
// fmt.Printf("err:%v\n", err)
// return
// }
}
func Test_SysIPV6(t *testing.T) {
err := redis.OnInit(map[string]interface{}{
"Redis_Single_Addr": "172.27.100.143:6382",
"Redis_Single_DB": 0,
"Redis_Single_Password": "idss@sjzt",
})
if err != nil {
fmt.Printf("Redis:err:%v \n", err)
return
}
fmt.Printf("Redis:succ \n")
if err = redis.Set("liwei1dao", 123, -1); err != nil {
fmt.Printf("Redis:err:%v \n", err)
}
}
func Test_Redis_ExpireatKey(t *testing.T) {
err := redis.OnInit(map[string]interface{}{
"Redis_Single_Addr": "172.20.27.145:10001",
"Redis_Single_DB": 0,
"Redis_Single_Password": "li13451234",
})
if err != nil {
fmt.Printf("Redis:err:%v \n", err)
return
}
fmt.Printf("Redis:succ \n")
var err error
if err = redis.Set("liwei1dao", 123, -1); err != nil {
fmt.Printf("Redis:err:%v \n", err)
}
@ -70,16 +41,6 @@ func Test_JsonMarshal(t *testing.T) {
}
func Test_Redis_SetNX(t *testing.T) {
err := redis.OnInit(map[string]interface{}{
"Redis_Single_Addr": "172.20.27.145:10001",
"RedisDB": 0,
"Redis_Single_Password": "li13451234",
})
if err != nil {
fmt.Printf("Redis:err:%v \n", err)
return
}
wg := new(sync.WaitGroup)
wg.Add(20)
for i := 0; i < 20; i++ {
@ -93,30 +54,11 @@ func Test_Redis_SetNX(t *testing.T) {
fmt.Printf("Redis:end \n")
}
func Test_Redis_Lock(t *testing.T) {
err := redis.OnInit(map[string]interface{}{
"Redis_Single_Addr": "172.20.27.145:10001",
"Redis_Single_DB": 0,
"Redis_Single_Password": "li13451234",
})
if err != nil {
fmt.Printf("Redis:err:%v \n", err)
return
}
result, err := redis.Lock("liwei2dao", 100000)
fmt.Printf("Redis result:%v err:%v \n", result, err)
}
func Test_Redis_Mutex(t *testing.T) {
err := redis.OnInit(map[string]interface{}{
"Redis_Single_Addr": "172.20.27.145:10001",
"Redis_Single_DB": 0,
"Redis_Single_Password": "li13451234",
})
if err != nil {
fmt.Printf("Redis:err:%v \n", err)
return
}
wg := new(sync.WaitGroup)
wg.Add(20)
for i := 0; i < 20; i++ {
@ -141,17 +83,6 @@ func Test_Redis_Mutex(t *testing.T) {
}
func Test_Redis_Type(t *testing.T) {
err := redis.OnInit(map[string]interface{}{
"Redis_Single_Addr": "172.20.27.145:10001",
"Redis_Single_DB": 1,
"Redis_Single_Password": "li13451234",
})
if err != nil {
fmt.Printf("Redis:err:%v \n", err)
return
}
fmt.Printf("Redis:succ \n")
if ty, err := redis.Type("test_set"); err != nil {
fmt.Printf("Test_Redis_Type:err:%v \n", err)
} else {
@ -193,45 +124,26 @@ func Test_Redis_Encoder_Hash(t *testing.T) {
//测试redis lua 脚本
func Test_Redis_Lua(t *testing.T) {
script := redis.NewScript(`
local goodsSurplus
local flag
local existUserIds = tostring(KEYS[1])
local memberUid = tonumber(ARGV[1])
local goodsSurplusKey = tostring(KEYS[2])
local hasBuy = redis.call("sIsMember", existUserIds, memberUid)
if hasBuy ~= 0 then
return 0
local key = tostring(KEYS[1])
local keys = redis.call("HGETALL", key)
local data = {}
local n = 1
for i, v in ipairs(keys) do
if i%2 == 0 then
data[n] = redis.call("HGETALL", v)
n = n+1
end
goodsSurplus = redis.call("GET", goodsSurplusKey)
if goodsSurplus == false then
return 0
end
-- 没有剩余可抢购物品
goodsSurplus = tonumber(goodsSurplus)
if goodsSurplus <= 0 then
return 0
end
flag = redis.call("SADD", existUserIds, memberUid)
flag = redis.call("DECR", goodsSurplusKey)
return 1
return data
`)
sha, err := script.Result()
if err != nil {
log.Fatalln(err)
fmt.Println(err)
}
ret := redis.EvalSha(sha, []string{
"hadBuyUids",
"goodsSurplus",
}, "userId")
ret := redis.EvalSha(redis.Context(), sha, []string{"items:0_62c259916d8cf3e4e06311a8"})
if result, err := ret.Result(); err != nil {
log.Fatalf("Execute Redis fail: %v", err.Error())
fmt.Printf("Execute Redis err: %v", err.Error())
} else {
fmt.Println("")
fmt.Printf("userid: %s, result: %d", "userId", result)
fmt.Printf("userid: %v", result)
}
}