gredis_config.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gredis
  7. import (
  8. "github.com/gogf/gf/internal/intlog"
  9. "time"
  10. "github.com/gogf/gf/errors/gerror"
  11. "github.com/gogf/gf/container/gmap"
  12. "github.com/gogf/gf/text/gregex"
  13. "github.com/gogf/gf/text/gstr"
  14. "github.com/gogf/gf/util/gconv"
  15. )
  16. const (
  17. DefaultGroupName = "default" // Default configuration group name.
  18. DefaultRedisPort = 6379 // Default redis port configuration if not passed.
  19. )
  20. var (
  21. // Configuration groups.
  22. configs = gmap.NewStrAnyMap(true)
  23. )
  24. // SetConfig sets the global configuration for specified group.
  25. // If <name> is not passed, it sets configuration for the default group name.
  26. func SetConfig(config Config, name ...string) {
  27. group := DefaultGroupName
  28. if len(name) > 0 {
  29. group = name[0]
  30. }
  31. configs.Set(group, config)
  32. instances.Remove(group)
  33. intlog.Printf(`SetConfig for group "%s": %+v`, group, config)
  34. }
  35. // SetConfigByStr sets the global configuration for specified group with string.
  36. // If <name> is not passed, it sets configuration for the default group name.
  37. func SetConfigByStr(str string, name ...string) error {
  38. group := DefaultGroupName
  39. if len(name) > 0 {
  40. group = name[0]
  41. }
  42. config, err := ConfigFromStr(str)
  43. if err != nil {
  44. return err
  45. }
  46. configs.Set(group, config)
  47. instances.Remove(group)
  48. return nil
  49. }
  50. // GetConfig returns the global configuration with specified group name.
  51. // If <name> is not passed, it returns configuration of the default group name.
  52. func GetConfig(name ...string) (config Config, ok bool) {
  53. group := DefaultGroupName
  54. if len(name) > 0 {
  55. group = name[0]
  56. }
  57. if v := configs.Get(group); v != nil {
  58. return v.(Config), true
  59. }
  60. return Config{}, false
  61. }
  62. // RemoveConfig removes the global configuration with specified group.
  63. // If <name> is not passed, it removes configuration of the default group name.
  64. func RemoveConfig(name ...string) {
  65. group := DefaultGroupName
  66. if len(name) > 0 {
  67. group = name[0]
  68. }
  69. configs.Remove(group)
  70. instances.Remove(group)
  71. intlog.Printf(`RemoveConfig: %s`, group)
  72. }
  73. // ConfigFromStr parses and returns config from given str.
  74. // Eg: host:port[,db,pass?maxIdle=x&maxActive=x&idleTimeout=x&maxConnLifetime=x]
  75. func ConfigFromStr(str string) (config Config, err error) {
  76. array, _ := gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)\?(.+?)`, str)
  77. if len(array) == 6 {
  78. parse, _ := gstr.Parse(array[5])
  79. config = Config{
  80. Host: array[1],
  81. Port: gconv.Int(array[2]),
  82. Db: gconv.Int(array[3]),
  83. Pass: array[4],
  84. }
  85. if config.Port == 0 {
  86. config.Port = DefaultRedisPort
  87. }
  88. if v, ok := parse["maxIdle"]; ok {
  89. config.MaxIdle = gconv.Int(v)
  90. }
  91. if v, ok := parse["maxActive"]; ok {
  92. config.MaxActive = gconv.Int(v)
  93. }
  94. if v, ok := parse["idleTimeout"]; ok {
  95. config.IdleTimeout = gconv.Duration(v) * time.Second
  96. }
  97. if v, ok := parse["maxConnLifetime"]; ok {
  98. config.MaxConnLifetime = gconv.Duration(v) * time.Second
  99. }
  100. if v, ok := parse["tls"]; ok {
  101. config.TLS = gconv.Bool(v)
  102. }
  103. if v, ok := parse["skipVerify"]; ok {
  104. config.TLSSkipVerify = gconv.Bool(v)
  105. }
  106. return
  107. }
  108. array, _ = gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)`, str)
  109. if len(array) == 5 {
  110. config = Config{
  111. Host: array[1],
  112. Port: gconv.Int(array[2]),
  113. Db: gconv.Int(array[3]),
  114. Pass: array[4],
  115. }
  116. if config.Port == 0 {
  117. config.Port = DefaultRedisPort
  118. }
  119. } else {
  120. err = gerror.Newf(`invalid redis configuration: "%s"`, str)
  121. }
  122. return
  123. }
  124. // ClearConfig removes all configurations and instances of redis.
  125. func ClearConfig() {
  126. configs.Clear()
  127. instances.Clear()
  128. }