Browse Source

增加设置有效期方法

master
guzeng 3 years ago
parent
commit
b7e2bd99ba
5 changed files with 115 additions and 1 deletions
  1. +30
    -0
      expire.go
  2. +17
    -0
      expire_test.go
  3. +9
    -1
      hash_test.go
  4. +48
    -0
      redis.pb.go
  5. +11
    -0
      redis.proto

+ 30
- 0
expire.go View File

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

+ 17
- 0
expire_test.go View File

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

+ 9
- 1
hash_test.go View File

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


+ 48
- 0
redis.pb.go View File

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


+ 11
- 0
redis.proto View File

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

Loading…
Cancel
Save