redis.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. "fmt"
  6. "smart-enterprise-management/config"
  7. "github.com/jaryhe/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.SmartEnterpriseManagement.RedisDb
  36. if db < 0 || db > 15 {
  37. db = conf.Redis.DB
  38. }
  39. fmt.Println("db :", db, conf.Redis)
  40. // 连接redis
  41. cache.Setup(conf.Redis.Addrs,
  42. conf.Redis.Password,
  43. db,
  44. conf.Redis.PoolSize,
  45. conf.Redis.MinIdleConns,
  46. conf.Redis.MaxRetries,
  47. conf.Redis.Cluster)
  48. if conf.RunMode != "prod" {
  49. // open 'DEBUG' switcher
  50. }
  51. redisConfig = conf.Redis
  52. }