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.

196 lines
3.0 KiB

3 years ago
  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. }
  78. /**
  79. * 全部
  80. */
  81. func HGetList(key string, url ...string) ([]string, error) {
  82. conn, _, err := Conn(url...)
  83. if err != nil {
  84. return []string{}, err
  85. }
  86. defer conn.Close()
  87. req := &GetRequest{proto.String(key), nil}
  88. res := &HGetListResponse{}
  89. err = conn.HGetList(req, res)
  90. if err != nil {
  91. return []string{}, err
  92. }
  93. value := res.GetList()
  94. var list []string
  95. err = json.Unmarshal(value, &list)
  96. if err != nil {
  97. log.Println("json unmarshal error:", err)
  98. return []string{}, err
  99. }
  100. return list, nil
  101. }
  102. /**
  103. * hash键是否存在
  104. */
  105. func HExists(key string, field string, url ...string) (int64, error) {
  106. conn, _, err := Conn(url...)
  107. if err != nil {
  108. return 0, err
  109. }
  110. defer conn.Close()
  111. req := &HGetRequest{proto.String(key), proto.String(field), nil}
  112. res := &DelResponse{}
  113. err = conn.HExists(req, res)
  114. if err != nil {
  115. return 0, err
  116. }
  117. return res.GetRet(), nil
  118. }
  119. func HIncrby(key, field string,value int64, url ...string) (int64, error) {
  120. conn, _, err := Conn(url...)
  121. if err != nil {
  122. return 0, err
  123. }
  124. defer conn.Close()
  125. req := &HSetRequest{proto.String(key), proto.String(field), proto.String(string(value)), nil}
  126. res := &HSetResponse{}
  127. err = conn.HIncrby(req, res)
  128. if err != nil {
  129. return 0, err
  130. }
  131. return res.GetRet(), nil
  132. }