支持单机、哨兵、集群
This commit is contained in:
@@ -2,8 +2,9 @@ package redisx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
redis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// PoolSize:连接池中的最大连接数。
|
||||
@@ -14,43 +15,66 @@ import (
|
||||
// IdleCheckFrequency:检查空闲连接的频率。
|
||||
|
||||
// 单机
|
||||
func NewRedis(opts ...Option) redis.UniversalClient {
|
||||
func NewRedis(ctx context.Context, opts ...Option) redis.UniversalClient {
|
||||
opt := defaultOptions()
|
||||
for _, apply := range opts {
|
||||
apply(&opt)
|
||||
}
|
||||
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: opt.addr,
|
||||
Password: opt.password, // no password set
|
||||
DB: opt.db, // use default DB
|
||||
var tlsConfig *tls.Config
|
||||
if opt.enableTLS {
|
||||
tlsConfig = &tls.Config{InsecureSkipVerify: true}
|
||||
}
|
||||
|
||||
// 连接池配置参数
|
||||
// PoolSize: 100, // 连接池最大连接数
|
||||
// MinIdleConns: 10, // 最小空闲连接数
|
||||
// MaxConnAge: 30 * time.Minute, // 连接最大寿命
|
||||
// PoolTimeout: 4 * time.Second, // 等待连接池连接的最长时间
|
||||
// IdleTimeout: 5 * time.Minute, // 空闲连接的生命周期
|
||||
// IdleCheckFrequency: 60 * time.Second, // 空闲连接检查频率
|
||||
})
|
||||
_, err := client.Ping(context.Background()).Result()
|
||||
var client redis.UniversalClient
|
||||
|
||||
switch opt.mode {
|
||||
case RedisModeCluster:
|
||||
// 集群模式
|
||||
if len(opt.address) == 0 {
|
||||
panic("redis cluster mode requires at least one address")
|
||||
}
|
||||
client = redis.NewClusterClient(
|
||||
&redis.ClusterOptions{
|
||||
Addrs: opt.address,
|
||||
Password: opt.password, // no password set
|
||||
TLSConfig: tlsConfig,
|
||||
},
|
||||
)
|
||||
case RedisModeSentinel:
|
||||
// 哨兵模式
|
||||
if len(opt.sentinels) == 0 {
|
||||
panic("redis sentinel mode requires at least one sentinel address")
|
||||
}
|
||||
client = redis.NewFailoverClient(
|
||||
&redis.FailoverOptions{
|
||||
MasterName: opt.masterName,
|
||||
SentinelAddrs: opt.sentinels,
|
||||
Password: opt.password, // no password set
|
||||
TLSConfig: tlsConfig,
|
||||
DB: opt.db, // use default DB
|
||||
// SentinelPassword: global.Config.Redis.Password, // 哨兵认证密码(如果有)
|
||||
// SentinelUsername: global.Config.Redis.Username, // 哨兵认证用户名(如果有)
|
||||
},
|
||||
)
|
||||
default:
|
||||
// 单机模式
|
||||
if len(opt.address) == 0 {
|
||||
panic("redis single mode requires an address")
|
||||
}
|
||||
client = redis.NewClient(
|
||||
&redis.Options{
|
||||
Addr: opt.address[0],
|
||||
Password: opt.password, // no password set
|
||||
DB: opt.db, // use default DB
|
||||
TLSConfig: tlsConfig,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
_, err := client.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
|
||||
// 集群
|
||||
|
||||
// 哨兵
|
||||
// client = redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
// MasterName: "mymaster",
|
||||
// SentinelAddrs: []string{"127.0.0.1:26379", "127.0.0.1:26380"},
|
||||
// Password: "",
|
||||
// DB: 0,
|
||||
// })
|
||||
// for {
|
||||
// reply, err := client.Incr("pvcont").Result()
|
||||
// fmt.Printf("reply=%v err=%v\n", reply, err)
|
||||
// time.Sleep(1 * time.Second)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user