redis.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Info().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)) {
  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
  143. }
  144. // Publish publish message
  145. func (c RedisCache) Publish(channel, message string) error {
  146. return c.c.Publish(channel, message).Err()
  147. }
  148. // PublishCluster publish message
  149. func (c RedisCache) PublishCluster(channel, message string) error {
  150. return c.cc.Publish(channel, message).Err()
  151. }
  152. func connect() (*RedisCache, error) {
  153. if cache != nil {
  154. return cache, nil
  155. }
  156. //*
  157. var err error
  158. once.Do(func() {
  159. isCluster = false
  160. if len(redisConfig.Addr) > 0 {
  161. cache = NewRedisCache()
  162. }
  163. if len(redisClusterConfig.Addrs) > 0 {
  164. cache = NewRedisClusterCache()
  165. isCluster = true
  166. }
  167. })
  168. //*/
  169. return cache, err
  170. }
  171. // Get key from cache
  172. func Get(key string) (string, error) {
  173. c, err := connect()
  174. if err != nil {
  175. return "", err
  176. }
  177. return c.Get(key)
  178. }
  179. // Set key-value to cache
  180. func Set(key, value string, expiration time.Duration) error {
  181. c, err := connect()
  182. if err != nil {
  183. return err
  184. }
  185. return c.Set(key, value, expiration)
  186. }
  187. // Del delete key from cache
  188. func Del(key string) (int64, error) {
  189. c, err := connect()
  190. if err != nil {
  191. return 0, err
  192. }
  193. return c.Del(key)
  194. }
  195. func Info() (string, error) {
  196. c, err := connect()
  197. if err != nil {
  198. return "", err
  199. }
  200. return c.Info()
  201. }