forked from printfcoder/stack-rpc-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
86 lines (71 loc) · 1.75 KB
/
redis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package config
import (
"strings"
)
// RedisConfig redis 配置
type RedisConfig interface {
GetEnabled() bool
GetConn() string
GetPassword() string
GetDBNum() int
GetSentinelConfig() RedisSentinelConfig
}
// RedisSentinelConfig 哨兵配置
type RedisSentinelConfig interface {
GetEnabled() bool
GetMaster() string
GetNodes() []string
}
// defaultRedisConfig redis 配置
type defaultRedisConfig struct {
Enabled bool `json:"enabled"`
Conn string `json:"conn"`
Password string `json:"password"`
DBNum int `json:"dbNum"`
Timeout int `json:"timeout"`
sentinel redisSentinel `json:"sentinel"`
}
type redisSentinel struct {
Enabled bool `json:"enabled"`
Master string `json:"master"`
Nodes string `json:"nodes"`
nodes []string
}
// GetEnabled redis 配置是否激活
func (r defaultRedisConfig) GetEnabled() bool {
return r.Enabled
}
// GetConn redis 地址
func (r defaultRedisConfig) GetConn() string {
return r.Conn
}
// GetPassword redis 密码
func (r defaultRedisConfig) GetPassword() string {
return r.Password
}
// GetDBNum redis 数据库分区序号
func (r defaultRedisConfig) GetDBNum() int {
return r.DBNum
}
// GetDBNum redis 数据库分区序号
func (r defaultRedisConfig) GetSentinelConfig() RedisSentinelConfig {
return r.sentinel
}
// GetEnabled redis 哨兵配置是否激活
func (s redisSentinel) GetEnabled() bool {
return s.Enabled
}
// GetMaster redis 主节点名
func (s redisSentinel) GetMaster() string {
return s.Master
}
// GetNodes redis 哨兵节点列表
func (s redisSentinel) GetNodes() []string {
if len(s.Nodes) != 0 {
for _, v := range strings.Split(s.Nodes, ",") {
v = strings.TrimSpace(v)
s.nodes = append(s.nodes, v)
}
}
return s.nodes
}