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.

197 lines
3.0 KiB

3 years ago
3 years ago
3 years ago
  1. package redisrpc
  2. import (
  3. "encoding/json"
  4. "log"
  5. "strconv"
  6. "github.com/golang/protobuf/proto"
  7. )
  8. /**
  9. * 使用用户名查询
  10. */
  11. func HGetString(key, field string, url ...string) (string, error) {
  12. conn, _, err := Conn(url...)
  13. if err != nil {
  14. return "", err
  15. }
  16. defer conn.Close()
  17. req := &HGetRequest{proto.String(key), proto.String(field), nil}
  18. res := &GetStringResponse{}
  19. err = conn.HGet(req, res)
  20. if err != nil {
  21. return "", err
  22. }
  23. return res.GetValue(), nil
  24. }
  25. //设置
  26. func HSet(key, field, value string, url ...string) (int64, error) {
  27. conn, _, err := Conn(url...)
  28. if err != nil {
  29. return 0, err
  30. }
  31. defer conn.Close()
  32. req := &HSetRequest{proto.String(key), proto.String(field), proto.String(value), nil}
  33. res := &HSetResponse{}
  34. err = conn.HSet(req, res)
  35. if err != nil {
  36. return 0, err
  37. }
  38. return res.GetRet(), nil
  39. }
  40. //删除
  41. func HDel(key string, field string, url ...string) (int64, error) {
  42. conn, _, err := Conn(url...)
  43. if err != nil {
  44. return 0, err
  45. }
  46. defer conn.Close()
  47. req := &HDelRequest{proto.String(key), proto.String(field), nil}
  48. res := &DelResponse{}
  49. err = conn.HDel(req, res)
  50. if err != nil {
  51. return 0, err
  52. }
  53. return res.GetRet(), nil
  54. }
  55. /**
  56. * 全部
  57. */
  58. func HGetAll(key string, url ...string) ([]map[string]string, error) {
  59. conn, _, err := Conn(url...)
  60. if err != nil {
  61. return []map[string]string{}, err
  62. }
  63. defer conn.Close()
  64. req := &GetRequest{proto.String(key), nil}
  65. res := &HGetListResponse{}
  66. err = conn.HGetAll(req, res)
  67. if err != nil {
  68. return []map[string]string{}, err
  69. }
  70. value := res.GetList()
  71. var list []map[string]string
  72. err = json.Unmarshal(value, &list)
  73. if err != nil {
  74. log.Println("json unmarshal error:", err)
  75. return []map[string]string{}, err
  76. }
  77. return list, nil
  78. }
  79. /**
  80. * 全部
  81. */
  82. func HGetList(key string, url ...string) ([]string, error) {
  83. conn, _, err := Conn(url...)
  84. if err != nil {
  85. return []string{}, err
  86. }
  87. defer conn.Close()
  88. req := &GetRequest{proto.String(key), nil}
  89. res := &HGetListResponse{}
  90. err = conn.HGetList(req, res)
  91. if err != nil {
  92. return []string{}, err
  93. }
  94. value := res.GetList()
  95. var list []string
  96. err = json.Unmarshal(value, &list)
  97. if err != nil {
  98. log.Println("json unmarshal error:", err)
  99. return []string{}, err
  100. }
  101. return list, nil
  102. }
  103. /**
  104. * hash键是否存在
  105. */
  106. func HExists(key string, field string, url ...string) (int64, error) {
  107. conn, _, err := Conn(url...)
  108. if err != nil {
  109. return 0, err
  110. }
  111. defer conn.Close()
  112. req := &HGetRequest{proto.String(key), proto.String(field), nil}
  113. res := &DelResponse{}
  114. err = conn.HExists(req, res)
  115. if err != nil {
  116. return 0, err
  117. }
  118. return res.GetRet(), nil
  119. }
  120. func HIncrby(key, field string,value int, url ...string) (int64, error) {
  121. conn, _, err := Conn(url...)
  122. if err != nil {
  123. return 0, err
  124. }
  125. defer conn.Close()
  126. req := &HSetRequest{proto.String(key), proto.String(field), proto.String(strconv.Itoa(value)), nil}
  127. res := &HSetResponse{}
  128. err = conn.HIncrby(req, res)
  129. if err != nil {
  130. return 0, err
  131. }
  132. return res.GetRet(), nil
  133. }