mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
feat: 支持Redis作为Cache的时候使用TLS (#834)
* feat: 支持Redis作为Cache的时候使用TLS * feat: fix lint * fix lint * Update redis.go
This commit is contained in:
33
cache/redis.go
vendored
33
cache/redis.go
vendored
@@ -2,6 +2,8 @@ package cache
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
@@ -15,25 +17,38 @@ type Redis struct {
|
|||||||
|
|
||||||
// RedisOpts redis 连接属性
|
// RedisOpts redis 连接属性
|
||||||
type RedisOpts struct {
|
type RedisOpts struct {
|
||||||
Host string `yml:"host" json:"host"`
|
Host string `json:"host" yml:"host"`
|
||||||
Username string `yaml:"username" json:"username"`
|
Username string `json:"username" yaml:"username"`
|
||||||
Password string `yml:"password" json:"password"`
|
Password string `json:"password" yml:"password"`
|
||||||
Database int `yml:"database" json:"database"`
|
Database int `json:"database" yml:"database"`
|
||||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
MaxIdle int `json:"max_idle" yml:"max_idle"`
|
||||||
MaxActive int `yml:"max_active" json:"max_active"`
|
MaxActive int `json:"max_active" yml:"max_active"`
|
||||||
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` // second
|
IdleTimeout int `json:"idle_timeout" yml:"idle_timeout"` // second
|
||||||
|
UseTLS bool `json:"use_tls" yml:"use_tls"` // 是否使用TLS
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedis 实例化
|
// NewRedis 实例化
|
||||||
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
||||||
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
uniOpt := &redis.UniversalOptions{
|
||||||
Addrs: []string{opts.Host},
|
Addrs: []string{opts.Host},
|
||||||
DB: opts.Database,
|
DB: opts.Database,
|
||||||
Username: opts.Username,
|
Username: opts.Username,
|
||||||
Password: opts.Password,
|
Password: opts.Password,
|
||||||
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||||
MinIdleConns: opts.MaxIdle,
|
MinIdleConns: opts.MaxIdle,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if opts.UseTLS {
|
||||||
|
h, _, err := net.SplitHostPort(opts.Host)
|
||||||
|
if err != nil {
|
||||||
|
h = opts.Host
|
||||||
|
}
|
||||||
|
uniOpt.TLSConfig = &tls.Config{
|
||||||
|
ServerName: h,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := redis.NewUniversalClient(uniOpt)
|
||||||
return &Redis{ctx: ctx, conn: conn}
|
return &Redis{ctx: ctx, conn: conn}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user