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/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) + +} diff --git a/redis.pb.go b/redis.pb.go index 9e4279e..a7dcc9f 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -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) { diff --git a/redis.proto b/redis.proto index 7f0894c..3bff948 100644 --- a/redis.proto +++ b/redis.proto @@ -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);//扣减库存 } \ No newline at end of file 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) + +}