redis_operation.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package cache
  2. import (
  3. "encoding/json"
  4. "github.com/go-redis/redis"
  5. "time"
  6. )
  7. const IsExists = 1
  8. func RedisTx(fn func(pipeline redis.Pipeliner) error) (err error) {
  9. if _isCluster {
  10. _, err = rcdb.TxPipelined(fn)
  11. }
  12. _, err = rdb.TxPipelined(fn)
  13. return
  14. }
  15. func RedisGet(key string) (string, error) {
  16. if _isCluster {
  17. return rcdb.Get(key).Result()
  18. }
  19. return rdb.Get(key).Result()
  20. }
  21. func RedisGetWithAddExpiration(key string, expiration time.Duration) (result string, err error) {
  22. if _isCluster {
  23. result, err = rcdb.Get(key).Result()
  24. if err != nil {
  25. rcdb.Expire(key, expiration)
  26. }
  27. } else {
  28. result, err = rdb.Get(key).Result()
  29. if err != nil {
  30. rdb.Expire(key, expiration)
  31. }
  32. }
  33. return
  34. }
  35. func RedisSet(key string, value interface{}) error {
  36. if _isCluster {
  37. return rcdb.Set(key, value, 0).Err()
  38. }
  39. return rdb.Set(key, value, 0).Err()
  40. }
  41. func RedisSetWithExpiration(key string, value interface{}, expiration time.Duration) error {
  42. if _isCluster {
  43. return rcdb.Set(key, value, expiration).Err()
  44. }
  45. return rdb.Set(key, value, expiration).Err()
  46. }
  47. func RedisGetObj(key string, obj interface{}) error {
  48. valueStr, err := RedisGet(key)
  49. if err != nil {
  50. return err
  51. }
  52. return json.Unmarshal([]byte(valueStr), &obj)
  53. }
  54. func RedisGetObjWithAddExpiration(key string, obj interface{}, expiration time.Duration) error {
  55. valueStr, err := RedisGetWithAddExpiration(key, expiration)
  56. if err != nil {
  57. return err
  58. }
  59. return json.Unmarshal([]byte(valueStr), &obj)
  60. }
  61. func RedisSetObj(key string, obj interface{}) error {
  62. valueBytes, err := json.Marshal(obj)
  63. if err != nil {
  64. return err
  65. }
  66. return RedisSet(key, string(valueBytes))
  67. }
  68. func RedisSetObjWithExpiration(key string, obj interface{}, expiration time.Duration) error {
  69. valueBytes, err := json.Marshal(obj)
  70. if err != nil {
  71. return err
  72. }
  73. return RedisSetWithExpiration(key, string(valueBytes), expiration)
  74. }
  75. func RedisLRange(key string, start int64, end int64) ([]string, error) {
  76. if _isCluster {
  77. return rcdb.LRange(key, start, end).Result()
  78. }
  79. return rdb.LRange(key, start, end).Result()
  80. }
  81. func RedisHMGet(key string, fields ...string) ([]interface{}, error) {
  82. if _isCluster {
  83. return rcdb.HMGet(key, fields...).Result()
  84. }
  85. return rdb.HMGet(key, fields...).Result()
  86. }
  87. func RedisHMSet(key string, fields map[string]interface{}) error {
  88. if _isCluster {
  89. return rcdb.HMSet(key, fields).Err()
  90. }
  91. return rdb.HMSet(key, fields).Err()
  92. }
  93. func RedisHSet(key string, field string, value interface{}) error {
  94. if _isCluster {
  95. return rcdb.HSet(key, field, value).Err()
  96. }
  97. return rdb.HSet(key, field, value).Err()
  98. }
  99. func RedisHIncrBy(key string, field string, incr int64) error {
  100. if _isCluster {
  101. return rcdb.HIncrBy(key, field, incr).Err()
  102. }
  103. return rdb.HIncrBy(key, field, incr).Err()
  104. }
  105. func RedisHGet(key string, field string) (string, error) {
  106. if _isCluster {
  107. return rcdb.HGet(key, field).Result()
  108. }
  109. return rdb.HGet(key, field).Result()
  110. }
  111. func RedisHKeys(key string) ([]string, error) {
  112. if _isCluster {
  113. return rcdb.HKeys(key).Result()
  114. }
  115. return rdb.HKeys(key).Result()
  116. }
  117. func RedisHGetAll(key string) (map[string]string, error) {
  118. if _isCluster {
  119. return rcdb.HGetAll(key).Result()
  120. }
  121. return rdb.HGetAll(key).Result()
  122. }
  123. func RedisDel(key string) error {
  124. if _isCluster {
  125. return rcdb.Del(key).Err()
  126. }
  127. return rdb.Del(key).Err()
  128. }
  129. func RedisExists(key string) (bool, error) {
  130. if _isCluster {
  131. result, err := rcdb.Do("EXISTS", key).Result()
  132. if err != nil {
  133. return false, err
  134. }
  135. return result == IsExists, nil
  136. } else {
  137. result, err := rdb.Do("EXISTS", key).Result()
  138. if err != nil {
  139. return false, err
  140. }
  141. return result == IsExists, nil
  142. }
  143. }
  144. func RedisExpireAt(key string, tm time.Time) error {
  145. if _isCluster {
  146. return rcdb.ExpireAt(key, tm).Err()
  147. }
  148. return rdb.ExpireAt(key, tm).Err()
  149. }
  150. func RedisLPush(key string, obj interface{}) error {
  151. if _isCluster {
  152. return rcdb.LPush(key, obj).Err()
  153. }
  154. return rdb.LPush(key, obj).Err()
  155. }
  156. func RedisLPushObj(key string, obj interface{}) error {
  157. valueBytes, err := json.Marshal(obj)
  158. if err != nil {
  159. return err
  160. }
  161. if _isCluster {
  162. return rcdb.LPush(key, string(valueBytes)).Err()
  163. }
  164. return rdb.LPush(key, string(valueBytes)).Err()
  165. }
  166. func RedisLPushObjList(key string, objList ...interface{}) error {
  167. strList := make([]string, 0)
  168. for _, obj := range objList {
  169. valueBytes, err := json.Marshal(obj)
  170. if err != nil {
  171. return err
  172. }
  173. strList = append(strList, string(valueBytes))
  174. }
  175. if _isCluster {
  176. return rcdb.LPush(key, strList).Err()
  177. }
  178. return rdb.LPush(key, strList).Err()
  179. }
  180. func RedisLTrim(key string, start int64, end int64) error {
  181. if _isCluster {
  182. return rcdb.LTrim(key, start, end).Err()
  183. }
  184. return rdb.LTrim(key, start, end).Err()
  185. }
  186. func RedisZIncr(key string, score float64, value string) error {
  187. if _isCluster {
  188. err := rcdb.ZIncr(key, redis.Z{Score: score, Member: value}).Err()
  189. return err
  190. }
  191. err := rdb.ZIncr(key, redis.Z{Score: score, Member: value}).Err()
  192. return err
  193. }
  194. func RedisZRevRange(key string, start int64, stop int64) ([]redis.Z, error) {
  195. if _isCluster {
  196. z, err := rcdb.ZRevRangeWithScores(key, start, stop).Result()
  197. return z, err
  198. }
  199. z, err := rdb.ZRevRangeWithScores(key, start, stop).Result()
  200. return z, err
  201. }
  202. func RedisZScore(key string, value string) (float64, error) {
  203. if _isCluster {
  204. score, err := rcdb.ZScore(key, value).Result()
  205. return score, err
  206. }
  207. score, err := rdb.ZScore(key, value).Result()
  208. return score, err
  209. }
  210. func RedisZRevRank(key string, value string) (int64, error) {
  211. if _isCluster {
  212. rank, err := rcdb.ZRevRank(key, value).Result()
  213. return rank, err
  214. }
  215. rank, err := rdb.ZRevRank(key, value).Result()
  216. return rank, err
  217. }