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.

201 lines
3.2 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
  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. /*
  68. * hash存值,
  69. * key
  70. * field
  71. * value
  72. * 2020/06/06
  73. */
  74. func HSet(key string, field, value interface{}) (int64, error) {
  75. c := GetConn()
  76. reply, err := c.Do("HSET", key, field, value)
  77. CloseConn(c)
  78. if err != nil {
  79. return 0, err
  80. } else {
  81. return redisdb.Int64(reply, nil)
  82. }
  83. }
  84. /*
  85. * hash存值,
  86. * key
  87. * field
  88. * value
  89. * 2020/06/06
  90. */
  91. func HIncrby(key string, field, value interface{}) (int64, error) {
  92. c := GetConn()
  93. reply, err := c.Do("hincrby", key, field, value)
  94. CloseConn(c)
  95. if err != nil {
  96. return 0, err
  97. } else {
  98. return redisdb.Int64(reply, nil)
  99. }
  100. }
  101. /*
  102. * 删除hash值
  103. */
  104. func HDel(key, field string) (int64, error) {
  105. c := GetConn()
  106. reply, err := c.Do("HDEL", key, field)
  107. CloseConn(c)
  108. if err != nil {
  109. return 0, err
  110. } else {
  111. return redisdb.Int64(reply, nil)
  112. }
  113. }
  114. /*
  115. * hash批量存值,
  116. * args (key,field,value,field,value,field,value...) ,,, ...
  117. * 2020/06/06
  118. */
  119. func HMSet(args ...interface{}) (interface{}, error) {
  120. c := GetConn()
  121. reply, err := c.Do("HMSET", args...)
  122. CloseConn(c)
  123. return reply, err
  124. }
  125. /*
  126. * hash存值,
  127. * key
  128. * expire 过期时长
  129. * 2020/06/06
  130. */
  131. func HSetExpire(key string, expire int) (interface{}, error) {
  132. c := GetConn()
  133. reply, err := c.Do("expire", key, expire)
  134. CloseConn(c)
  135. return reply, err
  136. }
  137. /**
  138. * hash键是否存在
  139. * key
  140. * field
  141. */
  142. func HExists(key, field string) (int64, error) {
  143. c := GetConn()
  144. reply, err := c.Do("HEXISTS", key, field)
  145. CloseConn(c)
  146. if err != nil {
  147. return 0, err
  148. } else {
  149. return redisdb.Int64(reply, nil)
  150. }
  151. }