From 1fe8afd6bb3741e8e9ce5459b44a4999326a0bee Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 30 Nov 2021 18:00:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=87=8Fclient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- num.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ num_test.go | 22 ++++++++++++++++++++++ string_test.go | 2 +- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 num.go create mode 100644 num_test.go diff --git a/num.go b/num.go new file mode 100644 index 0000000..ee6b3b2 --- /dev/null +++ b/num.go @@ -0,0 +1,51 @@ +package redisrpc + +import ( + "github.com/golang/protobuf/proto" +) + +//加 +func Incrby(key string, value int64, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &AddRequest{proto.String(key), proto.Int64(value), nil} + + res := &AddResponse{} + + err = conn.Incrby(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +//减 +func Decrby(key string, value int64, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &AddRequest{proto.String(key), proto.Int64(value), nil} + + res := &AddResponse{} + + err = conn.Decrby(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} diff --git a/num_test.go b/num_test.go new file mode 100644 index 0000000..fe6938f --- /dev/null +++ b/num_test.go @@ -0,0 +1,22 @@ +package redisrpc + +import ( + "testing" + // "time" +) + +func Test_Incrby(t *testing.T) { + + c, err := Incrby("test_incrby", 10) + t.Log(c) + t.Log(err) + +} + +func Test_Decrby(t *testing.T) { + + c, err := Decrby("test_incrby", 2) + t.Log(c) + t.Log(err) + +} diff --git a/string_test.go b/string_test.go index 604f3f4..70af761 100644 --- a/string_test.go +++ b/string_test.go @@ -15,7 +15,7 @@ func Test_Get(t *testing.T) { func Test_Set(t *testing.T) { - c, err := Set("test", "1111", 7200) + c, err := Set("test", "222", 10) t.Log(c) t.Log(err)