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

View File

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

View File

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

View File

@ -7,7 +7,6 @@ import (
"go_dreamfactory/lego/sys/redis"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"reflect"
"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{}
temp map[string]interface{}
)
itmes = make([]*pb.DB_UserItemData, len(lists))
if lists, err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), reflect.TypeOf(&pb.DB_UserItemData{})); err == nil {
itmes = make([]*pb.DB_UserItemData, 0)
if err = this.Redis.HGetAll(fmt.Sprintf(Redis_PackCache, uId), &itmes); err == nil {
for i, v := range lists {
itmes[i] = v.(*pb.DB_UserItemData)
}