|
|
- package redisrpc
-
- import (
- "encoding/json"
- "log"
-
- "github.com/golang/protobuf/proto"
- )
-
- /**
- * 使用用户名查询
- */
- func HGetString(key, field string, url ...string) (string, error) {
-
- conn, _, err := Conn(url...)
-
- if err != nil {
- return "", err
- }
-
- defer conn.Close()
-
- req := &HGetRequest{proto.String(key), proto.String(field), nil}
-
- res := &GetStringResponse{}
-
- err = conn.HGet(req, res)
-
- if err != nil {
- return "", err
- }
-
- return res.GetValue(), nil
- }
-
- //设置
- func HSet(key, field, value string, url ...string) (int64, error) {
-
- conn, _, err := Conn(url...)
-
- if err != nil {
- return 0, 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 0, err
- }
-
- return res.GetRet(), nil
- }
-
- //删除
- func HDel(key string, field string, url ...string) (int64, 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
- }
-
- /**
- * 全部
- */
- func HGetAll(key string, url ...string) ([]map[string]string, error) {
-
- conn, _, err := Conn(url...)
-
- if err != nil {
- return []map[string]string{}, err
- }
- defer conn.Close()
-
- req := &GetRequest{proto.String(key), nil}
-
- res := &HGetListResponse{}
-
- err = conn.HGetAll(req, res)
-
- if err != nil {
- return []map[string]string{}, err
- }
-
- value := res.GetList()
-
- var list []map[string]string
-
- err = json.Unmarshal(value, &list)
-
- if err != nil {
- log.Println("json unmarshal error:", err)
- return []map[string]string{}, err
- }
- 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) {
- conn, _, err := Conn(url...)
-
- if err != nil {
- return 0, err
- }
- defer conn.Close()
-
- req := &HGetRequest{proto.String(key), proto.String(field), nil}
-
- res := &DelResponse{}
-
- err = conn.HExists(req, res)
-
- if err != nil {
- return 0, err
- }
-
- return res.GetRet(), nil
- }
|