mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
support redis tls
This commit is contained in:
@ -46,6 +46,8 @@ type Config struct {
|
||||
IdleTimeout time.Duration // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0)
|
||||
MaxConnLifetime time.Duration // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0)
|
||||
ConnectTimeout time.Duration // Dial connection timeout.
|
||||
TLS bool //support tls
|
||||
TLSSkipVerify bool //tls skip verify
|
||||
}
|
||||
|
||||
// Pool statistics.
|
||||
@ -102,6 +104,8 @@ func New(config Config) *Redis {
|
||||
"tcp",
|
||||
fmt.Sprintf("%s:%d", config.Host, config.Port),
|
||||
redis.DialConnectTimeout(config.ConnectTimeout),
|
||||
redis.DialUseTLS(config.TLS),
|
||||
redis.DialTLSSkipVerify(config.TLSSkipVerify),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@ -110,6 +110,12 @@ func ConfigFromStr(str string) (config Config, err error) {
|
||||
if v, ok := parse["maxConnLifetime"]; ok {
|
||||
config.MaxConnLifetime = gconv.Duration(v) * time.Second
|
||||
}
|
||||
if v, ok := parse["tls"]; ok {
|
||||
config.TLS = gconv.Bool(v)
|
||||
}
|
||||
if v, ok := parse["skipVerify"]; ok {
|
||||
config.TLSSkipVerify = gconv.Bool(v)
|
||||
}
|
||||
return
|
||||
}
|
||||
array, _ = gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)`, str)
|
||||
|
||||
@ -27,6 +27,14 @@ var (
|
||||
Port: 6379,
|
||||
Db: 1,
|
||||
}
|
||||
|
||||
tlsConfig = gredis.Config{
|
||||
Host: "127.0.0.1",
|
||||
Port: 6379,
|
||||
Db: 1,
|
||||
TLS: true,
|
||||
TLSSkipVerify: true,
|
||||
}
|
||||
)
|
||||
|
||||
func Test_NewClose(t *testing.T) {
|
||||
@ -382,3 +390,27 @@ func Test_Auto_MarshalSlice(t *testing.T) {
|
||||
t.Assert(users2, users1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Conn_TLS(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
redis := gredis.New(tlsConfig)
|
||||
defer redis.Close()
|
||||
conn := redis.Conn()
|
||||
defer conn.Close()
|
||||
|
||||
key := gconv.String(gtime.TimestampNano())
|
||||
value := []byte("v")
|
||||
r, err := conn.Do("SET", key, value)
|
||||
t.Assert(err, nil)
|
||||
|
||||
r, err = conn.Do("GET", key)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(r, value)
|
||||
|
||||
_, err = conn.Do("DEL", key)
|
||||
t.Assert(err, nil)
|
||||
r, err = conn.Do("GET", key)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(r, nil)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user