redis.go 1.3 KB

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