Browse Source

增加hgetall接口

master v0.3.1
guzeng 3 years ago
parent
commit
7dcf6bfc8c
2 changed files with 58 additions and 17 deletions
  1. +38
    -0
      hash.go
  2. +20
    -17
      hash_test.go

+ 38
- 0
hash.go View File

@ -1,6 +1,9 @@
package redisrpc
import (
"encoding/json"
"log"
"github.com/golang/protobuf/proto"
)
@ -75,3 +78,38 @@ func HDel(key string, field string, url ...string) (int64, error) {
return res.GetRet(), nil
}
/**
* 全部
*/
func HGetAll(key string, url ...string) ([]map[string]string, error) {
conn, _, err := Conn(url...)
if err != nil {
return []map[string]string{}, err
}
defer conn.Close()
req := &GetRequest{proto.String(key), nil}
res := &HGetListResponse{}
err = conn.HGetAll(req, res)
if err != nil {
return []map[string]string{}, err
}
value := res.GetList()
var list []map[string]string
err = json.Unmarshal(value, &list)
if err != nil {
log.Println("json unmarshal error:", err)
return []map[string]string{}, err
}
return list, nil
}

+ 20
- 17
hash_test.go View File

@ -10,28 +10,28 @@ func Test_HSet(t *testing.T) {
// val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"}
reply, err := HSet("testing", "test", "test1")
// reply, err := HSet("testing", "test", "test1")
t.Log(reply)
t.Log(err)
// t.Log(reply)
// t.Log(err)
ret, err := HGetString("testing", "test")
// ret, err := HGetString("testing", "test")
t.Log(ret)
if err == nil {
t.Log("nil", err)
} else {
t.Log("error:", err)
}
// t.Log(ret)
// if err == nil {
// t.Log("nil", err)
// } else {
// t.Log("error:", err)
// }
del_ret, err := HDel("testing", "test")
// del_ret, err := HDel("testing", "test")
t.Log(del_ret)
if err == nil {
t.Log("nil", err)
} else {
t.Log("error:", err)
}
// t.Log(del_ret)
// if err == nil {
// t.Log("nil", err)
// } else {
// t.Log("error:", err)
// }
// ret2, err := HSetExpire("testing2", 200)
// t.Log(ret2)
// t.Log(err)
@ -49,4 +49,7 @@ func Test_HSet(t *testing.T) {
// t.Log(string(val))
// }
// t.Log(err)
ret, err := HGetAll("testing2")
t.Log(ret, err)
}

Loading…
Cancel
Save