redisclientmanager.go 814 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package redis
  2. import (
  3. "crypto/tls"
  4. "io"
  5. red "github.com/go-redis/redis"
  6. "github.com/tal-tech/go-zero/core/syncx"
  7. )
  8. const (
  9. defaultDatabase = 0
  10. maxRetries = 3
  11. idleConns = 8
  12. )
  13. var clientManager = syncx.NewResourceManager()
  14. func getClient(r *Redis) (*red.Client, error) {
  15. val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) {
  16. var tlsConfig *tls.Config
  17. if r.tls {
  18. tlsConfig = &tls.Config{
  19. InsecureSkipVerify: true,
  20. }
  21. }
  22. store := red.NewClient(&red.Options{
  23. Addr: r.Addr,
  24. Password: r.Pass,
  25. DB: defaultDatabase,
  26. MaxRetries: maxRetries,
  27. MinIdleConns: idleConns,
  28. TLSConfig: tlsConfig,
  29. })
  30. store.WrapProcess(process)
  31. return store, nil
  32. })
  33. if err != nil {
  34. return nil, err
  35. }
  36. return val.(*red.Client), nil
  37. }