From 06d399b82714f3a7a3a360d3e0810e1d8b858d68 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 21 May 2021 11:43:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AEhset=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash.go | 54 ++++++++++++++++++++++++++++++++++++++++++++------ hash_test.go | 8 ++++---- redis.pb.go | 18 +++++++++++++++++ redis.proto | 6 ++++++ string.go | 2 +- string_test.go | 10 +++++----- 6 files changed, 82 insertions(+), 16 deletions(-) diff --git a/hash.go b/hash.go index 1572553..5b169ce 100644 --- a/hash.go +++ b/hash.go @@ -1,7 +1,6 @@ package redisrpc import ( - "git.tetele.net/tgo/conf" "github.com/golang/protobuf/proto" ) @@ -10,15 +9,12 @@ import ( */ func HGetString(key, field string, url ...string) (string, error) { - var rpc_url string = "127.0.0.1:" + conf.REDIS_RPC_PORT - if len(url) > 0 && url[0] != "" { - rpc_url = url[0] - } + conn, _, err := Conn(url...) - conn, _, err := DialRedisService("tcp", rpc_url) if err != nil { return "", err } + defer conn.Close() req := &HGetRequest{proto.String(key), proto.String(field), nil} @@ -33,3 +29,49 @@ func HGetString(key, field string, url ...string) (string, error) { return res.GetValue(), nil } + +//设置 +func HSet(key, field, value string, url ...string) (uint64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return "", err + } + defer conn.Close() + + req := &HSetRequest{proto.String(key), proto.String(field), proto.String(value), nil} + + res := &HSetResponse{} + + err = conn.HSet(req, res) + + if err != nil { + return "", err + } + + return res.GetRet(), nil +} + +//删除 +func HDel(key string, field string, url ...string) (uint64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &HDelRequest{proto.String(key), proto.String(field), nil} + + res := &DelResponse{} + + err = conn.HDel(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} diff --git a/hash_test.go b/hash_test.go index 75f19fc..f537215 100644 --- a/hash_test.go +++ b/hash_test.go @@ -10,12 +10,12 @@ func Test_HSet(t *testing.T) { // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} - // reply, err := HSet("testing2", "test1", val) + reply, err := HSet("testing2", "test1", "val") - // t.Log(reply) - // t.Log(err) + t.Log(reply) + t.Log(err) - ret, err := GetByHashKey("testing", "test") + ret, err := HGetString("testing", "test") t.Log(ret) if err == nil { diff --git a/redis.pb.go b/redis.pb.go index 0134285..5b83bda 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -18,6 +18,7 @@ It has these top-level messages: GetStringResponse SetResponse DelResponse + HSetResponse */ package redisrpc @@ -235,6 +236,23 @@ func (m *DelResponse) GetRet() uint64 { return 0 } +// 设置key响应结构 +type HSetResponse struct { + Ret *uint64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *HSetResponse) Reset() { *m = HSetResponse{} } +func (m *HSetResponse) String() string { return proto.CompactTextString(m) } +func (*HSetResponse) ProtoMessage() {} + +func (m *HSetResponse) GetRet() uint64 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + func init() { } diff --git a/redis.proto b/redis.proto index ccb12d8..68a9065 100644 --- a/redis.proto +++ b/redis.proto @@ -47,6 +47,12 @@ message SetResponse { message DelResponse { uint64 ret = 1; } + +// 设置key响应结构 +message HSetResponse { + uint64 ret = 1; +} + // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 diff --git a/string.go b/string.go index 8234888..1cec089 100644 --- a/string.go +++ b/string.go @@ -52,7 +52,7 @@ func Set(key, value string, ttl string, url ...string) (string, error) { return res.GetRet(), nil } -//设置 +//删除 func Del(key string, url ...string) (uint64, error) { conn, _, err := Conn(url...) diff --git a/string_test.go b/string_test.go index 4a8b19c..647e93a 100644 --- a/string_test.go +++ b/string_test.go @@ -5,13 +5,13 @@ import ( // "time" ) -// func Test_Get(t *testing.T) { +func Test_Get(t *testing.T) { -// reply, err := GetString("siteListCache") + reply, err := GetString("siteListCache") -// t.Log(reply) -// t.Log(err) -// } + t.Log(reply) + t.Log(err) +} func Test_Set(t *testing.T) {