From b7e2bd99ba6c9021fe0d04e6a27795ba4797e6c8 Mon Sep 17 00:00:00 2001 From: guzeng Date: Sun, 23 May 2021 15:39:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AE=E6=9C=89?= =?UTF-8?q?=E6=95=88=E6=9C=9F=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- expire.go | 30 ++++++++++++++++++++++++++++++ expire_test.go | 17 +++++++++++++++++ hash_test.go | 10 +++++++++- redis.pb.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ redis.proto | 11 +++++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 expire.go create mode 100644 expire_test.go diff --git a/expire.go b/expire.go new file mode 100644 index 0000000..a11492f --- /dev/null +++ b/expire.go @@ -0,0 +1,30 @@ +package redisrpc + +import ( + "github.com/golang/protobuf/proto" +) + +/** + * 使用用户名查询 + */ +func SetExpire(key string, ttl uint64, url ...string) (uint64, error) { + + conn, _, err := Conn(url...) + + if err != nil { + return 0, err + } + defer conn.Close() + + req := &SetExpire(proto.String(key),proto.Uint64(ttl), nil} + + res := &SetExpireResponse{} + + err = conn.SetExpire(req, res) + + if err != nil { + return 0, err + } + + return res.GetRet(), nil +} diff --git a/expire_test.go b/expire_test.go new file mode 100644 index 0000000..14ce42a --- /dev/null +++ b/expire_test.go @@ -0,0 +1,17 @@ +package redisrpc + +import ( + "testing" + // "time" +) + +func Test_SetExpire(t *testing.T) { + + c, err := Set("test", "33") + t.Log(c) + t.Log(err) + c, err = SetExpire("test", 30) + t.Log(c) + t.Log(err) + +} diff --git a/hash_test.go b/hash_test.go index f537215..ad5adec 100644 --- a/hash_test.go +++ b/hash_test.go @@ -10,7 +10,7 @@ func Test_HSet(t *testing.T) { // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} - reply, err := HSet("testing2", "test1", "val") + reply, err := HSet("testing", "test", "test1") t.Log(reply) t.Log(err) @@ -24,6 +24,14 @@ func Test_HSet(t *testing.T) { t.Log("error:", err) } + del_ret, err := HDel("testing", "test") + + t.Log(del_ret) + if err == nil { + t.Log("nil", err) + } else { + t.Log("error:", err) + } // ret2, err := HSetExpire("testing2", 200) // t.Log(ret2) // t.Log(err) diff --git a/redis.pb.go b/redis.pb.go index f000dcf..7446ab3 100644 --- a/redis.pb.go +++ b/redis.pb.go @@ -15,10 +15,12 @@ It has these top-level messages: HGetRequest HSetRequest HDelRequest + SetExpireRequest GetStringResponse SetResponse DelResponse HSetResponse + SetExpireResponse */ package redisrpc @@ -185,6 +187,31 @@ func (m *HDelRequest) GetField() string { return "" } +// 有效期 +type SetExpireRequest struct { + Key *string `protobuf:"bytes,1,opt,name=key" json:"key,omitempty"` + Expire *uint64 `protobuf:"varint,2,opt,name=expire" json:"expire,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SetExpireRequest) Reset() { *m = SetExpireRequest{} } +func (m *SetExpireRequest) String() string { return proto.CompactTextString(m) } +func (*SetExpireRequest) ProtoMessage() {} + +func (m *SetExpireRequest) GetKey() string { + if m != nil && m.Key != nil { + return *m.Key + } + return "" +} + +func (m *SetExpireRequest) GetExpire() uint64 { + if m != nil && m.Expire != nil { + return *m.Expire + } + return 0 +} + // 使用key查询响应结构 type GetStringResponse struct { Value *string `protobuf:"bytes,1,opt,name=value" json:"value,omitempty"` @@ -253,6 +280,23 @@ func (m *HSetResponse) GetRet() uint64 { return 0 } +// 设置key有效期 +type SetExpireResponse struct { + Ret *uint64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"` + XXX_unrecognized []byte `json:"-"` +} + +func (m *SetExpireResponse) Reset() { *m = SetExpireResponse{} } +func (m *SetExpireResponse) String() string { return proto.CompactTextString(m) } +func (*SetExpireResponse) ProtoMessage() {} + +func (m *SetExpireResponse) GetRet() uint64 { + if m != nil && m.Ret != nil { + return *m.Ret + } + return 0 +} + func init() { } @@ -263,6 +307,7 @@ type RedisService interface { HGet(in *HGetRequest, out *GetStringResponse) error HSet(in *HSetRequest, out *HSetResponse) error HDel(in *HDelRequest, out *DelResponse) error + SetExpire(in *SetExpireRequest, out *SetExpireResponse) error } // AcceptRedisServiceClient accepts connections on the listener and serves requests @@ -352,6 +397,9 @@ func (c *RedisServiceClient) HSet(in *HSetRequest, out *HSetResponse) error { func (c *RedisServiceClient) HDel(in *HDelRequest, out *DelResponse) error { return c.Call("RedisService.HDel", in, out) } +func (c *RedisServiceClient) SetExpire(in *SetExpireRequest, out *SetExpireResponse) error { + return c.Call("RedisService.SetExpire", 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 fe46875..aa0a5e2 100644 --- a/redis.proto +++ b/redis.proto @@ -33,6 +33,12 @@ message HDelRequest { string field = 2; } +//有效期 +message SetExpireRequest { + string key = 1; + uint64 expire = 2; +} + // 使用key查询响应结构 message GetStringResponse { string value = 1; @@ -52,6 +58,10 @@ message DelResponse { message HSetResponse { uint64 ret = 1; } +// 设置key有效期 +message SetExpireResponse { + uint64 ret = 1; +} // rpc方法 service RedisService { @@ -61,4 +71,5 @@ service RedisService { rpc HGet (HGetRequest) returns (GetStringResponse); // 使用hash key查询 rpc HSet (HSetRequest) returns (HSetResponse); rpc HDel (HDelRequest) returns (DelResponse); + rpc SetExpire (SetExpireRequest) returns (SetExpireResponse); //设置有效期 } \ No newline at end of file