github.com/lingyao2333/mo-zero@v1.4.1/core/stores/redis/redisclientmanager.go (about)

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