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

@ -15,8 +15,11 @@ func NewSys(RedisUrl []string, RedisPassword string, timeOut time.Duration,
client *redis.ClusterClient client *redis.ClusterClient
) )
client = redis.NewClusterClient(&redis.ClusterOptions{ client = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: RedisUrl, Addrs: RedisUrl,
Password: RedisPassword, Password: RedisPassword,
DialTimeout: timeOut,
ReadTimeout: timeOut,
WriteTimeout: timeOut,
}) })
sys = &Redis{ sys = &Redis{
client: client, client: client,
@ -33,20 +36,20 @@ type Redis struct {
codec core.ICodec codec core.ICodec
} }
func (this *Redis) getContext() (ctx context.Context) {
ctx, _ = context.WithTimeout(context.Background(), this.timeOut)
return
}
///事务 ///事务
func (this *Redis) Close() (err error) { func (this *Redis) Close() (err error) {
err = this.client.Close() err = this.client.Close()
return return
} }
/// Context
func (this *Redis) Context() context.Context {
return this.client.Context()
}
/// Ping /// Ping
func (this *Redis) Ping() (string, error) { 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) { func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
cmd := redis.NewBoolCmd(this.getContext(), "set", key, 1, "ex", outTime, "nx") cmd := redis.NewBoolCmd(this.client.Context(), "set", key, 1, "ex", outTime, "nx")
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
return return
} }
@ -93,17 +96,18 @@ func (this *Redis) UnLock(key string) (err error) {
//lua Script //lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd { func (this *Redis) NewScript(src string) *redis.StringCmd {
script := redis.NewScript(src) 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 { func (this *Redis) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(this.getContext(), script, keys, args...) return this.client.Eval(ctx, script, keys, args...)
} }
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd { func (this *Redis) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(this.getContext(), sha1, keys, args...) return this.client.EvalSha(ctx, sha1, keys, args...)
} }
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd { func (this *Redis) ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(this.getContext(), hashes...) return this.client.ScriptExists(ctx, hashes...)
}
func (this *Redis) ScriptLoad(script string) *redis.StringCmd {
return this.client.ScriptLoad(this.getContext(), script)
} }
// 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 { for _, v := range fields {
agrs = append(agrs, v) agrs = append(agrs, v)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -22,7 +22,7 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
Redis Hexists 命令用于查看哈希表的指定字段是否存在 Redis Hexists 命令用于查看哈希表的指定字段是否存在
*/ */
func (this *Redis) HExists(key string, field string) (result bool, err error) { 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 return
} }
@ -42,7 +42,7 @@ func (this *Redis) HMSet(key string, v interface{}) (err error) {
for k, v := range data { for k, v := range data {
agrs = append(agrs, k, v) agrs = append(agrs, k, v)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -53,7 +53,7 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
for k, v := range v { for k, v := range v {
agrs = append(agrs, k, v) agrs = append(agrs, k, v)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -61,8 +61,8 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
Redis Hget 命令用于返回哈希表中指定字段的值 Redis Hget 命令用于返回哈希表中指定字段的值
*/ */
func (this *Redis) HGet(key string, field string, v interface{}) (err error) { func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "HGET", key, field) cmd := redis.NewStringCmd(this.client.Context(), "HGET", key, field)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result []byte var _result []byte
if _result, err = cmd.Bytes(); err == nil { if _result, err = cmd.Bytes(); err == nil {
if len(_result) == 0 { if len(_result) == 0 {
@ -79,8 +79,8 @@ Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍 在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/ */
func (this *Redis) HGetAll(key string, v interface{}) (err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key) cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result map[string]string var _result map[string]string
if _result, err = cmd.Result(); err == nil { if _result, err = cmd.Result(); err == nil {
if len(_result) == 0 { if len(_result) == 0 {
@ -96,8 +96,8 @@ func (this *Redis) HGetAll(key string, v interface{}) (err error) {
读取全部hash集合数据到map中 读取全部hash集合数据到map中
*/ */
func (this *Redis) HGetAllToMapString(key string) (result map[string]string, err error) { func (this *Redis) HGetAllToMapString(key string) (result map[string]string, err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key) cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
if result, err = cmd.Result(); err == nil { if result, err = cmd.Result(); err == nil {
if len(result) == 0 { if len(result) == 0 {
err = redis.Nil err = redis.Nil
@ -116,7 +116,7 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
本操作的值被限制在 64 (bit)有符号数字表示之内 本操作的值被限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) HIncrBy(key string, field string, value int) (err error) { 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 return
} }
@ -125,7 +125,7 @@ Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数
如果指定的字段不存在那么在执行命令前字段的值被初始化为 0 如果指定的字段不存在那么在执行命令前字段的值被初始化为 0
*/ */
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { 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 return
} }
@ -133,8 +133,8 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er
Redis Hkeys 命令用于获取哈希表中的所有域(field) Redis Hkeys 命令用于获取哈希表中的所有域(field)
*/ */
func (this *Redis) Hkeys(key string) (result []string, err error) { func (this *Redis) Hkeys(key string) (result []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) cmd := redis.NewStringSliceCmd(this.client.Context(), "HKEYS", key)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
return return
} }
@ -143,7 +143,7 @@ func (this *Redis) Hkeys(key string) (result []string, err error) {
Redis Hlen 命令用于获取哈希表中字段的数量 Redis Hlen 命令用于获取哈希表中字段的数量
*/ */
func (this *Redis) Hlen(key string) (result int, err error) { 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 return
} }
@ -158,8 +158,8 @@ func (this *Redis) HMGet(key string, v interface{}, fields ...string) (err error
for _, v := range fields { for _, v := range fields {
agrs = append(agrs, v) agrs = append(agrs, v)
} }
cmd := redis.NewStringStringMapCmd(this.getContext(), agrs...) cmd := redis.NewStringStringMapCmd(this.client.Context(), agrs...)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result map[string]string var _result map[string]string
if _result, err = cmd.Result(); err == nil { if _result, err = cmd.Result(); err == nil {
if len(_result) == 0 { if len(_result) == 0 {
@ -179,7 +179,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
func (this *Redis) HSet(key string, field string, value interface{}) (err error) { func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
var resultvalue []byte var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil { 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 return
} }
@ -193,7 +193,7 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
var resultvalue []byte var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil { 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 return
} }

View File

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

View File

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

View File

@ -13,7 +13,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v) result, _ := this.codec.Marshal(v)
agrs = append(agrs, result) agrs = append(agrs, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -21,7 +21,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
Redis Scard 命令返回集合中元素的数量 Redis Scard 命令返回集合中元素的数量
*/ */
func (this *Redis) SCard(key string) (result int64, err error) { 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 return
} }
@ -32,7 +32,7 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可
*/ */
func (this *Redis) SDiff(v interface{}, keys ...string) (err error) { func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -43,7 +43,7 @@ func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中 Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中
*/ */
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { 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 return
} }
@ -52,7 +52,7 @@ Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集
*/ */
func (this *Redis) SInter(v interface{}, keys ...string) (err error) { func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -63,7 +63,7 @@ func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖 Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖
*/ */
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { 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 return
} }
@ -71,7 +71,7 @@ func (this *Redis) SInterStore(destination string, keys ...string) (result int64
Redis Sismember 命令判断成员元素是否是集合的成员 Redis Sismember 命令判断成员元素是否是集合的成员
*/ */
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { 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 return
} }
@ -80,7 +80,7 @@ Redis Smembers 号召返回集合中的所有成员。
*/ */
func (this *Redis) SMembers(v interface{}, key string) (err error) { func (this *Redis) SMembers(v interface{}, key string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -95,7 +95,7 @@ SMOVE 是原子性操作。
source destination 不是集合类型时返回一个错误 source destination 不是集合类型时返回一个错误
*/ */
func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { 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 return
} }
@ -104,7 +104,7 @@ Redis Spop命令用于移除集合中的指定键的一个或多个随机元素
该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动 该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动
*/ */
func (this *Redis) Spop(key string) (result string, err error) { 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 return
} }
@ -116,7 +116,7 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。
该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动 该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动
*/ */
func (this *Redis) Srandmember(key string) (result string, err error) { 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 return
} }
@ -126,7 +126,7 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在
Redis 2.4 版本以前SREM 只接受个别成员值 Redis 2.4 版本以前SREM 只接受个别成员值
*/ */
func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { 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 return
} }
@ -135,7 +135,7 @@ Redis Sunion 命令返回给定集合的并集。
*/ */
func (this *Redis) SUnion(v interface{}, keys ...string) (err error) { func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -146,7 +146,7 @@ func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖 Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖
*/ */
func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { 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 return
} }
@ -154,6 +154,6 @@ func (this *Redis) Sunionstore(destination string, keys ...string) (result int64
Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan
*/ */
func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { 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 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 { if result, err = this.codec.Marshal(value); err != nil {
return return
} }
err = this.client.Set(this.getContext(), key, result, expiration).Err() err = this.client.Set(this.client.Context(), key, result, expiration).Err()
return return
} }
@ -23,8 +23,8 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
指定的 key 不存在时 key 设置指定的值 指定的 key 不存在时 key 设置指定的值
*/ */
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value) cmd := redis.NewIntCmd(this.client.Context(), "SETNX", key, value)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
// } // }
return return
@ -40,7 +40,7 @@ func (this *Redis) MSet(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v) result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result) agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -54,7 +54,7 @@ func (this *Redis) MSetNX(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v) result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result) agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -65,7 +65,7 @@ Redis Incr 命令将 key 中储存的数字值增一。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Incr(key string) (err error) { 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 return
} }
@ -76,7 +76,7 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) IncrBY(key string, value int) (err error) { 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 return
} }
@ -85,7 +85,7 @@ Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量
如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作 如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作
*/ */
func (this *Redis) Incrbyfloat(key string, value float32) (err error) { 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 return
} }
@ -96,7 +96,7 @@ Redis Decr 命令将 key 中储存的数字值减一。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Decr(key string, value int) (err error) { 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 return
} }
@ -107,7 +107,7 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) DecrBy(key string, value int) (err error) { 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 return
} }
@ -121,7 +121,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
if result, err = this.codec.Marshal(value); err != nil { if result, err = this.codec.Marshal(value); err != nil {
return return
} }
err = this.client.Do(this.getContext(), "APPEND", key, result).Err() err = this.client.Do(this.client.Context(), "APPEND", key, result).Err()
return 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) { func (this *Redis) Get(key string, value interface{}) (err error) {
var result []byte 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) err = this.codec.Unmarshal(result, value)
} }
return return
@ -144,8 +144,8 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
_value []byte _value []byte
) )
if _value, err = this.codec.Marshal(value); err == nil { if _value, err = this.codec.Marshal(value); err == nil {
cmd := redis.NewStringCmd(this.getContext(), "GETSET", key, _value) cmd := redis.NewStringCmd(this.client.Context(), "GETSET", key, _value)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result []byte var _result []byte
if _result, err = cmd.Bytes(); err == nil { if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, result) err = this.codec.Unmarshal(_result, result)
@ -163,8 +163,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
for _, v := range keys { for _, v := range keys {
agrs = append(agrs, v) agrs = append(agrs, v)
} }
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) cmd := redis.NewStringSliceCmd(this.client.Context(), agrs...)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var result []string var result []string
if result, err = cmd.Result(); err != nil { if result, err = cmd.Result(); err != nil {
return return
@ -175,8 +175,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
///判断是否存在key pattern:key* ///判断是否存在key pattern:key*
func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) {
cmd := redis.NewIntCmd(this.getContext(), "INCRBY", key, amount) cmd := redis.NewIntCmd(this.client.Context(), "INCRBY", key, amount)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
return return
} }

View File

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

View File

@ -10,6 +10,7 @@ import (
type ( type (
IRedis interface { IRedis interface {
Close() (err error) Close() (err error)
Context() context.Context
Do(ctx context.Context, args ...interface{}) *redis.Cmd Do(ctx context.Context, args ...interface{}) *redis.Cmd
Lock(key string, outTime int) (result bool, err error) Lock(key string, outTime int) (result bool, err error)
UnLock(key string) (err error) UnLock(key string) (err error)
@ -116,13 +117,15 @@ type (
/*Lua Script*/ /*Lua Script*/
NewScript(src string) *redis.StringCmd NewScript(src string) *redis.StringCmd
Eval(script string, keys []string, args ...interface{}) *redis.Cmd Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(hashes ...string) *redis.BoolSliceCmd ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd
// ScriptLoad(ctx context.Context, script string) *redis.StringCmd
} }
ISys interface { ISys interface {
IRedis IRedis
GetClient() IRedis
/*Lock*/ /*Lock*/
NewRedisMutex(key string, opt ...RMutexOption) (result *RedisMutex, err error) 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) { func Close() (err error) {
return defsys.Close() 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 { func Do(ctx context.Context, args ...interface{}) *redis.Cmd {
return defsys.Do(ctx, args...) 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 { func NewScript(src string) *redis.StringCmd {
return defsys.NewScript(src) return defsys.NewScript(src)
} }
func Eval(script string, keys []string, args ...interface{}) *redis.Cmd { func Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(script, keys, args...) return defsys.Eval(ctx, script, keys, args...)
} }
func EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd { func EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(sha1, keys, args...) return defsys.EvalSha(ctx, sha1, keys, args...)
} }
func ScriptExists(hashes ...string) *redis.BoolSliceCmd { func ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return defsys.ScriptExists(hashes...) 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_Addr string
Redis_Single_Password string Redis_Single_Password string
Redis_Single_DB int Redis_Single_DB int
Redis_Single_PoolSize int
Redis_Cluster_Addr []string Redis_Cluster_Addr []string
Redis_Cluster_Password string Redis_Cluster_Password string
TimeOut time.Duration 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 { func SetRedis_Cluster_Addr(v []string) Option {
return func(o *Options) { return func(o *Options) {
o.Redis_Cluster_Addr = v 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_Addr: []string{"127.0.0.1:6379"},
Redis_Cluster_Password: "", Redis_Cluster_Password: "",
TimeOut: time.Second * 3, TimeOut: time.Second * 3,
Redis_Single_PoolSize: 100,
} }
if config != nil { if config != nil {
mapstructure.Decode(config, &options) mapstructure.Decode(config, &options)
@ -107,7 +100,6 @@ func newOptionsByOption(opts ...Option) Options {
Redis_Cluster_Addr: []string{"127.0.0.1:6379"}, Redis_Cluster_Addr: []string{"127.0.0.1:6379"},
Redis_Cluster_Password: "", Redis_Cluster_Password: "",
TimeOut: time.Second * 3, TimeOut: time.Second * 3,
Redis_Single_PoolSize: 100,
} }
for _, o := range opts { for _, o := range opts {
o(&options) o(&options)

View File

@ -29,7 +29,6 @@ func (this *Redis) init() (err error) {
this.options.Redis_Single_Addr, this.options.Redis_Single_Addr,
this.options.Redis_Single_Password, this.options.Redis_Single_Password,
this.options.Redis_Single_DB, this.options.Redis_Single_DB,
this.options.Redis_Single_PoolSize,
this.options.TimeOut, this.options.TimeOut,
this, this,
) )
@ -48,6 +47,14 @@ func (this *Redis) init() (err error) {
func (this *Redis) Close() (err error) { func (this *Redis) Close() (err error) {
return this.client.Close() 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 { func (this *Redis) Do(ctx context.Context, args ...interface{}) *redis.Cmd {
return this.client.Do(ctx, args...) 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 { func (this *Redis) NewScript(src string) *redis.StringCmd {
return this.client.NewScript(src) return this.client.NewScript(src)
} }
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd { func (this *Redis) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(script, keys, args...) return this.client.Eval(ctx, script, keys, args...)
} }
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd { func (this *Redis) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(sha1, keys, args...) return this.client.EvalSha(ctx, sha1, keys, args...)
} }
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd { func (this *Redis) ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(hashes...) return this.client.ScriptExists(ctx, hashes...)
} }
// func (this *Redis) ScriptLoad(ctx context.Context, script string) *redis.StringCmd {
// return this.client.ScriptLoad(ctx, script)
// }
//Codec--------------------------------------------------------------------------------------------------------------------------------------- //Codec---------------------------------------------------------------------------------------------------------------------------------------
func (this *Redis) Marshal(v interface{}) ([]byte, error) { func (this *Redis) Marshal(v interface{}) ([]byte, error) {
if this.options.Codec != nil { if this.options.Codec != nil {

View File

@ -8,44 +8,45 @@ import (
"github.com/go-redis/redis/v8" "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, codec core.ICodec,
) (sys *Redis, err error) { ) (sys *Redis, err error) {
var ( var (
client *redis.Client client *redis.Client
) )
client = redis.NewClient(&redis.Options{ client = redis.NewClient(&redis.Options{
Addr: RedisUrl, Addr: RedisUrl,
Password: RedisPassword, Password: RedisPassword,
DB: RedisDB, DB: RedisDB,
PoolSize: PoolSize, // 连接池大小 DialTimeout: timeOut,
WriteTimeout: timeOut,
ReadTimeout: timeOut,
}) })
sys = &Redis{ sys = &Redis{
client: client, client: client,
timeOut: timeOut, codec: codec,
codec: codec,
} }
_, err = sys.Ping() _, err = sys.Ping()
return return
} }
type Redis struct { type Redis struct {
client *redis.Client client *redis.Client
timeOut time.Duration codec core.ICodec
codec core.ICodec
} }
func (this *Redis) getContext() (ctx context.Context) {
ctx, _ = context.WithTimeout(context.Background(), this.timeOut)
return
}
func (this *Redis) Close() (err error) { func (this *Redis) Close() (err error) {
return this.client.Close() return this.client.Close()
} }
/// Context
func (this *Redis) Context() context.Context {
return this.client.Context()
}
/// Ping /// Ping
func (this *Redis) Ping() (string, error) { 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) { func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
cmd := redis.NewBoolCmd(this.getContext(), "set", key, 1, "ex", outTime, "nx") cmd := redis.NewBoolCmd(this.client.Context(), "set", key, 1, "ex", outTime, "nx")
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
return return
} }
@ -92,17 +93,18 @@ func (this *Redis) UnLock(key string) (err error) {
//lua Script //lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd { func (this *Redis) NewScript(src string) *redis.StringCmd {
script := redis.NewScript(src) 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 { func (this *Redis) Eval(ctx context.Context, script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(this.getContext(), script, keys, args...) return this.client.Eval(ctx, script, keys, args...)
} }
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd { func (this *Redis) EvalSha(ctx context.Context, sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(this.getContext(), sha1, keys, args...) return this.client.EvalSha(ctx, sha1, keys, args...)
} }
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd { func (this *Redis) ScriptExists(ctx context.Context, hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(this.getContext(), hashes...) return this.client.ScriptExists(ctx, hashes...)
}
func (this *Redis) ScriptLoad(script string) *redis.StringCmd {
return this.client.ScriptLoad(this.getContext(), script)
} }
// 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 { for _, v := range fields {
agrs = append(agrs, v) agrs = append(agrs, v)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -22,7 +22,7 @@ func (this *Redis) HDel(key string, fields ...string) (err error) {
Redis Hexists 命令用于查看哈希表的指定字段是否存在 Redis Hexists 命令用于查看哈希表的指定字段是否存在
*/ */
func (this *Redis) HExists(key string, field string) (result bool, err error) { 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 return
} }
@ -42,7 +42,7 @@ func (this *Redis) HMSet(key string, v interface{}) (err error) {
for k, v := range data { for k, v := range data {
agrs = append(agrs, k, v) agrs = append(agrs, k, v)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) { 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 { for k, v := range v {
agrs = append(agrs, k, v) agrs = append(agrs, k, v)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -60,8 +60,8 @@ func (this *Redis) HMSetForMap(key string, v map[string]string) (err error) {
Redis Hget 命令用于返回哈希表中指定字段的值 Redis Hget 命令用于返回哈希表中指定字段的值
*/ */
func (this *Redis) HGet(key string, field string, v interface{}) (err error) { func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
cmd := redis.NewStringCmd(this.getContext(), "HGET", key, field) cmd := redis.NewStringCmd(this.client.Context(), "HGET", key, field)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result []byte var _result []byte
if _result, err = cmd.Bytes(); err == nil { if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, v) err = this.codec.Unmarshal(_result, v)
@ -74,8 +74,8 @@ Redis Hgetall 命令用于返回哈希表中,所有的字段和值。
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍 在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/ */
func (this *Redis) HGetAll(key string, v interface{}) (err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key) cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result map[string]string var _result map[string]string
if _result, err = cmd.Result(); err == nil { if _result, err = cmd.Result(); err == nil {
if len(_result) == 0 { if len(_result) == 0 {
@ -90,8 +90,8 @@ func (this *Redis) HGetAll(key string, v interface{}) (err error) {
读取全部hash集合数据到map中 读取全部hash集合数据到map中
*/ */
func (this *Redis) HGetAllToMapString(key string) (result map[string]string, err error) { func (this *Redis) HGetAllToMapString(key string) (result map[string]string, err error) {
cmd := redis.NewStringStringMapCmd(this.getContext(), "HGETALL", key) cmd := redis.NewStringStringMapCmd(this.client.Context(), "HGETALL", key)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
if result, err = cmd.Result(); err == nil { if result, err = cmd.Result(); err == nil {
if len(result) == 0 { if len(result) == 0 {
err = redis.Nil err = redis.Nil
@ -110,7 +110,7 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
本操作的值被限制在 64 (bit)有符号数字表示之内 本操作的值被限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) HIncrBy(key string, field string, value int) (err error) { 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 return
} }
@ -119,7 +119,7 @@ Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数
如果指定的字段不存在那么在执行命令前字段的值被初始化为 0 如果指定的字段不存在那么在执行命令前字段的值被初始化为 0
*/ */
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { 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 return
} }
@ -127,8 +127,8 @@ func (this *Redis) HIncrByFloat(key string, field string, value float32) (err er
Redis Hkeys 命令用于获取哈希表中的所有域(field) Redis Hkeys 命令用于获取哈希表中的所有域(field)
*/ */
func (this *Redis) Hkeys(key string) (result []string, err error) { func (this *Redis) Hkeys(key string) (result []string, err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HKEYS", key) cmd := redis.NewStringSliceCmd(this.client.Context(), "HKEYS", key)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
return return
} }
@ -137,7 +137,7 @@ func (this *Redis) Hkeys(key string) (result []string, err error) {
Redis Hlen 命令用于获取哈希表中字段的数量 Redis Hlen 命令用于获取哈希表中字段的数量
*/ */
func (this *Redis) Hlen(key string) (result int, err error) { 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 return
} }
@ -152,8 +152,8 @@ func (this *Redis) HMGet(key string, v interface{}, fields ...string) (err error
for _, v := range fields { for _, v := range fields {
agrs = append(agrs, v) agrs = append(agrs, v)
} }
cmd := redis.NewStringStringMapCmd(this.getContext(), agrs...) cmd := redis.NewStringStringMapCmd(this.client.Context(), agrs...)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result map[string]string var _result map[string]string
if _result, err = cmd.Result(); err == nil { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalMap(_result, v) err = this.codec.UnmarshalMap(_result, v)
@ -169,7 +169,7 @@ Redis Hset 命令用于为哈希表中的字段赋值
func (this *Redis) HSet(key string, field string, value interface{}) (err error) { func (this *Redis) HSet(key string, field string, value interface{}) (err error) {
var resultvalue []byte var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil { 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 return
} }
@ -183,7 +183,7 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) { func (this *Redis) HSetNX(key string, field string, value interface{}) (err error) {
var resultvalue []byte var resultvalue []byte
if resultvalue, err = this.codec.Marshal(value); err == nil { 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 return
} }

View File

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

View File

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

View File

@ -13,7 +13,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
result, _ := this.codec.Marshal(v) result, _ := this.codec.Marshal(v)
agrs = append(agrs, result) agrs = append(agrs, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -21,7 +21,7 @@ func (this *Redis) SAdd(key string, values ...interface{}) (err error) {
Redis Scard 命令返回集合中元素的数量 Redis Scard 命令返回集合中元素的数量
*/ */
func (this *Redis) SCard(key string) (result int64, err error) { 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 return
} }
@ -32,7 +32,7 @@ Redis Sdiff 命令返回第一个集合与其他集合之间的差异,也可
*/ */
func (this *Redis) SDiff(v interface{}, keys ...string) (err error) { func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -43,7 +43,7 @@ func (this *Redis) SDiff(v interface{}, keys ...string) (err error) {
Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中 Redis Sdiffstore 命令将给定集合之间的差集合存储在指定的集合中
*/ */
func (this *Redis) SDiffStore(destination string, keys ...string) (result int64, err error) { 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 return
} }
@ -52,7 +52,7 @@ Redis Sismember 命令返回给定所有给定集合的交集。 不存在的集
*/ */
func (this *Redis) SInter(v interface{}, keys ...string) (err error) { func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -63,7 +63,7 @@ func (this *Redis) SInter(v interface{}, keys ...string) (err error) {
Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖 Redis Sinterstore 决定将给定集合之间的交集在指定的集合中如果指定的集合已经存在则将其覆盖
*/ */
func (this *Redis) SInterStore(destination string, keys ...string) (result int64, err error) { 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 return
} }
@ -71,7 +71,7 @@ func (this *Redis) SInterStore(destination string, keys ...string) (result int64
Redis Sismember 命令判断成员元素是否是集合的成员 Redis Sismember 命令判断成员元素是否是集合的成员
*/ */
func (this *Redis) Sismember(key string, value interface{}) (iskeep bool, err error) { 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 return
} }
@ -80,7 +80,7 @@ Redis Smembers 号召返回集合中的所有成员。
*/ */
func (this *Redis) SMembers(v interface{}, key string) (err error) { func (this *Redis) SMembers(v interface{}, key string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -95,7 +95,7 @@ SMOVE 是原子性操作。
source destination 不是集合类型时返回一个错误 source destination 不是集合类型时返回一个错误
*/ */
func (this *Redis) SMove(source string, destination string, member interface{}) (result bool, err error) { 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 return
} }
@ -104,7 +104,7 @@ Redis Spop命令用于移除集合中的指定键的一个或多个随机元素
该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动 该命令类似于Srandmember命令但SPOP将随机元素从集合中移除并返回而Srandmember则返回元素而不是对集合进行任何改动
*/ */
func (this *Redis) Spop(key string) (result string, err error) { 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 return
} }
@ -116,7 +116,7 @@ Redis Srandmember 命令用于返回集合中的一个随机元素。
该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动 该操作和 SPOP 相似 SPOP 将随机元素从集合中移除并返回 Srandmember 则仅仅返回随机元素而不对集合进行任何改动
*/ */
func (this *Redis) Srandmember(key string) (result string, err error) { 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 return
} }
@ -126,7 +126,7 @@ Redis Srem 呼吁用于移除集合中的一个或多个元素元素,不存在
Redis 2.4 版本以前SREM 只接受个别成员值 Redis 2.4 版本以前SREM 只接受个别成员值
*/ */
func (this *Redis) SRem(key string, members ...interface{}) (result int64, err error) { 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 return
} }
@ -135,7 +135,7 @@ Redis Sunion 命令返回给定集合的并集。
*/ */
func (this *Redis) SUnion(v interface{}, keys ...string) (err error) { func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
var _result []string 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 { if _result, err = cmd.Result(); err == nil {
err = this.codec.UnmarshalSlice(_result, v) err = this.codec.UnmarshalSlice(_result, v)
} }
@ -146,7 +146,7 @@ func (this *Redis) SUnion(v interface{}, keys ...string) (err error) {
Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖 Redis Sunionstore 命令将给定集合的并集存储在指定的集合 destination 如果 destination 已经存在则将其覆盖
*/ */
func (this *Redis) Sunionstore(destination string, keys ...string) (result int64, err error) { 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 return
} }
@ -154,6 +154,6 @@ func (this *Redis) Sunionstore(destination string, keys ...string) (result int64
Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan Redis Sscan 用于继承集合中键的元素Sscan 继承自Scan
*/ */
func (this *Redis) Sscan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { 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 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 { if result, err = this.codec.Marshal(value); err != nil {
return return
} }
err = this.client.Set(this.getContext(), key, result, expiration).Err() err = this.client.Set(this.client.Context(), key, result, expiration).Err()
return return
} }
@ -23,8 +23,8 @@ func (this *Redis) Set(key string, value interface{}, expiration time.Duration)
指定的 key 不存在时 key 设置指定的值 指定的 key 不存在时 key 设置指定的值
*/ */
func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) { func (this *Redis) SetNX(key string, value interface{}) (result int64, err error) {
cmd := redis.NewIntCmd(this.getContext(), "SETNX", key, value) cmd := redis.NewIntCmd(this.client.Context(), "SETNX", key, value)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
// } // }
return return
@ -40,7 +40,7 @@ func (this *Redis) MSet(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v) result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result) agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -54,7 +54,7 @@ func (this *Redis) MSetNX(v map[string]interface{}) (err error) {
result, _ := this.codec.Marshal(v) result, _ := this.codec.Marshal(v)
agrs = append(agrs, k, result) agrs = append(agrs, k, result)
} }
err = this.client.Do(this.getContext(), agrs...).Err() err = this.client.Do(this.client.Context(), agrs...).Err()
return return
} }
@ -65,7 +65,7 @@ Redis Incr 命令将 key 中储存的数字值增一。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Incr(key string) (err error) { 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 return
} }
@ -76,7 +76,7 @@ Redis Incrby 命令将 key 中储存的数字加上指定的增量值。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) IncrBY(key string, value int) (err error) { 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 return
} }
@ -85,7 +85,7 @@ Redis Incrbyfloat 命令为 key 中所储存的值加上指定的浮点数增量
如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作 如果 key 不存在那么 INCRBYFLOAT 会先将 key 的值设为 0 再执行加法操作
*/ */
func (this *Redis) Incrbyfloat(key string, value float32) (err error) { 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 return
} }
@ -96,7 +96,7 @@ Redis Decr 命令将 key 中储存的数字值减一。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) Decr(key string, value int) (err error) { 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 return
} }
@ -107,7 +107,7 @@ Redis Decrby 命令将 key 所储存的值减去指定的减量值。
本操作的值限制在 64 (bit)有符号数字表示之内 本操作的值限制在 64 (bit)有符号数字表示之内
*/ */
func (this *Redis) DecrBy(key string, value int) (err error) { 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 return
} }
@ -121,7 +121,7 @@ func (this *Redis) Append(key string, value interface{}) (err error) {
if result, err = this.codec.Marshal(value); err != nil { if result, err = this.codec.Marshal(value); err != nil {
return return
} }
err = this.client.Do(this.getContext(), "APPEND", key, result).Err() err = this.client.Do(this.client.Context(), "APPEND", key, result).Err()
return 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) { func (this *Redis) Get(key string, value interface{}) (err error) {
var result []byte 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) err = this.codec.Unmarshal(result, value)
} }
return return
@ -144,8 +144,8 @@ func (this *Redis) GetSet(key string, value interface{}, result interface{}) (er
_value []byte _value []byte
) )
if _value, err = this.codec.Marshal(value); err == nil { if _value, err = this.codec.Marshal(value); err == nil {
cmd := redis.NewStringCmd(this.getContext(), "GETSET", key, _value) cmd := redis.NewStringCmd(this.client.Context(), "GETSET", key, _value)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var _result []byte var _result []byte
if _result, err = cmd.Bytes(); err == nil { if _result, err = cmd.Bytes(); err == nil {
err = this.codec.Unmarshal(_result, result) err = this.codec.Unmarshal(_result, result)
@ -163,8 +163,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
for _, v := range keys { for _, v := range keys {
agrs = append(agrs, v) agrs = append(agrs, v)
} }
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...) cmd := redis.NewStringSliceCmd(this.client.Context(), agrs...)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
var result []string var result []string
if result, err = cmd.Result(); err != nil { if result, err = cmd.Result(); err != nil {
return return
@ -175,8 +175,8 @@ func (this *Redis) MGet(v interface{}, keys ...string) (err error) {
///判断是否存在key pattern:key* ///判断是否存在key pattern:key*
func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) { func (this *Redis) INCRBY(key string, amount int64) (result int64, err error) {
cmd := redis.NewIntCmd(this.getContext(), "INCRBY", key, amount) cmd := redis.NewIntCmd(this.client.Context(), "INCRBY", key, amount)
this.client.Process(this.getContext(), cmd) this.client.Process(this.client.Context(), cmd)
result, err = cmd.Result() result, err = cmd.Result()
return return
} }

View File

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

View File

@ -3,7 +3,6 @@ package redis_test
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -14,47 +13,19 @@ import (
func TestMain(m *testing.M) { func TestMain(m *testing.M) {
if err := redis.OnInit(nil, if err := redis.OnInit(nil,
redis.SetRedisType(redis.Redis_Cluster), redis.SetRedisType(redis.Redis_Single),
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_Single_Addr("10.0.0.9:6986"),
redis.SetRedis_Cluster_Password(""), redis.SetRedis_Single_Password("li13451234"),
redis.SetRedis_Single_DB(6),
); err != nil { ); err != nil {
fmt.Println("err:", err) fmt.Println("err:", err)
return return
} }
defer os.Exit(m.Run()) 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) { func Test_Redis_ExpireatKey(t *testing.T) {
err := redis.OnInit(map[string]interface{}{ var err error
"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")
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) fmt.Printf("Redis:err:%v \n", err)
} }
@ -70,16 +41,6 @@ func Test_JsonMarshal(t *testing.T) {
} }
func Test_Redis_SetNX(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 := new(sync.WaitGroup)
wg.Add(20) wg.Add(20)
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
@ -93,30 +54,11 @@ func Test_Redis_SetNX(t *testing.T) {
fmt.Printf("Redis:end \n") fmt.Printf("Redis:end \n")
} }
func Test_Redis_Lock(t *testing.T) { 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) result, err := redis.Lock("liwei2dao", 100000)
fmt.Printf("Redis result:%v err:%v \n", result, err) fmt.Printf("Redis result:%v err:%v \n", result, err)
} }
func Test_Redis_Mutex(t *testing.T) { 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 := new(sync.WaitGroup)
wg.Add(20) wg.Add(20)
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
@ -141,17 +83,6 @@ func Test_Redis_Mutex(t *testing.T) {
} }
func Test_Redis_Type(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 { if ty, err := redis.Type("test_set"); err != nil {
fmt.Printf("Test_Redis_Type:err:%v \n", err) fmt.Printf("Test_Redis_Type:err:%v \n", err)
} else { } else {
@ -193,45 +124,26 @@ func Test_Redis_Encoder_Hash(t *testing.T) {
//测试redis lua 脚本 //测试redis lua 脚本
func Test_Redis_Lua(t *testing.T) { func Test_Redis_Lua(t *testing.T) {
script := redis.NewScript(` script := redis.NewScript(`
local goodsSurplus local key = tostring(KEYS[1])
local flag local keys = redis.call("HGETALL", key)
local existUserIds = tostring(KEYS[1]) local data = {}
local memberUid = tonumber(ARGV[1]) local n = 1
local goodsSurplusKey = tostring(KEYS[2]) for i, v in ipairs(keys) do
local hasBuy = redis.call("sIsMember", existUserIds, memberUid) if i%2 == 0 then
data[n] = redis.call("HGETALL", v)
if hasBuy ~= 0 then n = n+1
return 0
end end
end
return data
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
`)
sha, err := script.Result() sha, err := script.Result()
if err != nil { if err != nil {
log.Fatalln(err) fmt.Println(err)
} }
ret := redis.EvalSha(sha, []string{ ret := redis.EvalSha(redis.Context(), sha, []string{"items:0_62c259916d8cf3e4e06311a8"})
"hadBuyUids",
"goodsSurplus",
}, "userId")
if result, err := ret.Result(); err != nil { if result, err := ret.Result(); err != nil {
log.Fatalf("Execute Redis fail: %v", err.Error()) fmt.Printf("Execute Redis err: %v", err.Error())
} else { } else {
fmt.Println("") fmt.Printf("userid: %v", result)
fmt.Printf("userid: %s, result: %d", "userId", result)
} }
} }