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.

231 lines
3.6 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package redis
  2. import (
  3. redisdb "github.com/gomodule/redigo/redis"
  4. )
  5. //hash取值, 返回interface{}
  6. func HGet(key string, field interface{}) (interface{}, error) {
  7. c := GetConn()
  8. reply, err := c.Do("HGET", key, field)
  9. CloseConn(c)
  10. return reply, err
  11. }
  12. //hash取值, 返回字符串
  13. func HGetString(key string, field interface{}) (string, error) {
  14. c := GetConn()
  15. ret, err := c.Do("HGET", key, field)
  16. reply := ""
  17. if err == nil {
  18. reply, err = redisdb.String(ret, err)
  19. }
  20. CloseConn(c)
  21. return reply, err
  22. }
  23. //hash取值,返回接口类型
  24. func HGetStringMap(key string, field interface{}) (map[string]string, error) {
  25. c := GetConn()
  26. ret, err := c.Do("HGET", key, field)
  27. reply := make(map[string]string)
  28. if err == nil {
  29. reply, err = redisdb.StringMap(ret, err)
  30. }
  31. CloseConn(c)
  32. return reply, err
  33. }
  34. //hash取值,返回[]byte
  35. func HGetBytes(key string, field interface{}) ([]byte, error) {
  36. c := GetConn()
  37. ret, err := c.Do("HGET", key, field)
  38. reply := make([]byte, 0)
  39. if err == nil {
  40. reply, err = redisdb.Bytes(ret, err)
  41. }
  42. CloseConn(c)
  43. return reply, err
  44. }
  45. //hash取所有值
  46. func HGetAll(key string) ([]map[string]string, error) {
  47. c := GetConn()
  48. ret, err := c.Do("HGETAll", key)
  49. reply := make([][]byte, 0)
  50. if err == nil {
  51. reply, err = redisdb.ByteSlices(ret, err)
  52. }
  53. var info map[string]string
  54. var all []map[string]string
  55. if len(reply) > 0 {
  56. for key, item := range reply {
  57. if key%2 == 0 { //只处理奇数位
  58. info = make(map[string]string)
  59. info[string(item)] = string(reply[key+1])
  60. all = append(all, info)
  61. }
  62. }
  63. }
  64. CloseConn(c)
  65. return all, err
  66. }
  67. //hash取所有值
  68. func HGetList(key string) ([]string, error) {
  69. c := GetConn()
  70. ret, err := c.Do("HGETAll", key)
  71. reply := make([][]byte, 0)
  72. if err == nil {
  73. reply, err = redisdb.ByteSlices(ret, err)
  74. }
  75. // var info map[string]string
  76. var all []string
  77. if len(reply) > 0 {
  78. for key, item := range reply {
  79. if (key+1)%2 == 0 { //只处理偶数位
  80. all = append(all, string(item))
  81. }
  82. }
  83. }
  84. CloseConn(c)
  85. return all, err
  86. }
  87. /*
  88. * hash存值,
  89. * key
  90. * field
  91. * value
  92. * 2020/06/06
  93. */
  94. func HSet(key string, field, value interface{}) (int64, error) {
  95. c := GetConn()
  96. reply, err := c.Do("HSET", key, field, value)
  97. CloseConn(c)
  98. if err != nil {
  99. return 0, err
  100. } else {
  101. return redisdb.Int64(reply, nil)
  102. }
  103. }
  104. /*
  105. * hash存值,
  106. * key
  107. * field
  108. * value
  109. * 2020/06/06
  110. */
  111. func HIncrby(key string, field, value interface{}) (int64, error) {
  112. c := GetConn()
  113. reply, err := c.Do("hincrby", key, field, value)
  114. CloseConn(c)
  115. if err != nil {
  116. return 0, err
  117. } else {
  118. return redisdb.Int64(reply, nil)
  119. }
  120. }
  121. /*
  122. * 删除hash值
  123. */
  124. func HDel(key, field string) (int64, error) {
  125. c := GetConn()
  126. reply, err := c.Do("HDEL", key, field)
  127. CloseConn(c)
  128. if err != nil {
  129. return 0, err
  130. } else {
  131. return redisdb.Int64(reply, nil)
  132. }
  133. }
  134. /*
  135. * hash批量存值,
  136. * args (key,field,value,field,value,field,value...) ,,, ...
  137. * 2020/06/06
  138. */
  139. func HMSet(args ...interface{}) (interface{}, error) {
  140. c := GetConn()
  141. reply, err := c.Do("HMSET", args...)
  142. CloseConn(c)
  143. return reply, err
  144. }
  145. /*
  146. * hash存值,
  147. * key
  148. * expire 过期时长
  149. * 2020/06/06
  150. */
  151. func HSetExpire(key string, expire int) (interface{}, error) {
  152. c := GetConn()
  153. reply, err := c.Do("expire", key, expire)
  154. CloseConn(c)
  155. return reply, err
  156. }
  157. /**
  158. * hash键是否存在
  159. * key
  160. * field
  161. */
  162. func HExists(key, field string) (int64, error) {
  163. c := GetConn()
  164. reply, err := c.Do("HEXISTS", key, field)
  165. CloseConn(c)
  166. if err != nil {
  167. return 0, err
  168. } else {
  169. return redisdb.Int64(reply, nil)
  170. }
  171. }