Browse Source

设置hset修改

master v0.1.12
guzeng 3 years ago
parent
commit
06d399b827
6 changed files with 82 additions and 16 deletions
  1. +48
    -6
      hash.go
  2. +4
    -4
      hash_test.go
  3. +18
    -0
      redis.pb.go
  4. +6
    -0
      redis.proto
  5. +1
    -1
      string.go
  6. +5
    -5
      string_test.go

+ 48
- 6
hash.go View File

@ -1,7 +1,6 @@
package redisrpc package redisrpc
import ( import (
"git.tetele.net/tgo/conf"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
) )
@ -10,15 +9,12 @@ import (
*/ */
func HGetString(key, field string, url ...string) (string, error) { func HGetString(key, field string, url ...string) (string, error) {
var rpc_url string = "127.0.0.1:" + conf.REDIS_RPC_PORT
if len(url) > 0 && url[0] != "" {
rpc_url = url[0]
}
conn, _, err := Conn(url...)
conn, _, err := DialRedisService("tcp", rpc_url)
if err != nil { if err != nil {
return "", err return "", err
} }
defer conn.Close() defer conn.Close()
req := &HGetRequest{proto.String(key), proto.String(field), nil} req := &HGetRequest{proto.String(key), proto.String(field), nil}
@ -33,3 +29,49 @@ func HGetString(key, field string, url ...string) (string, error) {
return res.GetValue(), nil return res.GetValue(), nil
} }
//设置
func HSet(key, field, value string, url ...string) (uint64, error) {
conn, _, err := Conn(url...)
if err != nil {
return "", err
}
defer conn.Close()
req := &HSetRequest{proto.String(key), proto.String(field), proto.String(value), nil}
res := &HSetResponse{}
err = conn.HSet(req, res)
if err != nil {
return "", err
}
return res.GetRet(), nil
}
//删除
func HDel(key string, field string, url ...string) (uint64, error) {
conn, _, err := Conn(url...)
if err != nil {
return 0, err
}
defer conn.Close()
req := &HDelRequest{proto.String(key), proto.String(field), nil}
res := &DelResponse{}
err = conn.HDel(req, res)
if err != nil {
return 0, err
}
return res.GetRet(), nil
}

+ 4
- 4
hash_test.go View File

@ -10,12 +10,12 @@ func Test_HSet(t *testing.T) {
// val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"}
// reply, err := HSet("testing2", "test1", val)
reply, err := HSet("testing2", "test1", "val")
// t.Log(reply)
// t.Log(err)
t.Log(reply)
t.Log(err)
ret, err := GetByHashKey("testing", "test")
ret, err := HGetString("testing", "test")
t.Log(ret) t.Log(ret)
if err == nil { if err == nil {


+ 18
- 0
redis.pb.go View File

@ -18,6 +18,7 @@ It has these top-level messages:
GetStringResponse GetStringResponse
SetResponse SetResponse
DelResponse DelResponse
HSetResponse
*/ */
package redisrpc package redisrpc
@ -235,6 +236,23 @@ func (m *DelResponse) GetRet() uint64 {
return 0 return 0
} }
// 设置key响应结构
type HSetResponse struct {
Ret *uint64 `protobuf:"varint,1,opt,name=ret" json:"ret,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *HSetResponse) Reset() { *m = HSetResponse{} }
func (m *HSetResponse) String() string { return proto.CompactTextString(m) }
func (*HSetResponse) ProtoMessage() {}
func (m *HSetResponse) GetRet() uint64 {
if m != nil && m.Ret != nil {
return *m.Ret
}
return 0
}
func init() { func init() {
} }


+ 6
- 0
redis.proto View File

@ -47,6 +47,12 @@ message SetResponse {
message DelResponse { message DelResponse {
uint64 ret = 1; uint64 ret = 1;
} }
// key响应结构
message HSetResponse {
uint64 ret = 1;
}
// rpc方法 // rpc方法
service RedisService { service RedisService {
rpc Get (GetRequest) returns (GetStringResponse); // 使key查询 rpc Get (GetRequest) returns (GetStringResponse); // 使key查询


+ 1
- 1
string.go View File

@ -52,7 +52,7 @@ func Set(key, value string, ttl string, url ...string) (string, error) {
return res.GetRet(), nil return res.GetRet(), nil
} }
//设置
//删除
func Del(key string, url ...string) (uint64, error) { func Del(key string, url ...string) (uint64, error) {
conn, _, err := Conn(url...) conn, _, err := Conn(url...)


+ 5
- 5
string_test.go View File

@ -5,13 +5,13 @@ import (
// "time" // "time"
) )
// func Test_Get(t *testing.T) {
func Test_Get(t *testing.T) {
// reply, err := GetString("siteListCache")
reply, err := GetString("siteListCache")
// t.Log(reply)
// t.Log(err)
// }
t.Log(reply)
t.Log(err)
}
func Test_Set(t *testing.T) { func Test_Set(t *testing.T) {


Loading…
Cancel
Save