优化redis 哈希接口

This commit is contained in:
liwei1dao 2022-06-13 13:58:17 +08:00
parent 1d53007bf7
commit 34351ec69b
5 changed files with 68 additions and 153 deletions

View File

@ -1,8 +1,6 @@
package cluster package cluster
import ( import (
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
@ -10,13 +8,7 @@ import (
Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略 Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略
*/ */
func (this *Redis) HDel(key string, fields ...string) (err error) { func (this *Redis) HDel(key string, fields ...string) (err error) {
agrs := make([]interface{}, 0) err = this.client.HDel(this.getContext(), key, fields...).Err()
agrs = append(agrs, "HDEL")
agrs = append(agrs, key)
for _, v := range fields {
agrs = append(agrs, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
return return
} }
@ -24,18 +16,15 @@ 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() this.client.HExists(this.getContext(), key, field)
return return
} }
/* /*
Redis Hget 命令用于返回哈希表中指定字段的值 Redis Hget 命令用于返回哈希表中指定字段的值
*/ */
func (this *Redis) HGet(key string, field string, value interface{}) (err error) { func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
var resultvalue string err = this.client.HGet(this.getContext(), key, field).Scan(v)
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
err = this.Decode([]byte(resultvalue), value)
}
return return
} }
@ -43,19 +32,8 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error)
Redis Hgetall 命令用于返回哈希表中所有的字段和值 Redis Hgetall 命令用于返回哈希表中所有的字段和值
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍 在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/ */
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) err = this.client.HGetAll(this.getContext(), key).Scan(v)
this.client.Process(this.getContext(), cmd)
var _result []string
if _result, err = cmd.Result(); err == nil {
result = make([]interface{}, len(_result))
for i, v := range _result {
temp := reflect.New(valuetype.Elem()).Interface()
if err = this.Decode([]byte(v), &temp); err == nil {
result[i] = temp
}
}
}
return return
} }
@ -67,8 +45,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误 对一个储存字符串值的字段执行 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 int64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() err = this.client.HIncrBy(this.getContext(), key, field, value).Err()
return return
} }
@ -76,8 +54,8 @@ func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值 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 float64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err()
return return
} }
@ -85,17 +63,21 @@ 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) var cmd *redis.StringSliceCmd
this.client.Process(this.getContext(), cmd) cmd = this.client.HKeys(this.getContext(), key)
result, err = cmd.Result() result = cmd.Val()
err = cmd.Err()
return return
} }
/* /*
Redis Hlen 命令用于获取哈希表中字段的数量 Redis Hlen 命令用于获取哈希表中字段的数量
*/ */
func (this *Redis) Hlen(key string) (result int, err error) { func (this *Redis) Hlen(key string) (result int64, err error) {
result, err = this.client.Do(this.getContext(), "HLEN", key).Int() var cmd *redis.IntCmd
cmd = this.client.HLen(this.getContext(), key)
result = cmd.Val()
err = cmd.Err()
return return
} }
@ -103,26 +85,8 @@ func (this *Redis) Hlen(key string) (result int, err error) {
Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值 Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值
如果指定的字段不存在于哈希表那么返回一个 nil 如果指定的字段不存在于哈希表那么返回一个 nil
*/ */
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd {
agrs := make([]interface{}, 0) return this.client.HMGet(this.getContext(), key, fields...)
agrs = append(agrs, "HMGET")
agrs = append(agrs, key)
for _, v := range fields {
agrs = append(agrs, v)
}
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
this.client.Process(this.getContext(), cmd)
var _result []string
if _result, err = cmd.Result(); err == nil {
result = make([]interface{}, len(_result))
for i, v := range _result {
temp := reflect.New(valuetype.Elem()).Interface()
if err = this.Decode([]byte(v), &temp); err == nil {
result[i] = temp
}
}
}
return
} }
/* /*
@ -148,10 +112,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 err = this.client.HSet(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
}
return return
} }
@ -162,9 +123,6 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令 如果 key 不存在一个新哈希表被创建并执行 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 err = this.client.HSetNX(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
}
return return
} }

View File

@ -64,13 +64,13 @@ type (
/*Hash*/ /*Hash*/
HDel(key string, fields ...string) (err error) HDel(key string, fields ...string) (err error)
HExists(key string, field string) (result bool, err error) HExists(key string, field string) (result bool, err error)
HGet(key string, field string, value interface{}) (err error) HGet(key string, field string, v interface{}) (err error)
HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) HGetAll(key string, v interface{}) (err error)
HIncrBy(key string, field string, value int) (err error) HIncrBy(key string, field string, value int64) (err error)
HIncrByFloat(key string, field string, value float32) (err error) HIncrByFloat(key string, field string, value float64) (err error)
Hkeys(key string) (result []string, err error) Hkeys(key string) (result []string, err error)
Hlen(key string) (result int, err error) Hlen(key string) (result int64, err error)
HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) HMGet(key string, fields ...string) *redis.SliceCmd
HMSet(key string, value map[string]interface{}) (err error) HMSet(key string, value map[string]interface{}) (err error)
HSet(key string, field string, value interface{}) (err error) HSet(key string, field string, value interface{}) (err error)
HSetNX(key string, field string, value interface{}) (err error) HSetNX(key string, field string, value interface{}) (err error)
@ -315,23 +315,23 @@ func HExists(key string, field string) (result bool, err error) {
func HGet(key string, field string, value interface{}) (err error) { func HGet(key string, field string, value interface{}) (err error) {
return defsys.HGet(key, field, value) return defsys.HGet(key, field, value)
} }
func HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func HGetAll(key string, v interface{}) (err error) {
return defsys.HGetAll(key, valuetype) return defsys.HGetAll(key, v)
} }
func HIncrBy(key string, field string, value int) (err error) { func HIncrBy(key string, field string, value int64) (err error) {
return defsys.HIncrBy(key, field, value) return defsys.HIncrBy(key, field, value)
} }
func HIncrByFloat(key string, field string, value float32) (err error) { func HIncrByFloat(key string, field string, value float64) (err error) {
return defsys.HIncrByFloat(key, field, value) return defsys.HIncrByFloat(key, field, value)
} }
func Hkeys(key string) (result []string, err error) { func Hkeys(key string) (result []string, err error) {
return defsys.Hkeys(key) return defsys.Hkeys(key)
} }
func Hlen(key string) (result int, err error) { func Hlen(key string) (result int64, err error) {
return defsys.Hlen(key) return defsys.Hlen(key)
} }
func HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func HMGet(key string, fields ...string) *redis.SliceCmd {
return defsys.HMGet(key, valuetype, fields...) return defsys.HMGet(key, fields...)
} }
func HMSet(key string, value map[string]interface{}) (err error) { func HMSet(key string, value map[string]interface{}) (err error) {
return defsys.HMSet(key, value) return defsys.HMSet(key, value)

View File

@ -241,23 +241,23 @@ func (this *Redis) HExists(key string, field string) (result bool, err error) {
func (this *Redis) HGet(key string, field string, value interface{}) (err error) { func (this *Redis) HGet(key string, field string, value interface{}) (err error) {
return this.client.HGet(key, field, value) return this.client.HGet(key, field, value)
} }
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
return this.client.HGetAll(key, valuetype) return this.client.HGetAll(key, v)
} }
func (this *Redis) HIncrBy(key string, field string, value int) (err error) { func (this *Redis) HIncrBy(key string, field string, value int64) (err error) {
return this.client.HIncrBy(key, field, value) return this.client.HIncrBy(key, field, value)
} }
func (this *Redis) HIncrByFloat(key string, field string, value float32) (err error) { func (this *Redis) HIncrByFloat(key string, field string, value float64) (err error) {
return this.client.HIncrByFloat(key, field, value) return this.client.HIncrByFloat(key, field, value)
} }
func (this *Redis) Hkeys(key string) (result []string, err error) { func (this *Redis) Hkeys(key string) (result []string, err error) {
return this.client.Hkeys(key) return this.client.Hkeys(key)
} }
func (this *Redis) Hlen(key string) (result int, err error) { func (this *Redis) Hlen(key string) (result int64, err error) {
return this.client.Hlen(key) return this.client.Hlen(key)
} }
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd {
return this.client.HMGet(key, valuetype, fields...) return this.client.HMGet(key, fields...)
} }
func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) { func (this *Redis) HMSet(key string, value map[string]interface{}) (err error) {
return this.client.HMSet(key, value) return this.client.HMSet(key, value)

View File

@ -1,8 +1,6 @@
package single package single
import ( import (
"reflect"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
) )
@ -10,13 +8,7 @@ import (
Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略 Redis Hdel 命令用于删除哈希表 key 中的一个或多个指定字段不存在的字段将被忽略
*/ */
func (this *Redis) HDel(key string, fields ...string) (err error) { func (this *Redis) HDel(key string, fields ...string) (err error) {
agrs := make([]interface{}, 0) err = this.client.HDel(this.getContext(), key, fields...).Err()
agrs = append(agrs, "HDEL")
agrs = append(agrs, key)
for _, v := range fields {
agrs = append(agrs, v)
}
err = this.client.Do(this.getContext(), agrs...).Err()
return return
} }
@ -24,18 +16,15 @@ 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() this.client.HExists(this.getContext(), key, field)
return return
} }
/* /*
Redis Hget 命令用于返回哈希表中指定字段的值 Redis Hget 命令用于返回哈希表中指定字段的值
*/ */
func (this *Redis) HGet(key string, field string, value interface{}) (err error) { func (this *Redis) HGet(key string, field string, v interface{}) (err error) {
var resultvalue string err = this.client.HGet(this.getContext(), key, field).Scan(v)
if resultvalue = this.client.Do(this.getContext(), "HSET", key, field).String(); resultvalue != string(redis.Nil) {
err = this.Decode([]byte(resultvalue), value)
}
return return
} }
@ -43,19 +32,8 @@ func (this *Redis) HGet(key string, field string, value interface{}) (err error)
Redis Hgetall 命令用于返回哈希表中所有的字段和值 Redis Hgetall 命令用于返回哈希表中所有的字段和值
在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍 在返回值里紧跟每个字段名(field name)之后是字段的值(value)所以返回值的长度是哈希表大小的两倍
*/ */
func (this *Redis) HGetAll(key string, valuetype reflect.Type) (result []interface{}, err error) { func (this *Redis) HGetAll(key string, v interface{}) (err error) {
cmd := redis.NewStringSliceCmd(this.getContext(), "HGETALL", key) err = this.client.HGetAll(this.getContext(), key).Scan(v)
this.client.Process(this.getContext(), cmd)
var _result []string
if _result, err = cmd.Result(); err == nil {
result = make([]interface{}, len(_result))
for i, v := range _result {
temp := reflect.New(valuetype.Elem()).Interface()
if err = this.Decode([]byte(v), &temp); err == nil {
result[i] = temp
}
}
}
return return
} }
@ -67,8 +45,8 @@ Redis Hincrby 命令用于为哈希表中的字段值加上指定增量值。
对一个储存字符串值的字段执行 HINCRBY 命令将造成一个错误 对一个储存字符串值的字段执行 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 int64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBY", key, field, value).Err() err = this.client.HIncrBy(this.getContext(), key, field, value).Err()
return return
} }
@ -76,8 +54,8 @@ func (this *Redis) HIncrBy(key string, field string, value int) (err error) {
Redis Hincrbyfloat 命令用于为哈希表中的字段值加上指定浮点数增量值 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 float64) (err error) {
err = this.client.Do(this.getContext(), "HINCRBYFLOAT", key, field, value).Err() err = this.client.HIncrByFloat(this.getContext(), key, field, value).Err()
return return
} }
@ -85,17 +63,21 @@ 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) var cmd *redis.StringSliceCmd
this.client.Process(this.getContext(), cmd) cmd = this.client.HKeys(this.getContext(), key)
result, err = cmd.Result() result = cmd.Val()
err = cmd.Err()
return return
} }
/* /*
Redis Hlen 命令用于获取哈希表中字段的数量 Redis Hlen 命令用于获取哈希表中字段的数量
*/ */
func (this *Redis) Hlen(key string) (result int, err error) { func (this *Redis) Hlen(key string) (result int64, err error) {
result, err = this.client.Do(this.getContext(), "HLEN", key).Int() var cmd *redis.IntCmd
cmd = this.client.HLen(this.getContext(), key)
result = cmd.Val()
err = cmd.Err()
return return
} }
@ -103,26 +85,8 @@ func (this *Redis) Hlen(key string) (result int, err error) {
Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值 Redis Hmget 命令用于返回哈希表中一个或多个给定字段的值
如果指定的字段不存在于哈希表那么返回一个 nil 如果指定的字段不存在于哈希表那么返回一个 nil
*/ */
func (this *Redis) HMGet(key string, valuetype reflect.Type, fields ...string) (result []interface{}, err error) { func (this *Redis) HMGet(key string, fields ...string) *redis.SliceCmd {
agrs := make([]interface{}, 0) return this.client.HMGet(this.getContext(), key, fields...)
agrs = append(agrs, "HMGET")
agrs = append(agrs, key)
for _, v := range fields {
agrs = append(agrs, v)
}
cmd := redis.NewStringSliceCmd(this.getContext(), agrs...)
this.client.Process(this.getContext(), cmd)
var _result []string
if _result, err = cmd.Result(); err == nil {
result = make([]interface{}, len(_result))
for i, v := range _result {
temp := reflect.New(valuetype.Elem()).Interface()
if err = this.Decode([]byte(v), &temp); err == nil {
result[i] = temp
}
}
}
return
} }
/* /*
@ -148,10 +112,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 err = this.client.HSet(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSET", key, field, resultvalue).Err()
}
return return
} }
@ -162,9 +123,6 @@ Redis Hsetnx 命令用于为哈希表中不存在的的字段赋值
如果 key 不存在一个新哈希表被创建并执行 HSETNX 命令 如果 key 不存在一个新哈希表被创建并执行 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 err = this.client.HSetNX(this.getContext(), key, field, value).Err()
if resultvalue, err = this.Encode(value); err == nil {
err = this.client.Do(this.getContext(), "HSETNX", key, field, resultvalue).Err()
}
return return
} }

View File

@ -7,7 +7,6 @@ import (
"go_dreamfactory/lego/sys/redis" "go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules" "go_dreamfactory/modules"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"reflect"
"go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/bson/primitive"
) )
@ -31,8 +30,8 @@ func (this *Cache_Comp) Pack_QueryUserPack(uId string) (itmes []*pb.DB_UserItemD
lists []interface{} lists []interface{}
temp map[string]interface{} temp map[string]interface{}
) )
itmes = make([]*pb.DB_UserItemData, len(lists)) itmes = make([]*pb.DB_UserItemData, 0)
if lists, err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), reflect.TypeOf(&pb.DB_UserItemData{})); err == nil { if err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), &itmes); err == nil {
for i, v := range lists { for i, v := range lists {
itmes[i] = v.(*pb.DB_UserItemData) itmes[i] = v.(*pb.DB_UserItemData)
} }