redis.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package cache
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/go-redis/redis/v7"
  6. )
  7. // RedisConfig config
  8. type RedisConfig struct {
  9. Addr string
  10. Password string
  11. DB int
  12. DialTimeout time.Duration
  13. ReadTimeout time.Duration
  14. WriteTimeout time.Duration
  15. PoolTimeout time.Duration
  16. PoolSize int
  17. }
  18. // RedisClusterConfig redis cluster configure
  19. type RedisClusterConfig struct {
  20. // A seed list of host:port addresses of cluster nodes.
  21. Addrs []string
  22. Password string
  23. DB int
  24. // The maximum number of retries before giving up. Command is retried
  25. // on network errors and MOVED/ASK redirects.
  26. // Default is 16.
  27. MaxRedirects int
  28. // Enables read-only commands on slave nodes.
  29. ReadOnly bool
  30. // Allows routing read-only commands to the closest master or slave node.
  31. RouteByLatency bool
  32. //OnConnect func(*Conn) error
  33. MaxRetries int
  34. MinRetryBackoff time.Duration
  35. MaxRetryBackoff time.Duration
  36. DialTimeout time.Duration
  37. ReadTimeout time.Duration
  38. WriteTimeout time.Duration
  39. // PoolSize applies per cluster node and not for the whole cluster.
  40. PoolSize int
  41. PoolTimeout time.Duration
  42. IdleTimeout time.Duration
  43. IdleCheckFrequency time.Duration
  44. }
  45. var (
  46. redisConfig RedisConfig
  47. redisClusterConfig RedisClusterConfig
  48. once sync.Once
  49. cache *RedisCache
  50. isCluster bool
  51. )
  52. // RedisCache define
  53. type RedisCache struct {
  54. c *redis.Client
  55. cc *redis.ClusterClient
  56. }
  57. // SetRedisConfig set
  58. func SetRedisConfig(cfg RedisConfig) {
  59. redisConfig = cfg
  60. }
  61. // SetRedisClusterConfig set
  62. func SetRedisClusterConfig(cfg RedisClusterConfig) {
  63. redisClusterConfig = cfg
  64. }
  65. // NewRedisCache new RedisCache object
  66. func NewRedisCache() *RedisCache {
  67. client := redis.NewClient(&redis.Options{
  68. Addr: redisConfig.Addr,
  69. Password: redisConfig.Password,
  70. DialTimeout: redisConfig.DialTimeout,
  71. ReadTimeout: redisConfig.ReadTimeout,
  72. WriteTimeout: redisConfig.WriteTimeout,
  73. PoolSize: redisConfig.PoolSize,
  74. PoolTimeout: redisConfig.PoolTimeout,
  75. })
  76. client.Ping().Result()
  77. return &RedisCache{c: client}
  78. }
  79. // NewRedisClusterCache new RedisCluster object
  80. func NewRedisClusterCache() *RedisCache {
  81. var config redis.ClusterOptions
  82. config.Addrs = redisClusterConfig.Addrs
  83. config.Password = redisClusterConfig.Password
  84. config.DialTimeout = redisClusterConfig.DialTimeout
  85. config.ReadTimeout = redisClusterConfig.ReadTimeout
  86. config.WriteTimeout = redisClusterConfig.WriteTimeout
  87. config.PoolSize = redisClusterConfig.PoolSize
  88. config.PoolTimeout = redisClusterConfig.PoolTimeout
  89. config.IdleTimeout = redisClusterConfig.IdleTimeout
  90. config.IdleCheckFrequency = redisClusterConfig.IdleCheckFrequency
  91. client := redis.NewClusterClient(&config)
  92. client.Ping()
  93. return &RedisCache{cc: client}
  94. }
  95. // Get get value from cache
  96. func (c RedisCache) Ping() (string, error) {
  97. if c.cc != nil {
  98. return c.cc.Ping().Result()
  99. }
  100. return c.c.Ping().Result()
  101. }
  102. // Get get value from cache
  103. func (c RedisCache) Get(key string) (string, error) {
  104. if c.cc != nil {
  105. return c.cc.Get(key).Result()
  106. }
  107. return c.c.Get(key).Result()
  108. }
  109. // Set set key-value to cache
  110. func (c RedisCache) Set(key, value string, expiration time.Duration) error {
  111. if c.cc != nil {
  112. return c.cc.Set(key, value, expiration).Err()
  113. }
  114. return c.c.Set(key, value, expiration).Err()
  115. }
  116. // Del Del value from cache
  117. func (c RedisCache) Del(key string) (int64, error) {
  118. if c.cc != nil {
  119. return c.cc.Del(key).Result()
  120. }
  121. return c.c.Del(key).Result()
  122. }
  123. // Info info
  124. func (c RedisCache) Info() (string, error) {
  125. if c.cc != nil {
  126. return c.cc.ClusterInfo().Result()
  127. }
  128. return c.c.Info().Result()
  129. }
  130. // Subscribe subscribe message
  131. func (c RedisCache) Subscribe(channels string, cb func(channel string, message string, err error)) error {
  132. pubsub := &redis.PubSub{}
  133. if c.cc != nil {
  134. pubsub = c.cc.Subscribe(channels)
  135. } else {
  136. pubsub = c.c.Subscribe(channels)
  137. }
  138. defer pubsub.Close()
  139. var (
  140. msg *redis.Message
  141. err error
  142. )
  143. msg, err = pubsub.ReceiveMessage()
  144. for err == nil {
  145. cb(msg.Channel, msg.Payload, nil)
  146. msg, err = pubsub.ReceiveMessage()
  147. }
  148. cb("", "", err)
  149. return err
  150. }
  151. // Publish publish message
  152. func (c RedisCache) Publish(channel, message string) error {
  153. if c.cc != nil {
  154. return c.cc.Publish(channel, message).Err()
  155. }
  156. return c.c.Publish(channel, message).Err()
  157. }
  158. func connect() (*RedisCache, error) {
  159. if cache != nil {
  160. return cache, nil
  161. }
  162. //*
  163. var err error
  164. once.Do(func() {
  165. isCluster = false
  166. if len(redisConfig.Addr) > 0 {
  167. cache = NewRedisCache()
  168. }
  169. if len(redisClusterConfig.Addrs) > 0 {
  170. cache = NewRedisClusterCache()
  171. isCluster = true
  172. }
  173. })
  174. //*/
  175. return cache, err
  176. }
  177. // Ping ping
  178. func Ping() (string, error) {
  179. c, err := connect()
  180. if err != nil {
  181. return "", err
  182. }
  183. return c.Ping()
  184. }
  185. // Info info
  186. func Info() (string, error) {
  187. c, err := connect()
  188. if err != nil {
  189. return "", err
  190. }
  191. return c.Info()
  192. }
  193. // Get key from cache
  194. func Get(key string) (string, error) {
  195. c, err := connect()
  196. if err != nil {
  197. return "", err
  198. }
  199. return c.Get(key)
  200. }
  201. // Set key-value to cache
  202. func Set(key, value string, expiration time.Duration) error {
  203. c, err := connect()
  204. if err != nil {
  205. return err
  206. }
  207. return c.Set(key, value, expiration)
  208. }
  209. // Del delete key from cache
  210. func Del(key string) (int64, error) {
  211. c, err := connect()
  212. if err != nil {
  213. return 0, err
  214. }
  215. return c.Del(key)
  216. }
  217. // Publish publish message
  218. func Publish(channel, message string) error {
  219. c, err := connect()
  220. if err != nil {
  221. return err
  222. }
  223. return c.Publish(channel, message)
  224. }
  225. func Subscribe(channels string, cb func(channel string, message string, err error)) error {
  226. c, err := connect()
  227. if err != nil {
  228. return err
  229. }
  230. return c.Subscribe(channels, cb)
  231. }