12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- // Copyright 2019 github.com. All rights reserved.
- // Use of this source code is governed by github.com.
- package parser
- import (
- "property-device-gateway/config"
- "git.getensh.com/common/gopkgs/cache"
- )
- var redisConfig config.RedisConfig
- func RedisHandler(conf *config.Configure) {
- // redisConfig.Addr 不为空表示已经初始化
- if len(redisConfig.Addrs) != 0 {
- // 地址或密码变化,关闭当前连接,重新连接
- if len(redisConfig.Addrs) != len(conf.Redis.Addrs) ||
- redisConfig.Password != conf.Redis.Password {
- cache.Close()
- } else {
- // 判断addrs数组是否相同
- addrs := map[string]bool{}
- for _, v := range redisConfig.Addrs {
- addrs[v] = true
- }
- for _, v := range conf.Redis.Addrs {
- addrs[v] = true
- }
- // 地址和密码不变返回
- if len(redisConfig.Addrs) == len(addrs) {
- return
- }
- // 关闭cache
- cache.Close()
- }
- }
- // 私有db不合法使用公共db
- db := conf.PropertyDeviceGateway.RedisDb
- if db < 0 || db > 15 {
- db = conf.Redis.DB
- }
- // 连接redis
- cache.Setup(conf.Redis.Addrs,
- conf.Redis.Password,
- db,
- conf.Redis.PoolSize,
- conf.Redis.MinIdleConns,
- conf.Redis.MaxRetries,
- conf.Redis.Cluster)
- if conf.RunMode != "prod" {
- // open 'DEBUG' switcher
- }
- redisConfig = conf.Redis
- }
|