From 590e23464905381c45ece95dbd59f7c72c803fd2 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 21 May 2021 10:10:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AF=B7=E6=B1=82=E5=92=8C?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conn.go | 17 ++++ go.mod | 8 ++ go.sum | 14 ++++ hash.go | 17 ++-- hash_test.go | 44 +++++++++++ redis.pb.go | 211 ++++++++++++++++++++++++++++++++++++++++++------- redis.proto | 45 +++++++++-- string.go | 63 ++++++++++++--- string_test.go | 22 ++++++ 9 files changed, 389 insertions(+), 52 deletions(-) create mode 100644 conn.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 hash_test.go create mode 100644 string_test.go diff --git a/conn.go b/conn.go new file mode 100644 index 0000000..b5ecadd --- /dev/null +++ b/conn.go @@ -0,0 +1,17 @@ +package redisrpc + +import ( + "net/rpc" + + "git.tetele.net/tgo/conf" +) + +func Conn(url ...string) (*RedisServiceClient, *rpc.Client, error) { + + var rpc_url string = "127.0.0.1:" + conf.REDIS_RPC_PORT + if len(url) > 0 && url[0] != "" { + rpc_url = url[0] + } + + return DialRedisService("tcp", rpc_url) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b3f3d50 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module git.tetele.net/tgo/redisrpc + +go 1.14 + +require ( + github.com/chai2010/protorpc v1.0.0 // indirect + github.com/golang/protobuf v1.5.2 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..35feb65 --- /dev/null +++ b/go.sum @@ -0,0 +1,14 @@ +github.com/chai2010/protorpc v1.0.0 h1:aJ45G9sl1utSKo35EqnBSTs5jqTpdJDJAuZMMYPAtFo= +github.com/chai2010/protorpc v1.0.0/go.mod h1:woR3WwjaQDqFjlzdVsFEKiK5Ur12QL8mYxVPjfr5z54= +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/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/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= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= diff --git a/hash.go b/hash.go index 95fdf21..1572553 100644 --- a/hash.go +++ b/hash.go @@ -1,34 +1,35 @@ package redisrpc import ( + "git.tetele.net/tgo/conf" "github.com/golang/protobuf/proto" ) /** * 使用用户名查询 */ -func GetByHashKey(key, field string, url ...string) (string, error) { +func HGetString(key, field string, url ...string) (string, error) { - var user_rpc_url string = "127.0.0.1:7953" + var rpc_url string = "127.0.0.1:" + conf.REDIS_RPC_PORT if len(url) > 0 && url[0] != "" { - user_rpc_url = url[0] + rpc_url = url[0] } - conn, _, err := DialRedisService("tcp", user_rpc_url) + conn, _, err := DialRedisService("tcp", rpc_url) if err != nil { return "", err } defer conn.Close() - req := &GetHashKey{proto.String(key), proto.String(field), nil} + req := &HGetRequest{proto.String(key), proto.String(field), nil} - res := &GetResponse{} + res := &GetStringResponse{} - err = conn.GetByHashKey(req, res) + err = conn.HGet(req, res) if err != nil { return "", err } - return res.GetString_(), nil + return res.GetValue(), nil } diff --git a/hash_test.go b/hash_test.go new file mode 100644 index 0000000..75f19fc --- /dev/null +++ b/hash_test.go @@ -0,0 +1,44 @@ +package redisrpc + +import ( + // "strconv" + "testing" + // "tgo/helper" +) + +func Test_HSet(t *testing.T) { + + // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} + + // reply, err := HSet("testing2", "test1", val) + + // t.Log(reply) + // t.Log(err) + + ret, err := GetByHashKey("testing", "test") + + t.Log(ret) + if err == nil { + t.Log("nil", err) + } else { + t.Log("error:", err) + } + + // ret2, err := HSetExpire("testing2", 200) + // t.Log(ret2) + // t.Log(err) + + // ret3, err := HMSet("testing2", "test1", "test1") + // t.Log(ret3) + // t.Log(err) + + // ret4, err := HGetAll("1000064_product") + // // ret := ret4.([]interface{}) + // // t.Log() + // for _, val := range ret4 { + // // str := strconv.FormatUint(val.([]uint64), 10) + // // t.Log(helper.ToStr(val)) + // t.Log(string(val)) + // } + // t.Log(err) +} diff --git a/redis.pb.go b/redis.pb.go index 299691d..f8d81cf 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -9,9 +9,15 @@ It is generated from these files: redis.proto It has these top-level messages: - GetKey - GetHashKey - GetResponse + GetRequest + SetRequest + DelRequest + HGetRequest + HSetRequest + HDelRequest + GetStringResponse + SetResponse + DelResponse */ package redisrpc @@ -30,16 +36,57 @@ var _ = proto.Marshal var _ = math.Inf // 使用key查询 -type GetKey struct { +type GetRequest struct { Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` XXX_unrecognized []byte `json:"-"` } -func (m *GetKey) Reset() { *m = GetKey{} } -func (m *GetKey) String() string { return proto.CompactTextString(m) } -func (*GetKey) ProtoMessage() {} +func (m *GetRequest) Reset() { *m = GetRequest{} } +func (m *GetRequest) String() string { return proto.CompactTextString(m) } +func (*GetRequest) ProtoMessage() {} -func (m *GetKey) GetKey() string { +func (m *GetRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +// 设置key +type SetRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Value *string `protobuf:"bytes,2,opt,name=value" json:"value,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SetRequest) Reset() { *m = SetRequest{} } +func (m *SetRequest) String() string { return proto.CompactTextString(m) } +func (*SetRequest) ProtoMessage() {} + +func (m *SetRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *SetRequest) GetValue() string { + if m != nil && m.Value != nil { + return *m.Value + } + return "" +} + +type DelRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *DelRequest) Reset() { *m = DelRequest{} } +func (m *DelRequest) String() string { return proto.CompactTextString(m) } +func (*DelRequest) ProtoMessage() {} + +func (m *DelRequest) GetKey() string { if m != nil && m.Key != nil { return *m.Key } @@ -47,24 +94,82 @@ func (m *GetKey) GetKey() string { } // 使用hash key查询 -type GetHashKey struct { +type HGetRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Field *string `protobuf:"bytes,2,opt,name=field" json:"field,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *HGetRequest) Reset() { *m = HGetRequest{} } +func (m *HGetRequest) String() string { return proto.CompactTextString(m) } +func (*HGetRequest) ProtoMessage() {} + +func (m *HGetRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *HGetRequest) GetField() string { + if m != nil && m.Field != nil { + return *m.Field + } + return "" +} + +// 设置hash key +type HSetRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Field *string `protobuf:"bytes,2,opt,name=field" json:"field,omitempty"` + Value *string `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *HSetRequest) Reset() { *m = HSetRequest{} } +func (m *HSetRequest) String() string { return proto.CompactTextString(m) } +func (*HSetRequest) ProtoMessage() {} + +func (m *HSetRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *HSetRequest) GetField() string { + if m != nil && m.Field != nil { + return *m.Field + } + return "" +} + +func (m *HSetRequest) GetValue() string { + if m != nil && m.Value != nil { + return *m.Value + } + return "" +} + +// 删除hash key +type HDelRequest struct { Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` Field *string `protobuf:"bytes,2,opt,name=field" json:"field,omitempty"` XXX_unrecognized []byte `json:"-"` } -func (m *GetHashKey) Reset() { *m = GetHashKey{} } -func (m *GetHashKey) String() string { return proto.CompactTextString(m) } -func (*GetHashKey) ProtoMessage() {} +func (m *HDelRequest) Reset() { *m = HDelRequest{} } +func (m *HDelRequest) String() string { return proto.CompactTextString(m) } +func (*HDelRequest) ProtoMessage() {} -func (m *GetHashKey) GetKey() string { +func (m *HDelRequest) GetKey() string { if m != nil && m.Key != nil { return *m.Key } return "" } -func (m *GetHashKey) GetField() string { +func (m *HDelRequest) GetField() string { if m != nil && m.Field != nil { return *m.Field } @@ -72,28 +177,66 @@ func (m *GetHashKey) GetField() string { } // 使用key查询响应结构 -type GetResponse struct { - String_ *string `protobuf:"bytes,1,opt,name=string" json:"string,omitempty"` +type GetStringResponse struct { + Value *string `protobuf:"bytes,1,opt,name=value" json:"value,omitempty"` XXX_unrecognized []byte `json:"-"` } -func (m *GetResponse) Reset() { *m = GetResponse{} } -func (m *GetResponse) String() string { return proto.CompactTextString(m) } -func (*GetResponse) ProtoMessage() {} +func (m *GetStringResponse) Reset() { *m = GetStringResponse{} } +func (m *GetStringResponse) String() string { return proto.CompactTextString(m) } +func (*GetStringResponse) ProtoMessage() {} -func (m *GetResponse) GetString_() string { - if m != nil && m.String_ != nil { - return *m.String_ +func (m *GetStringResponse) GetValue() string { + if m != nil && m.Value != nil { + return *m.Value } return "" } +// 设置key响应结构 +type SetResponse struct { + Ret *uint32 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SetResponse) Reset() { *m = SetResponse{} } +func (m *SetResponse) String() string { return proto.CompactTextString(m) } +func (*SetResponse) ProtoMessage() {} + +func (m *SetResponse) GetRet() uint32 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + +// 删除key响应结构 +type DelResponse struct { + Ret *uint32 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *DelResponse) Reset() { *m = DelResponse{} } +func (m *DelResponse) String() string { return proto.CompactTextString(m) } +func (*DelResponse) ProtoMessage() {} + +func (m *DelResponse) GetRet() uint32 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + func init() { } type RedisService interface { - GetByKey(in *GetKey, out *GetResponse) error - GetByHashKey(in *GetHashKey, out *GetResponse) error + Get(in *GetRequest, out *GetStringResponse) error + Set(in *SetRequest, out *SetResponse) error + Del(in *DelRequest, out *DelResponse) error + HGet(in *HGetRequest, out *GetStringResponse) error + HSet(in *HSetRequest, out *SetResponse) error + HDel(in *HDelRequest, out *DelResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -165,11 +308,23 @@ func NewRedisServiceClient(conn io.ReadWriteCloser) (*RedisServiceClient, *rpc.C return &RedisServiceClient{c}, c } -func (c *RedisServiceClient) GetByKey(in *GetKey, out *GetResponse) error { - return c.Call("RedisService.GetByKey", in, out) +func (c *RedisServiceClient) Get(in *GetRequest, out *GetStringResponse) error { + return c.Call("RedisService.Get", in, out) +} +func (c *RedisServiceClient) Set(in *SetRequest, out *SetResponse) error { + return c.Call("RedisService.Set", in, out) +} +func (c *RedisServiceClient) Del(in *DelRequest, out *DelResponse) error { + return c.Call("RedisService.Del", in, out) +} +func (c *RedisServiceClient) HGet(in *HGetRequest, out *GetStringResponse) error { + return c.Call("RedisService.HGet", in, out) +} +func (c *RedisServiceClient) HSet(in *HSetRequest, out *SetResponse) error { + return c.Call("RedisService.HSet", in, out) } -func (c *RedisServiceClient) GetByHashKey(in *GetHashKey, out *GetResponse) error { - return c.Call("RedisService.GetByHashKey", in, out) +func (c *RedisServiceClient) HDel(in *HDelRequest, out *DelResponse) error { + return c.Call("RedisService.HDel", in, out) } // DialRedisService connects to an RedisService at the specified network address. diff --git a/redis.proto b/redis.proto index 8673d18..767fa7c 100644 --- a/redis.proto +++ b/redis.proto @@ -2,23 +2,56 @@ syntax = "proto3"; package redisrpc; // 使用key查询 -message GetKey { +message GetRequest { + string key = 1; +} +// 设置key +message SetRequest { + string key = 1; + string value = 2; +} +message DelRequest { string key = 1; } // 使用hash key查询 -message GetHashKey { +message HGetRequest { + string key = 1; + string field = 2; +} + +// 设置hash key +message HSetRequest { + string key = 1; + string field = 2; + string value = 3; +} + +// 删除hash key +message HDelRequest { string key = 1; string field = 2; } // 使用key查询响应结构 -message GetResponse { - string string = 1; +message GetStringResponse { + string value = 1; } +// 设置key响应结构 +message SetResponse { + uint32 ret = 1; +} +// 删除key响应结构 +message DelResponse { + uint32 ret = 1; +} // rpc方法 service RedisService { - rpc getByKey (GetKey) returns (GetResponse); // 使用key查询 - rpc getByHashKey (GetHashKey) returns (GetResponse); // 使用hash key查询 + rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 + rpc Set (SetRequest) returns (SetResponse); + rpc Del (DelRequest) returns (DelResponse); + rpc HGet (HGetRequest) returns (GetStringResponse); // 使用hash key查询 + rpc HSet (HSetRequest) returns (SetResponse); + rpc HDel (HDelRequest) returns (DelResponse); } \ No newline at end of file diff --git a/string.go b/string.go index 5fb8bc3..8eb11fc 100644 --- a/string.go +++ b/string.go @@ -2,33 +2,76 @@ package redisrpc import ( "github.com/golang/protobuf/proto" + "github.com/gomodule/redigo/redis" ) /** * 使用用户名查询 */ -func GetByKey(key string, url ...string) (string, error) { +func GetString(key string, url ...string) (string, error) { - var user_rpc_url string = "127.0.0.1:7953" - if len(url) > 0 && url[0] != "" { - user_rpc_url = url[0] - } + conn, _, err := Conn(url...) - conn, _, err := DialRedisService("tcp", user_rpc_url) if err != nil { return "", err } defer conn.Close() - req := &GetKey{proto.String(key), nil} + req := &GetRequest{proto.String(key), nil} - res := &GetResponse{} + res := &GetStringResponse{} - err = conn.GetByKey(req, res) + err = conn.Get(req, res) if err != nil { return "", err } - return res.GetString_(), nil + return res.GetValue(), nil +} + +//设置 +func Set(key, value string, url ...string) (uint32, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &SetRequest{proto.String(key), proto.String(value), nil} + + res := &SetResponse{} + + err = conn.Set(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +//设置 +func Del(key, value string, url ...string) (uint32, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &DelRequest{proto.String(key), nil} + + res := &DelResponse{} + + err = conn.Del(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil } diff --git a/string_test.go b/string_test.go new file mode 100644 index 0000000..16a7952 --- /dev/null +++ b/string_test.go @@ -0,0 +1,22 @@ +package redisrpc + +import ( + "testing" + // "time" +) + +func Test_Get(t *testing.T) { + + reply, err := Get("siteListCache") + + t.Log(reply) + t.Log(err) +} + +// func Test_Set(t *testing.T) { + +// c, err := Set("test", 1111, 7200) +// t.Log(c) +// t.Log(err) + +// }