From c5138187e6265b38235a43353c069447e7573eaa Mon Sep 17 00:00:00 2001 From: lijianbin <513837235@.qq.com> Date: Mon, 14 Feb 2022 17:03:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E8=B7=83=E6=81=92redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conn.go | 5 -- go.mod | 12 +++-- go.sum | 17 +++---- hash.go | 5 +- redis.pb.go | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++ redis.proto | 27 +++++++++++ set.go | 119 +++++++++++++++++++++++++++++++++++++++++++-- set_test.go | 25 ++++++++-- 8 files changed, 315 insertions(+), 30 deletions(-) diff --git a/conn.go b/conn.go index 1dc7035..d4e7d2e 100644 --- a/conn.go +++ b/conn.go @@ -1,7 +1,6 @@ package redisrpc import ( - "log" "net/rpc" "git.tetele.net/tgo/conf" @@ -9,8 +8,6 @@ import ( func Conn(url ...string) (*RedisServiceClient, *rpc.Client, error) { - log.Println("conn redis rpc server========", url) - var rpc_url string if len(url) > 0 && url[0] != "" { rpc_url = url[0] @@ -20,8 +17,6 @@ func Conn(url ...string) (*RedisServiceClient, *rpc.Client, error) { rpc_url = "127.0.0.1:" + conf.REDIS_RPC_PORT } - log.Println("rpc_url", rpc_url, "conf.REDIS_RPC_URL", conf.REDIS_RPC_URL) - return DialRedisService("tcp", rpc_url) } diff --git a/go.mod b/go.mod index 84a00d7..ccaf4c3 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,14 @@ module git.tetele.net/tgo/redisrpc -go 1.14 +go 1.17 require ( - git.tetele.net/tgo/conf v0.35.3 - git.tetele.net/tgo/helper v0.2.6 - github.com/chai2010/protorpc v1.0.0 + git.tetele.net/tgo/conf v0.47.0 + github.com/chai2010/protorpc v1.1.3 github.com/golang/protobuf v1.5.2 ) + +require ( + github.com/golang/snappy v0.0.3 // indirect + google.golang.org/protobuf v1.26.0 // indirect +) diff --git a/go.sum b/go.sum index f0c63f1..f8018c9 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,16 @@ -git.tetele.net/tgo/conf v0.25.0 h1:fopDch45xw/di5fLvvzwltFiGiXrilMobZwQNO678Wo= -git.tetele.net/tgo/conf v0.25.0/go.mod h1:DogEBvxG2fGdukpoobTVFE2b4Fd5OTE9FJ3Xetyn47E= -git.tetele.net/tgo/conf v0.35.3 h1:OQEa87qN5bAbscjMhaoTRinLnv8xZg1WErl5JXgFZco= -git.tetele.net/tgo/conf v0.35.3/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= -git.tetele.net/tgo/helper v0.2.6 h1:JC+N+If/bGvcwuUSklva17YDNtKBV2Qpvl5RQpMySOY= -git.tetele.net/tgo/helper v0.2.6/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= -github.com/chai2010/protorpc v1.0.0 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo= -github.com/chai2010/protorpc v1.0.0/go.mod h1:woR3WwjaQDqFjlzdVsFEKiK5Ur12QL8mYxVPjfr5z54= +git.tetele.net/tgo/conf v0.47.0 h1:2nGvXjyFV6gO7BPliG4lwERP/bgTOeSJpUv/I2ALGPg= +git.tetele.net/tgo/conf v0.47.0/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= +github.com/chai2010/protorpc v1.1.3 h1:VJK5hIoZn0XCGol0GmbxZkUG6FbTI5LP2Lam6RVd15w= +github.com/chai2010/protorpc v1.1.3/go.mod h1:/wO0kiyVdu7ug8dCMrA2yDr2vLfyhsLEuzLa9J2HJ+I= github.com/golang/protobuf v1.0.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/golang/snappy v0.0.0-20170215233205-553a64147049 h1:K9KHZbXKpGydfDN0aZrsoHpLJlZsBrGMFWbgLDGnPZk= -github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= diff --git a/hash.go b/hash.go index ac3a2fe..93bb092 100644 --- a/hash.go +++ b/hash.go @@ -174,8 +174,7 @@ func HExists(key string, field string, url ...string) (int64, error) { return res.GetRet(), nil } - -func HIncrby(key, field string,value int, url ...string) (int64, error) { +func HIncrby(key, field string, value int, url ...string) (int64, error) { conn, _, err := Conn(url...) @@ -195,4 +194,4 @@ func HIncrby(key, field string,value int, url ...string) (int64, error) { } return res.GetRet(), nil -} \ No newline at end of file +} diff --git a/redis.pb.go b/redis.pb.go index 0a41055..e6c812c 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -31,6 +31,10 @@ It has these top-level messages: LLenRequest AddRequest AddResponse + ZSetRequest + ZSetResponse + ZRangeRequest + ZScanRequest */ package redisrpc @@ -516,6 +520,121 @@ func (m *AddResponse) GetRet() int64 { return 0 } +// 有序集合添加值 +type ZSetRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Score *string `protobuf:"bytes,2,opt,name=score" json:"score,omitempty"` + Member *string `protobuf:"bytes,3,opt,name=member" json:"member,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *ZSetRequest) Reset() { *m = ZSetRequest{} } +func (m *ZSetRequest) String() string { return proto.CompactTextString(m) } +func (*ZSetRequest) ProtoMessage() {} + +func (m *ZSetRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *ZSetRequest) GetScore() string { + if m != nil && m.Score != nil { + return *m.Score + } + return "" +} + +func (m *ZSetRequest) GetMember() string { + if m != nil && m.Member != nil { + return *m.Member + } + return "" +} + +// 设置响应结构 +type ZSetResponse struct { + Ret *int64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *ZSetResponse) Reset() { *m = ZSetResponse{} } +func (m *ZSetResponse) String() string { return proto.CompactTextString(m) } +func (*ZSetResponse) ProtoMessage() {} + +func (m *ZSetResponse) GetRet() int64 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + +// 有序集合取值 +type ZRangeRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Start *int64 `protobuf:"varint,2,opt,name=start" json:"start,omitempty"` + Stop *int64 `protobuf:"varint,3,opt,name=stop" json:"stop,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *ZRangeRequest) Reset() { *m = ZRangeRequest{} } +func (m *ZRangeRequest) String() string { return proto.CompactTextString(m) } +func (*ZRangeRequest) ProtoMessage() {} + +func (m *ZRangeRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *ZRangeRequest) GetStart() int64 { + if m != nil && m.Start != nil { + return *m.Start + } + return 0 +} + +func (m *ZRangeRequest) GetStop() int64 { + if m != nil && m.Stop != nil { + return *m.Stop + } + return 0 +} + +type ZScanRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Field *string `protobuf:"bytes,2,opt,name=field" json:"field,omitempty"` + Len *int64 `protobuf:"varint,3,opt,name=len" json:"len,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *ZScanRequest) Reset() { *m = ZScanRequest{} } +func (m *ZScanRequest) String() string { return proto.CompactTextString(m) } +func (*ZScanRequest) ProtoMessage() {} + +func (m *ZScanRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *ZScanRequest) GetField() string { + if m != nil && m.Field != nil { + return *m.Field + } + return "" +} + +func (m *ZScanRequest) GetLen() int64 { + if m != nil && m.Len != nil { + return *m.Len + } + return 0 +} + func init() { } @@ -544,6 +663,10 @@ type RedisService interface { ReduceStock(in *SetRequest, out *SetResponse) error Incrby(in *AddRequest, out *AddResponse) error Decrby(in *AddRequest, out *AddResponse) error + ZAdd(in *ZSetRequest, out *ZSetResponse) error + ZIncrBy(in *ZSetRequest, out *ZSetResponse) error + ZRange(in *ZRangeRequest, out *HGetListResponse) error + ZScan(in *ZScanRequest, out *HGetListResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -687,6 +810,18 @@ func (c *RedisServiceClient) Incrby(in *AddRequest, out *AddResponse) error { func (c *RedisServiceClient) Decrby(in *AddRequest, out *AddResponse) error { return c.Call("RedisService.Decrby", in, out) } +func (c *RedisServiceClient) ZAdd(in *ZSetRequest, out *ZSetResponse) error { + return c.Call("RedisService.ZAdd", in, out) +} +func (c *RedisServiceClient) ZIncrBy(in *ZSetRequest, out *ZSetResponse) error { + return c.Call("RedisService.ZIncrBy", in, out) +} +func (c *RedisServiceClient) ZRange(in *ZRangeRequest, out *HGetListResponse) error { + return c.Call("RedisService.ZRange", in, out) +} +func (c *RedisServiceClient) ZScan(in *ZScanRequest, out *HGetListResponse) error { + return c.Call("RedisService.ZScan", 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 b998cd7..65f1998 100644 --- a/redis.proto +++ b/redis.proto @@ -112,6 +112,29 @@ message AddResponse { int64 ret = 1; } +//有序集合添加值 +message ZSetRequest{ + string key = 1; + string score = 2; + string member = 3; +} +// 设置响应结构 +message ZSetResponse { + int64 ret = 1; +} +//有序集合取值 +message ZRangeRequest{ + string key = 1; + int64 start = 2; + int64 stop = 3; +} + +message ZScanRequest { + string key = 1; + string field = 2; + int64 len = 3; +} + // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 @@ -138,4 +161,8 @@ service RedisService { rpc ReduceStock(SetRequest) returns(SetResponse);//扣减库存 rpc Incrby(AddRequest) returns(AddResponse);//增加 rpc Decrby(AddRequest) returns(AddResponse);//减 + rpc ZAdd(ZSetRequest) returns (ZSetResponse); //添加进入有序集合 + rpc ZIncrBy(ZSetRequest) returns (ZSetResponse); //有序集合权重递增 + rpc ZRange(ZRangeRequest) returns (HGetListResponse); + rpc ZScan(ZScanRequest) returns(HGetListResponse);//zset match } \ No newline at end of file diff --git a/set.go b/set.go index f85a481..634e563 100644 --- a/set.go +++ b/set.go @@ -1,6 +1,9 @@ package redisrpc import ( + "encoding/json" + "log" + "github.com/golang/protobuf/proto" ) @@ -75,11 +78,10 @@ func SIsmember(key string, field string, url ...string) (int64, error) { return res.GetRet(), nil } - /** * 集合成员数 */ -func SCard(key string,url ...string) (int64, error) { +func SCard(key string, url ...string) (int64, error) { conn, _, err := Conn(url...) @@ -88,7 +90,7 @@ func SCard(key string,url ...string) (int64, error) { } defer conn.Close() - req := &SMembersRequest{proto.String(key),nil} + req := &SMembersRequest{proto.String(key), nil} res := &SSetResponse{} @@ -99,4 +101,113 @@ func SCard(key string,url ...string) (int64, error) { } return res.GetRet(), nil -} \ No newline at end of file +} + +//设置 +func ZAdd(key, score, member string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &ZSetRequest{proto.String(key), proto.String(score), proto.String(member), nil} + + res := &ZSetResponse{} + + err = conn.ZAdd(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +//递增 +func ZIncrBy(key, inc, member string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &ZSetRequest{proto.String(key), proto.String(inc), proto.String(member), nil} + + res := &ZSetResponse{} + + err = conn.ZIncrBy(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +/** + * 集合成员 + */ +func ZRange(key string, start, stop int64, url ...string) ([]map[string]string, error) { + + conn, _, err := Conn(url...) + if err != nil { + return nil, err + } + defer conn.Close() + + req := &ZRangeRequest{proto.String(key), proto.Int64(start), proto.Int64(stop), nil} + res := &HGetListResponse{} + + err = conn.ZRange(req, res) + + if err != nil { + return nil, err + } + + value := res.GetList() + var list []map[string]string + + err = json.Unmarshal(value, &list) + if err != nil { + log.Println("json unmarshal error:", err) + return nil, err + } + return list, nil +} + +/** + * 集合成员 + */ +func ZScan(key, field string, len int64, url ...string) ([]string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return nil, err + } + defer conn.Close() + + req := &ZScanRequest{proto.String(key), proto.String(field), proto.Int64(len), nil} + res := &HGetListResponse{} + + err = conn.ZScan(req, res) + if err != nil { + return nil, err + } + + value := res.GetList() + var list []string + + err = json.Unmarshal(value, &list) + + if err != nil { + log.Println("json unmarshal error:", err) + return nil, err + } + return list, nil +} diff --git a/set_test.go b/set_test.go index a3b580e..2983464 100644 --- a/set_test.go +++ b/set_test.go @@ -2,8 +2,9 @@ package redisrpc import ( // "strconv" + "testing" - // "tgo/helper" + // "unicode/utf8" ) func Test_SAdd(t *testing.T) { @@ -20,9 +21,25 @@ func Test_SAdd(t *testing.T) { } -func Test_SCard(t *testing.T){ - res,err := SCard("test_article") +func Test_SCard(t *testing.T) { + res, err := SCard("test_article") t.Log(res) t.Log(err) -} \ No newline at end of file +} + +func Test_ZSet(t *testing.T) { + + Del("zset-test") + res, err := ZAdd("zset-test", "2", "{\"keyword\":\"test\",\"id\":\"12\"}") + t.Log("ZAdd: ", res, err) + + ress, err := ZScan("zset-test", "*test*", 10) + t.Log("ZScan: ", ress, err) + + list, err := ZRange("zset-test", 0, 100) + t.Log("ZRangexx: ", list, err) + + // res, err = ZIncrBy("zset-test", "2", "test") + // t.Log("ZIncrBy: ", res, err) +}