redisclustermanager.go 745 B

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