redis.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package parser
  4. import (
  5. "xingjia-management-gateway/config"
  6. "git.getensh.com/common/gopkgs/cache"
  7. )
  8. var redisConfig config.RedisConfig
  9. func RedisHandler(conf *config.Configure) {
  10. // redisConfig.Addr 不为空表示已经初始化
  11. if len(redisConfig.Addrs) != 0 {
  12. // 地址或密码变化,关闭当前连接,重新连接
  13. if len(redisConfig.Addrs) != len(conf.Redis.Addrs) ||
  14. redisConfig.Password != conf.Redis.Password {
  15. cache.Close()
  16. } else {
  17. // 判断addrs数组是否相同
  18. addrs := map[string]bool{}
  19. for _, v := range redisConfig.Addrs {
  20. addrs[v] = true
  21. }
  22. for _, v := range conf.Redis.Addrs {
  23. addrs[v] = true
  24. }
  25. // 地址和密码不变返回
  26. if len(redisConfig.Addrs) == len(addrs) {
  27. return
  28. }
  29. // 关闭cache
  30. cache.Close()
  31. }
  32. }
  33. // 私有db不合法使用公共db
  34. db := conf.XingjiaManagementGateway.RedisDb
  35. if db < 0 || db > 15 {
  36. db = conf.Redis.DB
  37. }
  38. // 连接redis
  39. cache.Setup(conf.Redis.Addrs,
  40. conf.Redis.Password,
  41. db,
  42. conf.Redis.PoolSize,
  43. conf.Redis.MinIdleConns,
  44. conf.Redis.MaxRetries,
  45. conf.Redis.Cluster)
  46. if conf.RunMode != "prod" {
  47. // open 'DEBUG' switcher
  48. }
  49. redisConfig = conf.Redis
  50. }