2020-07-26 17:09:05 +08:00
|
|
|
package redis
|
|
|
|
|
|
|
|
|
|
import (
|
2021-04-07 20:44:16 +08:00
|
|
|
"crypto/tls"
|
2020-07-26 17:09:05 +08:00
|
|
|
"io"
|
|
|
|
|
|
2022-02-09 11:06:06 +08:00
|
|
|
red "github.com/go-redis/redis/v8"
|
2022-01-04 15:51:32 +08:00
|
|
|
"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()
|
|
|
|
|
|
2021-04-09 22:40:43 +08:00
|
|
|
func getClient(r *Redis) (*red.Client, error) {
|
|
|
|
|
val, err := clientManager.GetResource(r.Addr, func() (io.Closer, error) {
|
2021-04-08 18:19:36 +08:00
|
|
|
var tlsConfig *tls.Config
|
2021-04-09 22:40:43 +08:00
|
|
|
if r.tls {
|
2021-04-07 20:44:16 +08:00
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-26 17:09:05 +08:00
|
|
|
store := red.NewClient(&red.Options{
|
2021-04-09 22:40:43 +08:00
|
|
|
Addr: r.Addr,
|
|
|
|
|
Password: r.Pass,
|
2020-07-26 17:09:05 +08:00
|
|
|
DB: defaultDatabase,
|
|
|
|
|
MaxRetries: maxRetries,
|
|
|
|
|
MinIdleConns: idleConns,
|
2021-04-07 20:44:16 +08:00
|
|
|
TLSConfig: tlsConfig,
|
2020-07-26 17:09:05 +08:00
|
|
|
})
|
2022-02-09 11:06:06 +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
|
|
|
|
|
}
|