redis.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package cache
  2. import (
  3. "context"
  4. "crypto/tls"
  5. "net"
  6. "time"
  7. "github.com/go-redis/redis/v8"
  8. )
  9. // Redis .redis cache
  10. type Redis struct {
  11. ctx context.Context
  12. conn redis.UniversalClient
  13. }
  14. // RedisOpts redis 连接属性
  15. type RedisOpts struct {
  16. Host string `json:"host" yaml:"host"`
  17. Username string `json:"username" yaml:"username"`
  18. Password string `json:"password" yaml:"password"`
  19. Database int `json:"database" yaml:"database"`
  20. MinIdleConns int `json:"min_idle_conns" yaml:"min_idle_conns"` // 最小空闲连接数
  21. PoolSize int `json:"pool_size" yaml:"pool_size"` // 连接池大小,0 表示使用默认值(即 CPU 核心数 * 10)
  22. MaxRetries int `json:"max_retries" yaml:"max_retries"` // 最大重试次数,-1 表示不重试,0 表示使用默认值(即 3 次)
  23. DialTimeout int `json:"dial_timeout" yaml:"dial_timeout"` // 连接超时时间(秒),0 表示使用默认值(即 5 秒)
  24. ReadTimeout int `json:"read_timeout" yaml:"read_timeout"` // 读取超时时间(秒),-1 表示不超时,0 表示使用默认值(即 3 秒)
  25. WriteTimeout int `json:"write_timeout" yaml:"write_timeout"` // 写入超时时间(秒),-1 表示不超时,0 表示使用默认值(即 ReadTimeout)
  26. PoolTimeout int `json:"pool_timeout" yaml:"pool_timeout"` // 连接池获取连接超时时间(秒),0 表示使用默认值(即 ReadTimeout + 1 秒)
  27. IdleTimeout int `json:"idle_timeout" yaml:"idle_timeout"` // 空闲连接超时时间(秒),-1 表示禁用空闲连接超时检查,0 表示使用默认值(即 5 分钟)
  28. UseTLS bool `json:"use_tls" yaml:"use_tls"` // 是否使用 TLS
  29. // Deprecated: 应使用 MinIdleConns 代替
  30. MaxIdle int `json:"max_idle" yaml:"max_idle"`
  31. // Deprecated: 应使用 PoolSize 代替
  32. MaxActive int `json:"max_active" yaml:"max_active"`
  33. }
  34. // NewRedis 实例化
  35. func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
  36. uniOpt := &redis.UniversalOptions{
  37. Addrs: []string{opts.Host},
  38. DB: opts.Database,
  39. Username: opts.Username,
  40. Password: opts.Password,
  41. MinIdleConns: opts.MinIdleConns,
  42. PoolSize: opts.PoolSize,
  43. MaxRetries: opts.MaxRetries,
  44. }
  45. // 兼容旧的 MaxIdle 参数,仅在未显式设置 MinIdleConns 时生效
  46. if opts.MaxIdle > 0 && opts.MinIdleConns == 0 {
  47. uniOpt.MinIdleConns = opts.MaxIdle
  48. }
  49. // 兼容旧的 MaxActive 参数,仅在未显式设置 PoolSize 时生效
  50. if opts.MaxActive > 0 && opts.PoolSize == 0 {
  51. uniOpt.PoolSize = opts.MaxActive
  52. }
  53. applyTimeout := func(seconds int, target *time.Duration) {
  54. if seconds > 0 {
  55. *target = time.Duration(seconds) * time.Second
  56. } else if seconds == -1 {
  57. // 当 seconds 为 -1 时,表示禁用超时:按 go-redis 约定,将超时时间设置为负值(如 -1ns)代表「无超时」
  58. *target = -1
  59. }
  60. // 当 seconds 为 0 时,使用 go-redis 的默认超时配置:
  61. // 不修改 target,保持其零值(0),由 go-redis 解释为“使用默认值”
  62. }
  63. applyTimeout(opts.DialTimeout, &uniOpt.DialTimeout)
  64. applyTimeout(opts.ReadTimeout, &uniOpt.ReadTimeout)
  65. applyTimeout(opts.WriteTimeout, &uniOpt.WriteTimeout)
  66. applyTimeout(opts.PoolTimeout, &uniOpt.PoolTimeout)
  67. applyTimeout(opts.IdleTimeout, &uniOpt.IdleTimeout)
  68. if opts.UseTLS {
  69. h, _, err := net.SplitHostPort(opts.Host)
  70. if err != nil {
  71. h = opts.Host
  72. }
  73. uniOpt.TLSConfig = &tls.Config{
  74. ServerName: h,
  75. }
  76. }
  77. conn := redis.NewUniversalClient(uniOpt)
  78. return &Redis{ctx: ctx, conn: conn}
  79. }
  80. // SetConn 设置conn
  81. func (r *Redis) SetConn(conn redis.UniversalClient) {
  82. r.conn = conn
  83. }
  84. // SetRedisCtx 设置redis ctx 参数
  85. func (r *Redis) SetRedisCtx(ctx context.Context) {
  86. r.ctx = ctx
  87. }
  88. // Get 获取一个值
  89. func (r *Redis) Get(key string) interface{} {
  90. return r.GetContext(r.ctx, key)
  91. }
  92. // GetContext 获取一个值
  93. func (r *Redis) GetContext(ctx context.Context, key string) interface{} {
  94. result, err := r.conn.Do(ctx, "GET", key).Result()
  95. if err != nil {
  96. return nil
  97. }
  98. return result
  99. }
  100. // Set 设置一个值
  101. func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
  102. return r.SetContext(r.ctx, key, val, timeout)
  103. }
  104. // SetContext 设置一个值
  105. func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
  106. return r.conn.SetEX(ctx, key, val, timeout).Err()
  107. }
  108. // IsExist 判断key是否存在
  109. func (r *Redis) IsExist(key string) bool {
  110. return r.IsExistContext(r.ctx, key)
  111. }
  112. // IsExistContext 判断key是否存在
  113. func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
  114. result, _ := r.conn.Exists(ctx, key).Result()
  115. return result > 0
  116. }
  117. // Delete 删除
  118. func (r *Redis) Delete(key string) error {
  119. return r.DeleteContext(r.ctx, key)
  120. }
  121. // DeleteContext 删除
  122. func (r *Redis) DeleteContext(ctx context.Context, key string) error {
  123. return r.conn.Del(ctx, key).Err()
  124. }