simplify redis tls implementation (#606)

This commit is contained in:
Kevin Wan
2021-04-08 18:19:36 +08:00
committed by GitHub
parent 8cb6490724
commit 9963ffb1c1
5 changed files with 83 additions and 208 deletions

View File

@@ -14,10 +14,10 @@ var (
type (
// A RedisConf is a redis config.
RedisConf struct {
Host string
Type string `json:",default=node,options=node|cluster"`
Pass string `json:",optional"`
TLSFlag bool `json:",default=false,options=true|false"`
Host string
Type string `json:",default=node,options=node|cluster"`
Pass string `json:",optional"`
Tls bool `json:",default=false,options=true|false"`
}
// A RedisKeyConf is a redis config with key.
@@ -29,10 +29,18 @@ type (
// NewRedis returns a Redis.
func (rc RedisConf) NewRedis() *Redis {
if rc.TLSFlag {
return NewRedisWithTLS(rc.Host, rc.Type, rc.TLSFlag, rc.Pass)
var opts []Option
if rc.Type == ClusterType {
opts = append(opts, Cluster())
}
return NewRedis(rc.Host, rc.Type, rc.Pass)
if len(rc.Pass) > 0 {
opts = append(opts, WithPass(rc.Pass))
}
if rc.Tls {
opts = append(opts, WithTLS())
}
return New(rc.Host, opts...)
}
// Validate validates the RedisConf.