You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package redis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
red "github.com/go-redis/redis"
|
|
|
|
"github.com/tal-tech/go-zero/core/syncx"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultDatabase = 0
|
|
|
|
maxRetries = 3
|
|
|
|
idleConns = 8
|
|
|
|
)
|
|
|
|
|
|
|
|
var clientManager = syncx.NewResourceManager()
|
|
|
|
|
|
|
|
func getClient(server, pass string) (*red.Client, error) {
|
|
|
|
return getClientWithTLS(server, pass, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getClientWithTLS(server, pass string, tlsEnabled bool) (*red.Client, error) {
|
|
|
|
val, err := clientManager.GetResource(server, func() (io.Closer, error) {
|
|
|
|
var tlsConfig *tls.Config
|
|
|
|
if tlsEnabled {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store := red.NewClient(&red.Options{
|
|
|
|
Addr: server,
|
|
|
|
Password: pass,
|
|
|
|
DB: defaultDatabase,
|
|
|
|
MaxRetries: maxRetries,
|
|
|
|
MinIdleConns: idleConns,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
})
|
|
|
|
store.WrapProcess(process)
|
|
|
|
return store, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return val.(*red.Client), nil
|
|
|
|
}
|