mirror of
https://github.com/silenceper/wechat.git
synced 2025-12-19 16:52:24 +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 (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
@@ -15,25 +17,38 @@ type Redis struct {
|
||||
|
||||
// RedisOpts redis 连接属性
|
||||
type RedisOpts struct {
|
||||
Host string `yml:"host" json:"host"`
|
||||
Username string `yaml:"username" json:"username"`
|
||||
Password string `yml:"password" json:"password"`
|
||||
Database int `yml:"database" json:"database"`
|
||||
MaxIdle int `yml:"max_idle" json:"max_idle"`
|
||||
MaxActive int `yml:"max_active" json:"max_active"`
|
||||
IdleTimeout int `yml:"idle_timeout" json:"idle_timeout"` // second
|
||||
Host string `json:"host" yml:"host"`
|
||||
Username string `json:"username" yaml:"username"`
|
||||
Password string `json:"password" yml:"password"`
|
||||
Database int `json:"database" yml:"database"`
|
||||
MaxIdle int `json:"max_idle" yml:"max_idle"`
|
||||
MaxActive int `json:"max_active" yml:"max_active"`
|
||||
IdleTimeout int `json:"idle_timeout" yml:"idle_timeout"` // second
|
||||
UseTLS bool `json:"use_tls" yml:"use_tls"` // 是否使用TLS
|
||||
}
|
||||
|
||||
// NewRedis 实例化
|
||||
func NewRedis(ctx context.Context, opts *RedisOpts) *Redis {
|
||||
conn := redis.NewUniversalClient(&redis.UniversalOptions{
|
||||
uniOpt := &redis.UniversalOptions{
|
||||
Addrs: []string{opts.Host},
|
||||
DB: opts.Database,
|
||||
Username: opts.Username,
|
||||
Password: opts.Password,
|
||||
IdleTimeout: time.Second * time.Duration(opts.IdleTimeout),
|
||||
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}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user