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/redis.pb.go b/redis.pb.go index d929dd2..e323079 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -29,6 +29,8 @@ It has these top-level messages: LSetResponse LRangeRequest LLenRequest + AddRequest + AddResponse */ package redisrpc @@ -473,6 +475,47 @@ 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 +} + +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() { } @@ -498,6 +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 *AddResponse) error + Decrby(in *AddRequest, out *AddResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -632,6 +677,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 *AddResponse) error { + return c.Call("RedisService.Incrby", in, out) +} +func (c *RedisServiceClient) Decrby(in *AddRequest, out *AddResponse) 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 eb287c5..21b1e89 100644 --- a/redis.proto +++ b/redis.proto @@ -103,6 +103,15 @@ message LLenRequest{ string key = 1; } +//数值增加 +message AddRequest { + string key = 1; + int64 value = 2; +} +message AddResponse { + int64 ret = 1; +} + // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 @@ -127,4 +136,6 @@ service RedisService { rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 rpc LLen(LLenRequest) returns (LSetResponse); //列表长度 rpc ReduceStock(SetRequest) returns(SetResponse);//扣减库存 + rpc Incrby(AddRequest) returns(AddResponse);//增加 + rpc Decrby(AddRequest) returns(AddResponse);//减 } \ No newline at end of file diff --git a/string_test.go b/string_test.go index a48c7f6..8f26ffa 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) 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 +}