Browse Source

增加hash查询多个键值

master v1.6.0
guzeng 2 years ago
parent
commit
b768284e21
2 changed files with 77 additions and 0 deletions
  1. +62
    -0
      hash.go
  2. +15
    -0
      hash_test.go

+ 62
- 0
hash.go View File

@ -126,6 +126,35 @@ func HGetList(key string) ([]string, error) {
} }
//hash取所有值
func HGetAllItem(key string) (map[string]string, error) {
c := GetConn()
ret, err := c.Do("HGETAll", key)
reply := make([][]byte, 0)
if err == nil {
reply, err = redisdb.ByteSlices(ret, err)
}
var info map[string]string = make(map[string]string)
if len(reply) > 0 {
for key, item := range reply {
if key%2 == 0 { //只处理奇数位
info[string(item)] = string(reply[key+1])
}
}
}
CloseConn(c)
return info, err
}
/* /*
* hash存值, * hash存值,
* key * key
@ -198,6 +227,39 @@ func HMSet(args ...interface{}) (interface{}, error) {
return reply, err return reply, err
} }
/*
* hash批量查询,
* args (key,field,field,field...) , ...
* 2020/06/06
*/
func HMGet(args ...interface{}) (interface{}, error) {
c := GetConn()
reply, err := c.Do("HMGET", args...)
CloseConn(c)
return reply, err
}
/*
* hash批量查询,
* args (key,field,field,field...) , ...
* 2020/06/06
*/
func HMGetString(args ...interface{}) ([]string, error) {
c := GetConn()
reply, err := c.Do("HMGET", args...)
CloseConn(c)
list := make([]string, 0)
if err == nil {
list, err = redisdb.Strings(reply, err)
}
return list, err
}
/* /*
* hash存值, * hash存值,
* key * key


+ 15
- 0
hash_test.go View File

@ -17,3 +17,18 @@ func Test_HVals(t *testing.T) {
t.Log(err) t.Log(err)
} }
func Test_HGetAllItem(t *testing.T) {
RedisPassword = "123456"
ret3, err := HGetAllItem("60007_config")
t.Log(ret3)
t.Log(err)
}
func Test_HMGet(t *testing.T) {
RedisPassword = "123456"
ret3, err := HMGetString("60007_config", "wx_mp_appsecret", "wx_mp_appid", "withdrawal_rate")
t.Log(ret3)
t.Log(err)
}

Loading…
Cancel
Save