redis.go 5.8 KB

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