redis.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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) Get(key string) (string, error) {
  97. if c.cc != nil {
  98. return c.cc.Get(key).Result()
  99. }
  100. return c.c.Get(key).Result()
  101. }
  102. // Set set key-value to cache
  103. func (c RedisCache) Set(key, value string, expiration time.Duration) error {
  104. if c.cc != nil {
  105. return c.cc.Set(key, value, expiration).Err()
  106. }
  107. return c.c.Set(key, value, expiration).Err()
  108. }
  109. // Del Del value from cache
  110. func (c RedisCache) Del(key string) (int64, error) {
  111. if c.cc != nil {
  112. return c.cc.Del(key).Result()
  113. }
  114. return c.c.Del(key).Result()
  115. }
  116. // Info info
  117. func (c RedisCache) Info() (string, error) {
  118. if c.cc != nil {
  119. return c.cc.ClusterInfo().Result()
  120. }
  121. return c.c.Info().Result()
  122. }
  123. // Subscribe subscribe message
  124. func (c RedisCache) Subscribe(channels string, cb func(channel string, message string, err error)) error {
  125. pubsub := &redis.PubSub{}
  126. if c.cc != nil {
  127. pubsub = c.cc.Subscribe(channels)
  128. } else {
  129. pubsub = c.c.Subscribe(channels)
  130. }
  131. defer pubsub.Close()
  132. var (
  133. msg *redis.Message
  134. err error
  135. )
  136. msg, err = pubsub.ReceiveMessage()
  137. for err == nil {
  138. cb(msg.Channel, msg.Payload, nil)
  139. msg, err = pubsub.ReceiveMessage()
  140. }
  141. cb("", "", err)
  142. return err
  143. }
  144. // Publish publish message
  145. func (c RedisCache) Publish(channel, message string) error {
  146. if c.cc != nil {
  147. return c.cc.Publish(channel, message).Err()
  148. }
  149. return c.c.Publish(channel, message).Err()
  150. }
  151. func connect() (*RedisCache, error) {
  152. if cache != nil {
  153. return cache, nil
  154. }
  155. //*
  156. var err error
  157. once.Do(func() {
  158. isCluster = false
  159. if len(redisConfig.Addr) > 0 {
  160. cache = NewRedisCache()
  161. }
  162. if len(redisClusterConfig.Addrs) > 0 {
  163. cache = NewRedisClusterCache()
  164. isCluster = true
  165. }
  166. })
  167. //*/
  168. return cache, err
  169. }
  170. // Get key from cache
  171. func Get(key string) (string, error) {
  172. c, err := connect()
  173. if err != nil {
  174. return "", err
  175. }
  176. return c.Get(key)
  177. }
  178. // Set key-value to cache
  179. func Set(key, value string, expiration time.Duration) error {
  180. c, err := connect()
  181. if err != nil {
  182. return err
  183. }
  184. return c.Set(key, value, expiration)
  185. }
  186. // Del delete key from cache
  187. func Del(key string) (int64, error) {
  188. c, err := connect()
  189. if err != nil {
  190. return 0, err
  191. }
  192. return c.Del(key)
  193. }
  194. func Info() (string, error) {
  195. c, err := connect()
  196. if err != nil {
  197. return "", err
  198. }
  199. return c.Info()
  200. }
  201. // Publish publish message
  202. func Publish(channel, message string) error {
  203. c, err := connect()
  204. if err != nil {
  205. return err
  206. }
  207. return c.Publish(channel, message)
  208. }
  209. func Subscribe(channels string, cb func(channel string, message string, err error)) error {
  210. c, err := connect()
  211. if err != nil {
  212. return err
  213. }
  214. return c.Subscribe(channels, cb)
  215. }