redis.go 1.4 KB

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