增加redis配置项,可通过配置文件配置指定的redis库

This commit is contained in:
wulorn 2020-03-25 16:36:27 +08:00
parent 3e8a6946e0
commit 76fd7b38df
5 changed files with 24 additions and 0 deletions

View File

@ -6,6 +6,7 @@ query:
redis:
addrs:
- 127.0.0.1:6379
db: 0
pass: ""
# timeout:
# conn: 500

View File

@ -52,6 +52,7 @@ link:
# for alarm event and message queue
redis:
addr: "127.0.0.1:6379"
db: 0
pass: ""
# in ms
# timeout:

View File

@ -15,6 +15,7 @@ var Config RedisSection
type RedisSection struct {
Addrs []string `yaml:"addrs"`
Pass string `yaml:"pass"`
DB int `yaml:"db"`
Idle int `yaml:"idle"`
Timeout TimeoutSection `yaml:"timeout"`
Prefix string `yaml:"prefix"`
@ -31,6 +32,7 @@ func Init(cfg RedisSection) {
addrs := cfg.Addrs
pass := cfg.Pass
db := cfg.DB
maxIdle := cfg.Idle
idleTimeout := 240 * time.Second
@ -59,6 +61,15 @@ func Init(cfg RedisSection) {
}
}
if db != 0 {
if _, err := c.Do("SELECT", db); err != nil {
c.Close()
logger.Error("redis select db fail, db: ", db)
stats.Counter.Set("redis.conn.failed", 1)
return nil, err
}
}
return c, err
},
TestOnBorrow: PingRedis,

View File

@ -43,6 +43,7 @@ type cleanerSection struct {
type redisSection struct {
Addr string `yaml:"addr"`
Pass string `yaml:"pass"`
DB int `yaml:"db"`
Idle int `yaml:"idle"`
Timeout timeoutSection `yaml:"timeout"`
}

View File

@ -17,6 +17,7 @@ func InitRedis() {
addr := cfg.Redis.Addr
pass := cfg.Redis.Pass
db := cfg.Redis.DB
maxIdle := cfg.Redis.Idle
idleTimeout := 240 * time.Second
@ -44,6 +45,15 @@ func InitRedis() {
}
}
if db != 0 {
if _, err := c.Do("SELECT", db); err != nil {
c.Close()
logger.Error("redis select db fail, db: ", db)
stats.Counter.Set("redis.conn.failed", 1)
return nil, err
}
}
return c, err
},
TestOnBorrow: PingRedis,