Browse Source

commit

master
listen 2 years ago
parent
commit
7791674df3
11 changed files with 512 additions and 4 deletions
  1. +6
    -1
      conn.go
  2. +1
    -1
      go.mod
  3. +2
    -0
      go.sum
  4. +36
    -1
      hash.go
  5. +1
    -1
      hash_test.go
  6. +112
    -0
      list.go
  7. +18
    -0
      list_test.go
  8. +194
    -0
      redis.pb.go
  9. +45
    -0
      redis.proto
  10. +76
    -0
      set.go
  11. +21
    -0
      set_test.go

+ 6
- 1
conn.go View File

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

+ 1
- 1
go.mod View File

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

+ 2
- 0
go.sum View File

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


+ 36
- 1
hash.go View File

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


+ 1
- 1
hash_test.go View File

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

+ 112
- 0
list.go View File

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

+ 18
- 0
list_test.go View File

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

+ 194
- 0
redis.pb.go View File

@ -22,6 +22,13 @@ It has these top-level messages:
DelResponse
HSetResponse
SetExpireResponse
SSetRequest
SMembersRequest
SSetResponse
LSetRequest
LSetResponse
LRangeRequest
LLenRequest
*/
package redisrpc
@ -315,6 +322,157 @@ func (m *SetExpireResponse) GetRet() int64 {
return 0
}
// 集合添加值
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 *SSetRequest) Reset() { *m = SSetRequest{} }
func (m *SSetRequest) String() string { return proto.CompactTextString(m) }
func (*SSetRequest) ProtoMessage() {}
func (m *SSetRequest) GetKey() string {
if m != nil && m.Key != nil {
return *m.Key
}
return ""
}
func (m *SSetRequest) 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 ""
}
// 设置响应结构
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
}
// 列表添加值
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
}
// 列表长度
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() {
}
@ -327,8 +485,17 @@ 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
LLen(in *LLenRequest, out *LSetResponse) error
ReduceStock(in *SetRequest, out *SetResponse) error
}
// AcceptRedisServiceClient accepts connections on the listener and serves requests
@ -424,12 +591,39 @@ 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)
}
func (c *RedisServiceClient) Exists(in *GetRequest, out *DelResponse) error {
return c.Call("RedisService.Exists", in, out)
}
func (c *RedisServiceClient) SAdd(in *SSetRequest, out *SSetResponse) error {
return c.Call("RedisService.SAdd", in, out)
}
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)
}
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)
}
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) {


+ 45
- 0
redis.proto View File

@ -67,6 +67,42 @@ message SetExpireResponse {
int64 ret = 1;
}
//
message SSetRequest{
string key = 1;
string field = 2;
}
//
message SMembersRequest{
string key = 1;
}
//
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;
}
//
message LLenRequest{
string key = 1;
}
// rpc方法
service RedisService {
rpc Get (GetRequest) returns (GetStringResponse); // 使key查询
@ -77,7 +113,16 @@ 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 HIncrby(GetRequest) returns (DelResponse); //hash数值+-1
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); //
rpc LLen(LLenRequest) returns (LSetResponse); //
rpc ReduceStock(SetRequest) returns(SetResponse);//
}

+ 76
- 0
set.go View File

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

+ 21
- 0
set_test.go View File

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

Loading…
Cancel
Save