支持单机、哨兵、集群

This commit is contained in:
Yun
2026-05-20 19:46:32 +08:00
parent 6343c814b3
commit 81fa9b7690
4 changed files with 130 additions and 56 deletions
+50 -7
View File
@@ -1,24 +1,67 @@
package redisx
type redisOption struct {
addr string
password string
db int
poolSize int
mode RedisMode // 模式:single / cluster / sentinel,默认 single
address []string // 单机/集群地址:["127.0.0.1:6379"]
password string // 密码
db int // 单机/哨兵
poolSize int
enableTLS bool `yaml:"enable_tls"` // 是否启用TLS,默认false
sentinels []string `yaml:"sentinels"` // 哨兵模式地址,例如: ["127.0.0.1:26379"]
masterName string `yaml:"master_name"` // 哨兵监控的主节点名称
}
type RedisMode string
const (
RedisModeSingle RedisMode = "single" // 单机模式
RedisModeCluster RedisMode = "cluster" // 集群模式
RedisModeSentinel RedisMode = "sentinel" // 哨兵模式
)
func defaultOptions() redisOption {
return redisOption{
addr: "localhost:6379",
mode: "single",
address: []string{"localhost:6379"},
}
}
type Option func(*redisOption)
// 127.0.0.1:6379
func SetAddress(addr string) Option {
func SetAddress(addrs []string) Option {
return func(o *redisOption) {
o.addr = addr
o.address = addrs
}
}
func SetMode(mode RedisMode) Option {
return func(o *redisOption) {
o.mode = mode
}
}
func SetPoolSize(size int) Option {
return func(o *redisOption) {
o.poolSize = size
}
}
func SetEnableTLS(enableTLS bool) Option {
return func(o *redisOption) {
o.enableTLS = enableTLS
}
}
func SetSentinels(sentinels []string) Option {
return func(o *redisOption) {
o.sentinels = sentinels
}
}
func SetMasterName(masterName string) Option {
return func(o *redisOption) {
o.masterName = masterName
}
}