From 4daaf096875575ebff8df5e801b342eae684f6ab Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 30 Nov 2021 11:23:45 +0800 Subject: [PATCH 1/4] client for watch --- watch_test.go | 24 ++++++++++++++++++++++++ wath.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 watch_test.go create mode 100644 wath.go diff --git a/watch_test.go b/watch_test.go new file mode 100644 index 0000000..7ecc527 --- /dev/null +++ b/watch_test.go @@ -0,0 +1,24 @@ +package redisrpc + +import ( + "log" + "testing" + // "time" +) + +func Test_ReduceStock(t *testing.T) { + + ch := make(chan int) + for i := 0; i < 100; i++ { + go func() { + log.Println("start") + + reply, err := ReduceStock("test_watch", "2") + + log.Println(reply) + log.Println(err) + log.Println("end") + }() + } + <-ch +} diff --git a/wath.go b/wath.go new file mode 100644 index 0000000..5468ac2 --- /dev/null +++ b/wath.go @@ -0,0 +1,30 @@ +package redisrpc + +import ( + "github.com/golang/protobuf/proto" +) + +/** + * 使用用户名查询 + */ +func ReduceStock(key string, value string, url ...string) (string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return "", err + } + defer conn.Close() + + req := &SetRequest{proto.String(key), proto.String(value), proto.Int64(0), nil} + + res := &SetResponse{} + + err = conn.ReduceStock(req, res) + + if err != nil { + return "", err + } + + return res.GetRet(), nil +} From baeafb732c9c99807e4d8e24c79eda32204d7de8 Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 30 Nov 2021 17:29:52 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=8A=A0=E5=87=8F?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 34 ++++++++++++++++++++++++++++++++++ redis.proto | 7 +++++++ 2 files changed, 41 insertions(+) diff --git a/redis.pb.go b/redis.pb.go index d929dd2..815d6bb 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -29,6 +29,7 @@ It has these top-level messages: LSetResponse LRangeRequest LLenRequest + AddRequest */ package redisrpc @@ -473,6 +474,31 @@ func (m *LLenRequest) GetKey() string { return "" } +// 数值增加 +type AddRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Value *int64 `protobuf:"varint,2,opt,name=value" json:"value,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *AddRequest) Reset() { *m = AddRequest{} } +func (m *AddRequest) String() string { return proto.CompactTextString(m) } +func (*AddRequest) ProtoMessage() {} + +func (m *AddRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *AddRequest) GetValue() int64 { + if m != nil && m.Value != nil { + return *m.Value + } + return 0 +} + func init() { } @@ -498,6 +524,8 @@ type RedisService interface { LRange(in *LRangeRequest, out *HGetListResponse) error LLen(in *LLenRequest, out *LSetResponse) error ReduceStock(in *SetRequest, out *SetResponse) error + Incrby(in *AddRequest, out *SetResponse) error + Decrby(in *AddRequest, out *SetResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -632,6 +660,12 @@ func (c *RedisServiceClient) LLen(in *LLenRequest, out *LSetResponse) error { func (c *RedisServiceClient) ReduceStock(in *SetRequest, out *SetResponse) error { return c.Call("RedisService.ReduceStock", in, out) } +func (c *RedisServiceClient) Incrby(in *AddRequest, out *SetResponse) error { + return c.Call("RedisService.Incrby", in, out) +} +func (c *RedisServiceClient) Decrby(in *AddRequest, out *SetResponse) error { + return c.Call("RedisService.Decrby", in, out) +} // DialRedisService connects to an RedisService at the specified network address. func DialRedisService(network, addr string) (*RedisServiceClient, *rpc.Client, error) { diff --git a/redis.proto b/redis.proto index 2c02d1c..cdc19e1 100644 --- a/redis.proto +++ b/redis.proto @@ -103,6 +103,11 @@ message LLenRequest{ string key = 1; } +//数值增加 +message AddRequest { + string key = 1; + int64 value = 2; +} // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 @@ -126,4 +131,6 @@ service RedisService { rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 rpc LLen(LLenRequest) returns (LSetResponse); //列表长度 rpc ReduceStock(SetRequest) returns(SetResponse);//扣减库存 + rpc Incrby(AddRequest) returns(SetResponse);//增加 + rpc Decrby(AddRequest) returns(SetResponse);//减 } \ No newline at end of file From 74208f60e129441095f9e1b96bf24b9ccc90adff Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 30 Nov 2021 17:33:19 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=8A=A0=E5=87=8F?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 25 +++++++++++++++++++++---- redis.proto | 8 ++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/redis.pb.go b/redis.pb.go index 815d6bb..e323079 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -30,6 +30,7 @@ It has these top-level messages: LRangeRequest LLenRequest AddRequest + AddResponse */ package redisrpc @@ -499,6 +500,22 @@ func (m *AddRequest) GetValue() int64 { return 0 } +type AddResponse struct { + Ret *int64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *AddResponse) Reset() { *m = AddResponse{} } +func (m *AddResponse) String() string { return proto.CompactTextString(m) } +func (*AddResponse) ProtoMessage() {} + +func (m *AddResponse) GetRet() int64 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + func init() { } @@ -524,8 +541,8 @@ type RedisService interface { LRange(in *LRangeRequest, out *HGetListResponse) error LLen(in *LLenRequest, out *LSetResponse) error ReduceStock(in *SetRequest, out *SetResponse) error - Incrby(in *AddRequest, out *SetResponse) error - Decrby(in *AddRequest, out *SetResponse) error + Incrby(in *AddRequest, out *AddResponse) error + Decrby(in *AddRequest, out *AddResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -660,10 +677,10 @@ func (c *RedisServiceClient) LLen(in *LLenRequest, out *LSetResponse) error { func (c *RedisServiceClient) ReduceStock(in *SetRequest, out *SetResponse) error { return c.Call("RedisService.ReduceStock", in, out) } -func (c *RedisServiceClient) Incrby(in *AddRequest, out *SetResponse) error { +func (c *RedisServiceClient) Incrby(in *AddRequest, out *AddResponse) error { return c.Call("RedisService.Incrby", in, out) } -func (c *RedisServiceClient) Decrby(in *AddRequest, out *SetResponse) error { +func (c *RedisServiceClient) Decrby(in *AddRequest, out *AddResponse) error { return c.Call("RedisService.Decrby", in, out) } diff --git a/redis.proto b/redis.proto index cdc19e1..dec7254 100644 --- a/redis.proto +++ b/redis.proto @@ -108,6 +108,10 @@ message AddRequest { string key = 1; int64 value = 2; } +message AddResponse { + int64 ret = 1; +} + // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 @@ -131,6 +135,6 @@ service RedisService { rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 rpc LLen(LLenRequest) returns (LSetResponse); //列表长度 rpc ReduceStock(SetRequest) returns(SetResponse);//扣减库存 - rpc Incrby(AddRequest) returns(SetResponse);//增加 - rpc Decrby(AddRequest) returns(SetResponse);//减 + rpc Incrby(AddRequest) returns(AddResponse);//增加 + rpc Decrby(AddRequest) returns(AddResponse);//减 } \ No newline at end of file From 1fe8afd6bb3741e8e9ce5459b44a4999326a0bee Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 30 Nov 2021 18:00:22 +0800 Subject: [PATCH 4/4] =?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)