redis rpc服务, 提供redis操作方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
1.7 KiB

  1. package redisrpc
  2. import (
  3. "encoding/json"
  4. "log"
  5. "github.com/golang/protobuf/proto"
  6. )
  7. /**
  8. * 使用用户名查询
  9. */
  10. func HGetString(key, field string, url ...string) (string, error) {
  11. conn, _, err := Conn(url...)
  12. if err != nil {
  13. return "", err
  14. }
  15. defer conn.Close()
  16. req := &HGetRequest{proto.String(key), proto.String(field), nil}
  17. res := &GetStringResponse{}
  18. err = conn.HGet(req, res)
  19. if err != nil {
  20. return "", err
  21. }
  22. return res.GetValue(), nil
  23. }
  24. //设置
  25. func HSet(key, field, value string, url ...string) (int64, error) {
  26. conn, _, err := Conn(url...)
  27. if err != nil {
  28. return 0, err
  29. }
  30. defer conn.Close()
  31. req := &HSetRequest{proto.String(key), proto.String(field), proto.String(value), nil}
  32. res := &HSetResponse{}
  33. err = conn.HSet(req, res)
  34. if err != nil {
  35. return 0, err
  36. }
  37. return res.GetRet(), nil
  38. }
  39. //删除
  40. func HDel(key string, field string, url ...string) (int64, error) {
  41. conn, _, err := Conn(url...)
  42. if err != nil {
  43. return 0, err
  44. }
  45. defer conn.Close()
  46. req := &HDelRequest{proto.String(key), proto.String(field), nil}
  47. res := &DelResponse{}
  48. err = conn.HDel(req, res)
  49. if err != nil {
  50. return 0, err
  51. }
  52. return res.GetRet(), nil
  53. }
  54. /**
  55. * 全部
  56. */
  57. func HGetAll(key string, url ...string) ([]map[string]string, error) {
  58. conn, _, err := Conn(url...)
  59. if err != nil {
  60. return []map[string]string{}, err
  61. }
  62. defer conn.Close()
  63. req := &GetRequest{proto.String(key), nil}
  64. res := &HGetListResponse{}
  65. err = conn.HGetAll(req, res)
  66. if err != nil {
  67. return []map[string]string{}, err
  68. }
  69. value := res.GetList()
  70. var list []map[string]string
  71. err = json.Unmarshal(value, &list)
  72. if err != nil {
  73. log.Println("json unmarshal error:", err)
  74. return []map[string]string{}, err
  75. }
  76. return list, nil
  77. }