From e63eb33904b0df0ac8a7a975cbc5eaeb0963553e Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 31 Aug 2021 14:57:04 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=9B=86=E5=90=88?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ redis.proto | 12 ++++++++++++ 2 files changed, 64 insertions(+) diff --git a/redis.pb.go b/redis.pb.go index 9e4279e..c8758c9 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -22,6 +22,8 @@ It has these top-level messages: DelResponse HSetResponse SetExpireResponse + SAddRequest + SMembersRequest */ package redisrpc @@ -315,6 +317,48 @@ func (m *SetExpireResponse) GetRet() int64 { return 0 } +// 集合添加值 +type SAddRequest 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 *SAddRequest) Reset() { *m = SAddRequest{} } +func (m *SAddRequest) String() string { return proto.CompactTextString(m) } +func (*SAddRequest) ProtoMessage() {} + +func (m *SAddRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *SAddRequest) GetField() string { + if m != nil && m.Field != nil { + return *m.Field + } + return "" +} + +// 集合添加值 +type SMembersRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SMembersRequest) Reset() { *m = SMembersRequest{} } +func (m *SMembersRequest) String() string { return proto.CompactTextString(m) } +func (*SMembersRequest) ProtoMessage() {} + +func (m *SMembersRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + func init() { } @@ -329,6 +373,8 @@ type RedisService interface { HGetAll(in *GetRequest, out *HGetListResponse) error HExists(in *HGetRequest, out *DelResponse) error Exists(in *GetRequest, out *DelResponse) error + SAdd(in *SAddRequest, out *HSetResponse) error + SIsmember(in *SAddRequest, out *HSetResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -430,6 +476,12 @@ func (c *RedisServiceClient) HExists(in *HGetRequest, out *DelResponse) error { func (c *RedisServiceClient) Exists(in *GetRequest, out *DelResponse) error { return c.Call("RedisService.Exists", in, out) } +func (c *RedisServiceClient) SAdd(in *SAddRequest, out *HSetResponse) error { + return c.Call("RedisService.SAdd", in, out) +} +func (c *RedisServiceClient) SIsmember(in *SAddRequest, out *HSetResponse) error { + return c.Call("RedisService.SIsmember", 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 b81d559..c5f4635 100644 --- a/redis.proto +++ b/redis.proto @@ -67,6 +67,16 @@ message SetExpireResponse { int64 ret = 1; } +//集合添加值 +message SAddRequest{ + string key = 1; + string field = 2; +} +//集合添加值 +message SMembersRequest{ + string key = 1; +} + // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 @@ -79,4 +89,6 @@ service RedisService { rpc HGetAll(GetRequest) returns (HGetListResponse); //get hash all rpc HExists(HGetRequest) returns (DelResponse); //hash键是否存在 rpc Exists(GetRequest) returns (DelResponse); //键是否存在 + rpc SAdd(SAddRequest) returns (HSetResponse); //添加进入集合 + rpc SIsmember(SAddRequest) returns (HSetResponse); //集合中是否存在值 } \ No newline at end of file From bdf4e2045fed0aea420c4f56d5125a4e4d0637e7 Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 31 Aug 2021 15:04:10 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=9B=86=E5=90=88rem?= =?UTF-8?q?=E7=9A=84=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 44 +++++++++++++++++++++++++++++++++----------- redis.proto | 11 ++++++++--- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/redis.pb.go b/redis.pb.go index c8758c9..27afd5f 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -22,8 +22,9 @@ It has these top-level messages: DelResponse HSetResponse SetExpireResponse - SAddRequest + SSetRequest SMembersRequest + SSetResponse */ package redisrpc @@ -318,24 +319,24 @@ func (m *SetExpireResponse) GetRet() int64 { } // 集合添加值 -type SAddRequest struct { +type SSetRequest 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 *SAddRequest) Reset() { *m = SAddRequest{} } -func (m *SAddRequest) String() string { return proto.CompactTextString(m) } -func (*SAddRequest) ProtoMessage() {} +func (m *SSetRequest) Reset() { *m = SSetRequest{} } +func (m *SSetRequest) String() string { return proto.CompactTextString(m) } +func (*SSetRequest) ProtoMessage() {} -func (m *SAddRequest) GetKey() string { +func (m *SSetRequest) GetKey() string { if m != nil && m.Key != nil { return *m.Key } return "" } -func (m *SAddRequest) GetField() string { +func (m *SSetRequest) GetField() string { if m != nil && m.Field != nil { return *m.Field } @@ -359,6 +360,23 @@ func (m *SMembersRequest) GetKey() string { return "" } +// 设置响应结构 +type SSetResponse struct { + Ret *int64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SSetResponse) Reset() { *m = SSetResponse{} } +func (m *SSetResponse) String() string { return proto.CompactTextString(m) } +func (*SSetResponse) ProtoMessage() {} + +func (m *SSetResponse) GetRet() int64 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + func init() { } @@ -373,8 +391,9 @@ type RedisService interface { HGetAll(in *GetRequest, out *HGetListResponse) error HExists(in *HGetRequest, out *DelResponse) error Exists(in *GetRequest, out *DelResponse) error - SAdd(in *SAddRequest, out *HSetResponse) error - SIsmember(in *SAddRequest, out *HSetResponse) error + SAdd(in *SSetRequest, out *SSetResponse) error + SIsmember(in *SSetRequest, out *SSetResponse) error + SRem(in *SSetRequest, out *SSetResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -476,12 +495,15 @@ func (c *RedisServiceClient) HExists(in *HGetRequest, out *DelResponse) error { func (c *RedisServiceClient) Exists(in *GetRequest, out *DelResponse) error { return c.Call("RedisService.Exists", in, out) } -func (c *RedisServiceClient) SAdd(in *SAddRequest, out *HSetResponse) error { +func (c *RedisServiceClient) SAdd(in *SSetRequest, out *SSetResponse) error { return c.Call("RedisService.SAdd", in, out) } -func (c *RedisServiceClient) SIsmember(in *SAddRequest, out *HSetResponse) error { +func (c *RedisServiceClient) SIsmember(in *SSetRequest, out *SSetResponse) error { return c.Call("RedisService.SIsmember", in, out) } +func (c *RedisServiceClient) SRem(in *SSetRequest, out *SSetResponse) error { + return c.Call("RedisService.SRem", 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 c5f4635..ef1d92b 100644 --- a/redis.proto +++ b/redis.proto @@ -68,7 +68,7 @@ message SetExpireResponse { } //集合添加值 -message SAddRequest{ +message SSetRequest{ string key = 1; string field = 2; } @@ -76,6 +76,10 @@ message SAddRequest{ message SMembersRequest{ string key = 1; } +// 设置响应结构 +message SSetResponse { + int64 ret = 1; +} // rpc方法 service RedisService { @@ -89,6 +93,7 @@ service RedisService { rpc HGetAll(GetRequest) returns (HGetListResponse); //get hash all rpc HExists(HGetRequest) returns (DelResponse); //hash键是否存在 rpc Exists(GetRequest) returns (DelResponse); //键是否存在 - rpc SAdd(SAddRequest) returns (HSetResponse); //添加进入集合 - rpc SIsmember(SAddRequest) returns (HSetResponse); //集合中是否存在值 + rpc SAdd(SSetRequest) returns (SSetResponse); //添加进入集合 + rpc SIsmember(SSetRequest) returns (SSetResponse); //集合中是否存在值 + rpc SRem(SSetRequest) returns (SSetResponse); //集合中删除值 } \ No newline at end of file From 3f3325e46f79838332547099ecf86de2c7f7c41c Mon Sep 17 00:00:00 2001 From: guzeng Date: Tue, 31 Aug 2021 15:24:11 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- conn.go | 7 ++++- go.mod | 2 +- go.sum | 2 ++ set.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++ set_test.go | 21 +++++++++++++++ 5 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 set.go create mode 100644 set_test.go diff --git a/conn.go b/conn.go index b5ecadd..d4e7d2e 100644 --- a/conn.go +++ b/conn.go @@ -8,10 +8,15 @@ import ( func Conn(url ...string) (*RedisServiceClient, *rpc.Client, error) { - var rpc_url string = "127.0.0.1:" + conf.REDIS_RPC_PORT + var rpc_url string if len(url) > 0 && url[0] != "" { rpc_url = url[0] + } else if conf.REDIS_RPC_URL != "" { + rpc_url = conf.REDIS_RPC_URL + } else { + rpc_url = "127.0.0.1:" + conf.REDIS_RPC_PORT } return DialRedisService("tcp", rpc_url) + } diff --git a/go.mod b/go.mod index df3f1ed..82ea8b2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module git.tetele.net/tgo/redisrpc go 1.14 require ( - git.tetele.net/tgo/conf v0.25.0 + git.tetele.net/tgo/conf v0.35.3 github.com/chai2010/protorpc v1.0.0 github.com/golang/protobuf v1.5.2 ) diff --git a/go.sum b/go.sum index 778c639..ef593ba 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ 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= 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= diff --git a/set.go b/set.go new file mode 100644 index 0000000..6c86056 --- /dev/null +++ b/set.go @@ -0,0 +1,76 @@ +package redisrpc + +import ( + "github.com/golang/protobuf/proto" +) + +//设置 +func SAdd(key, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &SSetRequest{proto.String(key), proto.String(field), nil} + + res := &SSetResponse{} + + err = conn.SAdd(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +//删除 +func SRem(key string, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &SSetRequest{proto.String(key), proto.String(field), nil} + + res := &SSetResponse{} + + err = conn.SRem(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +/** + * 全部 + */ +func SIsmember(key string, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &SSetRequest{proto.String(key), proto.String(field), nil} + + res := &SSetResponse{} + + err = conn.SIsmember(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} diff --git a/set_test.go b/set_test.go new file mode 100644 index 0000000..be10b2f --- /dev/null +++ b/set_test.go @@ -0,0 +1,21 @@ +package redisrpc + +import ( + // "strconv" + "testing" + // "tgo/helper" +) + +func Test_SAdd(t *testing.T) { + + // reply, err := SAdd("test", "44") + reply, err := SIsmember("test", "44") + t.Log(reply) + t.Log(err) + + reply, err = SRem("test", "44") + + t.Log(reply) + t.Log(err) + +} From 340dbbd205d932b241b68dd46617f8136e1b1bf8 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 15 Oct 2021 16:33:13 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++ redis.proto | 22 +++++++++++++ 2 files changed, 116 insertions(+) diff --git a/redis.pb.go b/redis.pb.go index 27afd5f..c7dbd17 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -25,6 +25,9 @@ It has these top-level messages: SSetRequest SMembersRequest SSetResponse + LSetRequest + LSetResponse + LRangeRequest */ package redisrpc @@ -377,6 +380,81 @@ func (m *SSetResponse) GetRet() int64 { return 0 } +// 列表添加值 +type LSetRequest 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 *LSetRequest) Reset() { *m = LSetRequest{} } +func (m *LSetRequest) String() string { return proto.CompactTextString(m) } +func (*LSetRequest) ProtoMessage() {} + +func (m *LSetRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *LSetRequest) GetField() string { + if m != nil && m.Field != nil { + return *m.Field + } + return "" +} + +// 设置key响应结构 +type LSetResponse struct { + Ret *int64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *LSetResponse) Reset() { *m = LSetResponse{} } +func (m *LSetResponse) String() string { return proto.CompactTextString(m) } +func (*LSetResponse) ProtoMessage() {} + +func (m *LSetResponse) GetRet() int64 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + +// 列表取值 +type LRangeRequest 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 *LRangeRequest) Reset() { *m = LRangeRequest{} } +func (m *LRangeRequest) String() string { return proto.CompactTextString(m) } +func (*LRangeRequest) ProtoMessage() {} + +func (m *LRangeRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *LRangeRequest) GetStart() int64 { + if m != nil && m.Start != nil { + return *m.Start + } + return 0 +} + +func (m *LRangeRequest) GetStop() int64 { + if m != nil && m.Stop != nil { + return *m.Stop + } + return 0 +} + func init() { } @@ -389,11 +467,15 @@ type RedisService interface { HDel(in *HDelRequest, out *DelResponse) error SetExpire(in *SetExpireRequest, out *SetExpireResponse) error HGetAll(in *GetRequest, out *HGetListResponse) error + HGetList(in *GetRequest, out *HGetListResponse) error HExists(in *HGetRequest, out *DelResponse) error Exists(in *GetRequest, out *DelResponse) error SAdd(in *SSetRequest, out *SSetResponse) error SIsmember(in *SSetRequest, out *SSetResponse) error SRem(in *SSetRequest, out *SSetResponse) error + LLpush(in *LSetRequest, out *LSetResponse) error + LRpush(in *LSetRequest, out *LSetResponse) error + LRange(in *LRangeRequest, out *HGetListResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -489,6 +571,9 @@ func (c *RedisServiceClient) SetExpire(in *SetExpireRequest, out *SetExpireRespo func (c *RedisServiceClient) HGetAll(in *GetRequest, out *HGetListResponse) error { return c.Call("RedisService.HGetAll", in, out) } +func (c *RedisServiceClient) HGetList(in *GetRequest, out *HGetListResponse) error { + return c.Call("RedisService.HGetList", in, out) +} func (c *RedisServiceClient) HExists(in *HGetRequest, out *DelResponse) error { return c.Call("RedisService.HExists", in, out) } @@ -504,6 +589,15 @@ func (c *RedisServiceClient) SIsmember(in *SSetRequest, out *SSetResponse) error func (c *RedisServiceClient) SRem(in *SSetRequest, out *SSetResponse) error { return c.Call("RedisService.SRem", in, out) } +func (c *RedisServiceClient) LLpush(in *LSetRequest, out *LSetResponse) error { + return c.Call("RedisService.LLpush", in, out) +} +func (c *RedisServiceClient) LRpush(in *LSetRequest, out *LSetResponse) error { + return c.Call("RedisService.LRpush", in, out) +} +func (c *RedisServiceClient) LRange(in *LRangeRequest, out *HGetListResponse) error { + return c.Call("RedisService.LRange", 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 ef1d92b..ff60703 100644 --- a/redis.proto +++ b/redis.proto @@ -81,6 +81,24 @@ message SSetResponse { int64 ret = 1; } +//列表添加值 +message LSetRequest{ + string key = 1; + string field = 2; +} + +// 设置key响应结构 +message LSetResponse { + int64 ret = 1; +} + +//列表取值 +message LRangeRequest{ + string key = 1; + int64 start = 2; + int64 stop = 3; +} + // rpc方法 service RedisService { rpc Get (GetRequest) returns (GetStringResponse); // 使用key查询 @@ -91,9 +109,13 @@ service RedisService { rpc HDel (HDelRequest) returns (DelResponse); rpc SetExpire (SetExpireRequest) returns (SetExpireResponse); //设置有效期 rpc HGetAll(GetRequest) returns (HGetListResponse); //get hash all + rpc HGetList(GetRequest) returns (HGetListResponse); //get hash all rpc HExists(HGetRequest) returns (DelResponse); //hash键是否存在 rpc Exists(GetRequest) returns (DelResponse); //键是否存在 rpc SAdd(SSetRequest) returns (SSetResponse); //添加进入集合 rpc SIsmember(SSetRequest) returns (SSetResponse); //集合中是否存在值 rpc SRem(SSetRequest) returns (SSetResponse); //集合中删除值 + rpc LLpush(LSetRequest) returns (LSetResponse); //列表头部增加值 + rpc LRpush(LSetRequest) returns (LSetResponse); //列表尾部增加值 + rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 } \ No newline at end of file From f8bb76a3bb454ab7d49011a4fbb03e7c6e0daaa1 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 15 Oct 2021 17:30:42 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8F=96=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E9=95=BF=E5=BA=A6=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 22 ++++++++++++++++++++++ redis.proto | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/redis.pb.go b/redis.pb.go index c7dbd17..f3a5c75 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -28,6 +28,7 @@ It has these top-level messages: LSetRequest LSetResponse LRangeRequest + LLenRequest */ package redisrpc @@ -455,6 +456,23 @@ func (m *LRangeRequest) GetStop() int64 { return 0 } +// 列表长度 +type LLenRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *LLenRequest) Reset() { *m = LLenRequest{} } +func (m *LLenRequest) String() string { return proto.CompactTextString(m) } +func (*LLenRequest) ProtoMessage() {} + +func (m *LLenRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + func init() { } @@ -476,6 +494,7 @@ type RedisService interface { LLpush(in *LSetRequest, out *LSetResponse) error LRpush(in *LSetRequest, out *LSetResponse) error LRange(in *LRangeRequest, out *HGetListResponse) error + LLen(in *LLenRequest, out *LSetResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -598,6 +617,9 @@ func (c *RedisServiceClient) LRpush(in *LSetRequest, out *LSetResponse) error { func (c *RedisServiceClient) LRange(in *LRangeRequest, out *HGetListResponse) error { return c.Call("RedisService.LRange", in, out) } +func (c *RedisServiceClient) LLen(in *LLenRequest, out *LSetResponse) error { + return c.Call("RedisService.LLen", 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 ff60703..75ec848 100644 --- a/redis.proto +++ b/redis.proto @@ -98,6 +98,10 @@ message LRangeRequest{ int64 start = 2; int64 stop = 3; } +//列表长度 +message LLenRequest{ + string key = 1; +} // rpc方法 service RedisService { @@ -118,4 +122,5 @@ service RedisService { rpc LLpush(LSetRequest) returns (LSetResponse); //列表头部增加值 rpc LRpush(LSetRequest) returns (LSetResponse); //列表尾部增加值 rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 + rpc LLen(LLenRequest) returns (LSetResponse); //列表长度 } \ No newline at end of file From 7b64db726880e50bbf41b011bfed4d4cb44bbcb6 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 15 Oct 2021 17:52:41 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=85=AC=E7=94=A8=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hash.go | 37 ++++++++++++++++- hash_test.go | 2 +- list.go | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++ list_test.go | 18 +++++++++ 4 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 list.go create mode 100644 list_test.go diff --git a/hash.go b/hash.go index 04126cb..461b314 100644 --- a/hash.go +++ b/hash.go @@ -114,10 +114,45 @@ func HGetAll(key string, url ...string) ([]map[string]string, error) { return list, nil } +/** + * 全部 + */ +func HGetList(key string, url ...string) ([]string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return []string{}, err + } + defer conn.Close() + + req := &GetRequest{proto.String(key), nil} + + res := &HGetListResponse{} + + err = conn.HGetList(req, res) + + if err != nil { + return []string{}, err + } + + value := res.GetList() + + var list []string + + err = json.Unmarshal(value, &list) + + if err != nil { + log.Println("json unmarshal error:", err) + return []string{}, err + } + return list, nil +} + /** * hash键是否存在 */ -func HExists(key string, field string, url ...string)(int64,error){ +func HExists(key string, field string, url ...string) (int64, error) { conn, _, err := Conn(url...) if err != nil { diff --git a/hash_test.go b/hash_test.go index b95d5da..1dbae09 100644 --- a/hash_test.go +++ b/hash_test.go @@ -50,6 +50,6 @@ func Test_HSet(t *testing.T) { // } // t.Log(err) - ret, err := HGetAll("testing2") + ret, err := HGetList("test2") t.Log(ret, err) } diff --git a/list.go b/list.go new file mode 100644 index 0000000..f49e6b6 --- /dev/null +++ b/list.go @@ -0,0 +1,112 @@ +package redisrpc + +import ( + "encoding/json" + "log" + + "github.com/golang/protobuf/proto" +) + +//头部增加 +func LLpush(key, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &LSetRequest{proto.String(key), proto.String(field), nil} + + res := &LSetResponse{} + + err = conn.LLpush(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +//尾部增加 +func LRpush(key, field string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &LSetRequest{proto.String(key), proto.String(field), nil} + + res := &LSetResponse{} + + err = conn.LRpush(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} + +/** + * 全部 + */ +func LRange(key string, start, stop int64, url ...string) ([]string, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return []string{}, err + } + defer conn.Close() + + req := &LRangeRequest{proto.String(key), proto.Int64(start), proto.Int64(stop), nil} + + res := &HGetListResponse{} + + err = conn.LRange(req, res) + + if err != nil { + return []string{}, err + } + + value := res.GetList() + + var list []string + + err = json.Unmarshal(value, &list) + + if err != nil { + log.Println("json unmarshal error:", err) + return []string{}, err + } + return list, nil +} + +//长度 +func LLen(key string, url ...string) (int64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &LLenRequest{proto.String(key), nil} + + res := &LSetResponse{} + + err = conn.LLen(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} diff --git a/list_test.go b/list_test.go new file mode 100644 index 0000000..df54e89 --- /dev/null +++ b/list_test.go @@ -0,0 +1,18 @@ +package redisrpc + +import ( + // "strconv" + "testing" + // "tgo/helper" +) + +func Test_LLpush(t *testing.T) { + + // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} + + reply, err := LLen("testing") + + t.Log(reply) + t.Log(err) + +} From 6a2bc7a0f851eafdceabe7ba0a12a3a50dc3363c Mon Sep 17 00:00:00 2001 From: guzeng Date: Mon, 8 Nov 2021 16:51:36 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=89=A3=E5=87=8F?= =?UTF-8?q?=E5=BA=93=E5=AD=98=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- redis.pb.go | 4 ++++ redis.proto | 1 + 2 files changed, 5 insertions(+) diff --git a/redis.pb.go b/redis.pb.go index f3a5c75..a7dcc9f 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -495,6 +495,7 @@ type RedisService interface { LRpush(in *LSetRequest, out *LSetResponse) error LRange(in *LRangeRequest, out *HGetListResponse) error LLen(in *LLenRequest, out *LSetResponse) error + ReduceStock(in *SetRequest, out *SetResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -620,6 +621,9 @@ func (c *RedisServiceClient) LRange(in *LRangeRequest, out *HGetListResponse) er func (c *RedisServiceClient) LLen(in *LLenRequest, out *LSetResponse) error { return c.Call("RedisService.LLen", in, out) } +func (c *RedisServiceClient) ReduceStock(in *SetRequest, out *SetResponse) error { + return c.Call("RedisService.ReduceStock", 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 75ec848..059e871 100644 --- a/redis.proto +++ b/redis.proto @@ -123,4 +123,5 @@ service RedisService { rpc LRpush(LSetRequest) returns (LSetResponse); //列表尾部增加值 rpc LRange(LRangeRequest) returns (HGetListResponse); //列表尾部增加值 rpc LLen(LLenRequest) returns (LSetResponse); //列表长度 + rpc ReduceStock(SetRequest) returns(SetResponse);//扣减库存 } \ No newline at end of file