redis.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package cache
  2. import (
  3. "time"
  4. "github.com/go-redis/redis"
  5. )
  6. // RedisConfig config
  7. type RedisConfig struct {
  8. Addr string
  9. Password string
  10. DialTimeout time.Duration
  11. ReadTimeout time.Duration
  12. WriteTimeout time.Duration
  13. PoolTimeout time.Duration
  14. PoolSize int
  15. }
  16. // RedisClusterConfig redis cluster configure
  17. type RedisClusterConfig struct {
  18. // A seed list of host:port addresses of cluster nodes.
  19. Addrs []string
  20. // The maximum number of retries before giving up. Command is retried
  21. // on network errors and MOVED/ASK redirects.
  22. // Default is 16.
  23. MaxRedirects int
  24. // Enables read-only commands on slave nodes.
  25. ReadOnly bool
  26. // Allows routing read-only commands to the closest master or slave node.
  27. RouteByLatency bool
  28. //OnConnect func(*Conn) error
  29. MaxRetries int
  30. MinRetryBackoff time.Duration
  31. MaxRetryBackoff time.Duration
  32. Password string
  33. DialTimeout time.Duration
  34. ReadTimeout time.Duration
  35. WriteTimeout time.Duration
  36. // PoolSize applies per cluster node and not for the whole cluster.
  37. PoolSize int
  38. PoolTimeout time.Duration
  39. IdleTimeout time.Duration
  40. IdleCheckFrequency time.Duration
  41. }
  42. var (
  43. redisConfig RedisConfig
  44. redisClusterConfig RedisClusterConfig
  45. )
  46. // RedisCache define
  47. type RedisCache struct {
  48. c *redis.Client
  49. cc *redis.ClusterClient
  50. }
  51. // SetRedisConfig set
  52. func SetRedisConfig(cfg RedisConfig) {
  53. redisConfig = cfg
  54. }
  55. // SetRedisClusterConfig set
  56. func SetRedisClusterConfig(cfg RedisClusterConfig) {
  57. redisClusterConfig = cfg
  58. }
  59. // NewRedisCache new RedisCache object
  60. func NewRedisCache() *RedisCache {
  61. client := redis.NewClient(&redis.Options{
  62. Addr: redisConfig.Addr,
  63. Password: redisConfig.Password,
  64. DialTimeout: redisConfig.DialTimeout,
  65. ReadTimeout: redisConfig.ReadTimeout,
  66. WriteTimeout: redisConfig.WriteTimeout,
  67. PoolSize: redisConfig.PoolSize,
  68. PoolTimeout: redisConfig.PoolTimeout,
  69. })
  70. client.Ping()
  71. return &RedisCache{c: client}
  72. }
  73. // NewRedisClusterCache new RedisCluster object
  74. func NewRedisClusterCache() *RedisCache {
  75. var config redis.ClusterOptions
  76. config.Addrs = redisClusterConfig.Addrs
  77. config.MaxRedirects = redisClusterConfig.MaxRedirects
  78. config.ReadOnly = redisClusterConfig.ReadOnly
  79. config.RouteByLatency = redisClusterConfig.RouteByLatency
  80. config.MaxRetries = redisClusterConfig.MaxRetries
  81. config.MinRetryBackoff = redisClusterConfig.MinRetryBackoff
  82. config.MaxRetryBackoff = redisClusterConfig.MaxRetryBackoff
  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. return c.c.Get(key).Result()
  98. }
  99. // GetCluster get value from cluster cache
  100. func (c RedisCache) GetCluster(key string) (string, error) {
  101. return c.cc.Get(key).Result()
  102. }
  103. // Set set key-value to cache
  104. func (c RedisCache) Set(key, value string, expiration time.Duration) error {
  105. return c.c.Set(key, value, expiration).Err()
  106. }
  107. // SetCluster set key-value to cache
  108. func (c RedisCache) SetCluster(key, value string, expiration time.Duration) error {
  109. return c.cc.Set(key, value, expiration).Err()
  110. }
  111. // Subscribe subscribe message
  112. func (c RedisCache) Subscribe(channels string, cb func(channel string, message string, err error)) {
  113. pubsub := c.c.Subscribe(channels)
  114. defer pubsub.Close()
  115. var (
  116. msg *redis.Message
  117. err error
  118. )
  119. msg, err = pubsub.ReceiveMessage()
  120. for err == nil {
  121. cb(msg.Channel, msg.Payload, nil)
  122. msg, err = pubsub.ReceiveMessage()
  123. }
  124. cb("", "", err)
  125. return
  126. }
  127. // SubscribeCluster subscribe cluster message
  128. func (c RedisCache) SubscribeCluster(channels string, cb func(channel string, message string, err error)) {
  129. pubsub := c.cc.Subscribe(channels)
  130. defer pubsub.Close()
  131. var (
  132. msg *redis.Message
  133. err error
  134. )
  135. msg, err = pubsub.ReceiveMessage()
  136. for err == nil {
  137. cb(msg.Channel, msg.Payload, nil)
  138. msg, err = pubsub.ReceiveMessage()
  139. }
  140. cb("", "", err)
  141. return
  142. }
  143. // Publish publish message
  144. func (c RedisCache) Publish(channel, message string) error {
  145. return c.c.Publish(channel, message).Err()
  146. }
  147. // PublishCluster publish message
  148. func (c RedisCache) PublishCluster(channel, message string) error {
  149. return c.cc.Publish(channel, message).Err()
  150. }