listen 2 years ago
parent
commit
ab07817052
7 changed files with 190 additions and 1 deletions
  1. +51
    -0
      num.go
  2. +22
    -0
      num_test.go
  3. +51
    -0
      redis.pb.go
  4. +11
    -0
      redis.proto
  5. +1
    -1
      string_test.go
  6. +24
    -0
      watch_test.go
  7. +30
    -0
      wath.go

+ 51
- 0
num.go View File

@ -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
}

+ 22
- 0
num_test.go View File

@ -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)
}

+ 51
- 0
redis.pb.go View File

@ -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) {


+ 11
- 0
redis.proto View File

@ -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);//
}

+ 1
- 1
string_test.go View File

@ -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)


+ 24
- 0
watch_test.go View File

@ -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
}

+ 30
- 0
wath.go View File

@ -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
}

Loading…
Cancel
Save