Browse Source

同步跃恒redis

master v1.0.0
lijianbin 2 years ago
parent
commit
c5138187e6
8 changed files with 315 additions and 30 deletions
  1. +0
    -5
      conn.go
  2. +8
    -4
      go.mod
  3. +7
    -10
      go.sum
  4. +2
    -3
      hash.go
  5. +135
    -0
      redis.pb.go
  6. +27
    -0
      redis.proto
  7. +115
    -4
      set.go
  8. +21
    -4
      set_test.go

+ 0
- 5
conn.go View File

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

+ 8
- 4
go.mod View File

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

+ 7
- 10
go.sum View File

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


+ 2
- 3
hash.go View File

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

+ 135
- 0
redis.pb.go View File

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


+ 27
- 0
redis.proto View File

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

+ 115
- 4
set.go View File

@ -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
}
}
//设置
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
}

+ 21
- 4
set_test.go View File

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

Loading…
Cancel
Save