redis.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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" yml:"host"`
  17. Username string `json:"username" yaml:"username"`
  18. Password string `json:"password" yml:"password"`
  19. Database int `json:"database" yml:"database"`
  20. MaxIdle int `json:"max_idle" yml:"max_idle"`
  21. MaxActive int `json:"max_active" yml:"max_active"`
  22. IdleTimeout int `json:"idle_timeout" yml:"idle_timeout"` // second
  23. UseTLS bool `json:"use_tls" yml:"use_tls"` // 是否使用TLS
  24. }
  25. // NewRedis 实例化
  26. func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
  27. uniOpt := &redis.UniversalOptions{
  28. Addrs: []string{opts.Host},
  29. DB: opts.Database,
  30. Username: opts.Username,
  31. Password: opts.Password,
  32. IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
  33. MinIdleConns: opts.MaxIdle,
  34. }
  35. if opts.UseTLS {
  36. h, _, err := net.SplitHostPort(opts.Host)
  37. if err != nil {
  38. h = opts.Host
  39. }
  40. uniOpt.TLSConfig = &tls.Config{
  41. ServerName: h,
  42. }
  43. }
  44. conn := redis.NewUniversalClient(uniOpt)
  45. return &Redis{ctx: ctx, conn: conn}
  46. }
  47. // SetConn 设置conn
  48. func (r *Redis) SetConn(conn redis.UniversalClient) {
  49. r.conn = conn
  50. }
  51. // SetRedisCtx 设置redis ctx 参数
  52. func (r *Redis) SetRedisCtx(ctx context.Context) {
  53. r.ctx = ctx
  54. }
  55. // Get 获取一个值
  56. func (r *Redis) Get(key string) interface{} {
  57. return r.GetContext(r.ctx, key)
  58. }
  59. // GetContext 获取一个值
  60. func (r *Redis) GetContext(ctx context.Context, key string) interface{} {
  61. result, err := r.conn.Do(ctx, "GET", key).Result()
  62. if err != nil {
  63. return nil
  64. }
  65. return result
  66. }
  67. // Set 设置一个值
  68. func (r *Redis) Set(key string, val interface{}, timeout time.Duration) error {
  69. return r.SetContext(r.ctx, key, val, timeout)
  70. }
  71. // SetContext 设置一个值
  72. func (r *Redis) SetContext(ctx context.Context, key string, val interface{}, timeout time.Duration) error {
  73. return r.conn.SetEX(ctx, key, val, timeout).Err()
  74. }
  75. // IsExist 判断key是否存在
  76. func (r *Redis) IsExist(key string) bool {
  77. return r.IsExistContext(r.ctx, key)
  78. }
  79. // IsExistContext 判断key是否存在
  80. func (r *Redis) IsExistContext(ctx context.Context, key string) bool {
  81. result, _ := r.conn.Exists(ctx, key).Result()
  82. return result > 0
  83. }
  84. // Delete 删除
  85. func (r *Redis) Delete(key string) error {
  86. return r.DeleteContext(r.ctx, key)
  87. }
  88. // DeleteContext 删除
  89. func (r *Redis) DeleteContext(ctx context.Context, key string) error {
  90. return r.conn.Del(ctx, key).Err()
  91. }