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.

329 lines
5.1 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
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. //hash取所有值
  88. func HGetAllItem(key string) (map[string]string, error) {
  89. c := GetConn()
  90. ret, err := c.Do("HGETAll", key)
  91. reply := make([][]byte, 0)
  92. if err == nil {
  93. reply, err = redisdb.ByteSlices(ret, err)
  94. }
  95. var info map[string]string = make(map[string]string)
  96. if len(reply) > 0 {
  97. for key, item := range reply {
  98. if key%2 == 0 { //只处理奇数位
  99. info[string(item)] = string(reply[key+1])
  100. }
  101. }
  102. }
  103. CloseConn(c)
  104. return info, err
  105. }
  106. /*
  107. * hash存值,
  108. * key
  109. * field
  110. * value
  111. * 2020/06/06
  112. */
  113. func HSet(key string, field, value interface{}) (int64, error) {
  114. c := GetConn()
  115. reply, err := c.Do("HSET", key, field, value)
  116. CloseConn(c)
  117. if err != nil {
  118. return 0, err
  119. } else {
  120. return redisdb.Int64(reply, nil)
  121. }
  122. }
  123. /*
  124. * hash存值,
  125. * key
  126. * field
  127. * value
  128. * 2020/06/06
  129. */
  130. func HIncrby(key string, field, value interface{}) (int64, error) {
  131. c := GetConn()
  132. reply, err := c.Do("hincrby", key, field, value)
  133. CloseConn(c)
  134. if err != nil {
  135. return 0, err
  136. } else {
  137. return redisdb.Int64(reply, nil)
  138. }
  139. }
  140. /*
  141. * 删除hash值
  142. */
  143. func HDel(key, field string) (int64, error) {
  144. c := GetConn()
  145. reply, err := c.Do("HDEL", key, field)
  146. CloseConn(c)
  147. if err != nil {
  148. return 0, err
  149. } else {
  150. return redisdb.Int64(reply, nil)
  151. }
  152. }
  153. /*
  154. * hash批量存值,
  155. * args (key,field,value,field,value,field,value...) ,,, ...
  156. * 2020/06/06
  157. */
  158. func HMSet(args ...interface{}) (interface{}, error) {
  159. c := GetConn()
  160. reply, err := c.Do("HMSET", args...)
  161. CloseConn(c)
  162. return reply, err
  163. }
  164. /*
  165. * hash批量查询,
  166. * args (key,field,field,field...) , ...
  167. * 2020/06/06
  168. */
  169. func HMGet(args ...interface{}) (interface{}, error) {
  170. c := GetConn()
  171. reply, err := c.Do("HMGET", args...)
  172. CloseConn(c)
  173. return reply, err
  174. }
  175. /*
  176. * hash批量查询,
  177. * args (key,field,field,field...) , ...
  178. * 2020/06/06
  179. */
  180. func HMGetString(args ...interface{}) ([]string, error) {
  181. c := GetConn()
  182. reply, err := c.Do("HMGET", args...)
  183. CloseConn(c)
  184. list := make([]string, 0)
  185. if err == nil {
  186. list, err = redisdb.Strings(reply, err)
  187. }
  188. return list, err
  189. }
  190. /*
  191. * hash存值,
  192. * key
  193. * expire 过期时长
  194. * 2020/06/06
  195. */
  196. func HSetExpire(key string, expire int) (interface{}, error) {
  197. c := GetConn()
  198. reply, err := c.Do("expire", key, expire)
  199. CloseConn(c)
  200. return reply, err
  201. }
  202. /**
  203. * hash键是否存在
  204. * key
  205. * field
  206. */
  207. func HExists(key, field string) (int64, error) {
  208. c := GetConn()
  209. reply, err := c.Do("HEXISTS", key, field)
  210. CloseConn(c)
  211. if err != nil {
  212. return 0, err
  213. } else {
  214. return redisdb.Int64(reply, nil)
  215. }
  216. }
  217. /**
  218. * hash 键名
  219. */
  220. func HKeys(key string) ([]string, error) {
  221. c := GetConn()
  222. ret, err := c.Do("HKEYS", key)
  223. reply := make([]string, 0)
  224. if err == nil {
  225. reply, err = redisdb.Strings(ret, err)
  226. }
  227. CloseConn(c)
  228. return reply, err
  229. }
  230. /**
  231. * hash 键值
  232. */
  233. func HVals(key string) ([]string, error) {
  234. c := GetConn()
  235. ret, err := c.Do("HVALS", key)
  236. reply := make([]string, 0)
  237. if err == nil {
  238. reply, err = redisdb.Strings(ret, err)
  239. }
  240. CloseConn(c)
  241. return reply, err
  242. }