Browse Source

增加列表公用方法

master v0.5.2
guzeng 2 years ago
parent
commit
7b64db7268
4 changed files with 167 additions and 2 deletions
  1. +36
    -1
      hash.go
  2. +1
    -1
      hash_test.go
  3. +112
    -0
      list.go
  4. +18
    -0
      list_test.go

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

Loading…
Cancel
Save