Files
redisx/options.go
T
2026-05-20 19:46:32 +08:00

79 lines
1.7 KiB
Go

package redisx
type redisOption struct {
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{
mode: "single",
address: []string{"localhost:6379"},
}
}
type Option func(*redisOption)
// 127.0.0.1:6379
func SetAddress(addrs []string) Option {
return func(o *redisOption) {
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
}
}
func SetPassword(password string) Option {
return func(o *redisOption) {
o.password = password
}
}
func SetDb(db int) Option {
return func(o *redisOption) {
o.db = db
}
}