Files
go-zero/core/stores/redis/redisclientmanager.go

45 lines
820 B
Go
Raw Normal View History

2020-07-26 17:09:05 +08:00
package redis
import (
"crypto/tls"
2020-07-26 17:09:05 +08:00
"io"
red "github.com/go-redis/redis/v8"
"github.com/zeromicro/go-zero/core/syncx"
2020-07-26 17:09:05 +08:00
)
const (
defaultDatabase = 0
maxRetries = 3
idleConns = 8
)
var clientManager = syncx.NewResourceManager()
func getClient(r *Redis) (*red.Client, error) {
val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) {
var tlsConfig *tls.Config
if r.tls {
tlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
2020-07-26 17:09:05 +08:00
store := red.NewClient(&red.Options{
Addr: r.Addr,
Password: r.Pass,
2020-07-26 17:09:05 +08:00
DB: defaultDatabase,
MaxRetries: maxRetries,
MinIdleConns: idleConns,
TLSConfig: tlsConfig,
2020-07-26 17:09:05 +08:00
})
store.AddHook(durationHook)
2020-07-26 17:09:05 +08:00
return store, nil
})
if err != nil {
return nil, err
}
return val.(*red.Client), nil
}