Browse Source

修改请求和返回数据类型

master
guzeng 3 years ago
parent
commit
590e234649
9 changed files with 389 additions and 52 deletions
  1. +17
    -0
      conn.go
  2. +8
    -0
      go.mod
  3. +14
    -0
      go.sum
  4. +9
    -8
      hash.go
  5. +44
    -0
      hash_test.go
  6. +183
    -28
      redis.pb.go
  7. +39
    -6
      redis.proto
  8. +53
    -10
      string.go
  9. +22
    -0
      string_test.go

+ 17
- 0
conn.go View File

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

+ 8
- 0
go.mod View File

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

+ 14
- 0
go.sum View File

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

+ 9
- 8
hash.go View File

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

+ 44
- 0
hash_test.go View File

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

+ 183
- 28
redis.pb.go View File

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


+ 39
- 6
redis.proto View File

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

+ 53
- 10
string.go View File

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

+ 22
- 0
string_test.go View File

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

Loading…
Cancel
Save