redisclustermanager.go 774 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package redis
  2. import (
  3. "crypto/tls"
  4. "io"
  5. "strings"
  6. red "github.com/go-redis/redis/v8"
  7. "github.com/zeromicro/go-zero/core/syncx"
  8. )
  9. var clusterManager = syncx.NewResourceManager()
  10. func getCluster(r *Redis) (*red.ClusterClient, error) {
  11. val, err := clusterManager.GetResource(r.Addr, func() (io.Closer, error) {
  12. var tlsConfig *tls.Config
  13. if r.tls {
  14. tlsConfig = &tls.Config{
  15. InsecureSkipVerify: true,
  16. }
  17. }
  18. store := red.NewClusterClient(&red.ClusterOptions{
  19. Addrs: strings.Split(r.Addr, ","),
  20. Password: r.Pass,
  21. MaxRetries: maxRetries,
  22. MinIdleConns: idleConns,
  23. TLSConfig: tlsConfig,
  24. })
  25. store.AddHook(durationHook)
  26. return store, nil
  27. })
  28. if err != nil {
  29. return nil, err
  30. }
  31. return val.(*red.ClusterClient), nil
  32. }