From 7b64db726880e50bbf41b011bfed4d4cb44bbcb6 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 15 Oct 2021 17:52:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=97=E8=A1=A8=E5=85=AC?= =?UTF-8?q?=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash.go | 37 ++++++++++++++++- hash_test.go | 2 +- list.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ list_test.go | 18 +++++++++ 4 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 list.go create mode 100644 list_test.go diff --git a/hash.go b/hash.go index 04126cb..461b314 100644 --- a/hash.go +++ b/hash.go @@ -114,10 +114,45 @@ func HGetAll(key string, url ...string) ([]map[string]string, error) { return list, nil } +/** + * 全部 + */ +func HGetList(key string, url ...string) ([]string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return []string{}, err + } + defer conn.Close() + + req := &GetRequest{proto.String(key), nil} + + res := &HGetListResponse{} + + err = conn.HGetList(req, res) + + if err != nil { + return []string{}, err + } + + value := res.GetList() + + var list []string + + err = json.Unmarshal(value, &list) + + if err != nil { + log.Println("json unmarshal error:", err) + return []string{}, err + } + return list, nil +} + /** * hash键是否存在 */ -func HExists(key string, field string, url ...string)(int64,error){ +func HExists(key string, field string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { diff --git a/hash_test.go b/hash_test.go index b95d5da..1dbae09 100644 --- a/hash_test.go +++ b/hash_test.go @@ -50,6 +50,6 @@ func Test_HSet(t *testing.T) { // } // t.Log(err) - ret, err := HGetAll("testing2") + ret, err := HGetList("test2") t.Log(ret, err) } diff --git a/list.go b/list.go new file mode 100644 index 0000000..f49e6b6 --- /dev/null +++ b/list.go @@ -0,0 +1,112 @@ +package redisrpc + +import ( + "encoding/json" + "log" + + "github.com/golang/protobuf/proto" +) + +//头部增加 +func LLpush(key, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &LSetRequest{proto.String(key), proto.String(field), nil} + + res := &LSetResponse{} + + err = conn.LLpush(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +//尾部增加 +func LRpush(key, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &LSetRequest{proto.String(key), proto.String(field), nil} + + res := &LSetResponse{} + + err = conn.LRpush(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +/** + * 全部 + */ +func LRange(key string, start, stop int64, url ...string) ([]string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return []string{}, err + } + defer conn.Close() + + req := &LRangeRequest{proto.String(key), proto.Int64(start), proto.Int64(stop), nil} + + res := &HGetListResponse{} + + err = conn.LRange(req, res) + + if err != nil { + return []string{}, err + } + + value := res.GetList() + + var list []string + + err = json.Unmarshal(value, &list) + + if err != nil { + log.Println("json unmarshal error:", err) + return []string{}, err + } + return list, nil +} + +//长度 +func LLen(key string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &LLenRequest{proto.String(key), nil} + + res := &LSetResponse{} + + err = conn.LLen(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} diff --git a/list_test.go b/list_test.go new file mode 100644 index 0000000..df54e89 --- /dev/null +++ b/list_test.go @@ -0,0 +1,18 @@ +package redisrpc + +import ( + // "strconv" + "testing" + // "tgo/helper" +) + +func Test_LLpush(t *testing.T) { + + // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} + + reply, err := LLen("testing") + + t.Log(reply) + t.Log(err) + +}