gredis_config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright GoFrame Author(https://goframe.org). 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. "context"
  9. "github.com/gogf/gf/errors/gcode"
  10. "github.com/gogf/gf/errors/gerror"
  11. "github.com/gogf/gf/internal/intlog"
  12. "github.com/gogf/gf/container/gmap"
  13. "github.com/gogf/gf/text/gregex"
  14. "github.com/gogf/gf/text/gstr"
  15. "github.com/gogf/gf/util/gconv"
  16. )
  17. const (
  18. DefaultGroupName = "default" // Default configuration group name.
  19. DefaultRedisPort = 6379 // Default redis port configuration if not passed.
  20. )
  21. var (
  22. // Configuration groups.
  23. configs = gmap.NewStrAnyMap(true)
  24. )
  25. // SetConfig sets the global configuration for specified group.
  26. // If <name> is not passed, it sets configuration for the default group name.
  27. func SetConfig(config *Config, name ...string) {
  28. group := DefaultGroupName
  29. if len(name) > 0 {
  30. group = name[0]
  31. }
  32. configs.Set(group, config)
  33. instances.Remove(group)
  34. intlog.Printf(context.TODO(), `SetConfig for group "%s": %+v`, group, config)
  35. }
  36. // SetConfigByStr sets the global configuration for specified group with string.
  37. // If <name> is not passed, it sets configuration for the default group name.
  38. func SetConfigByStr(str string, name ...string) error {
  39. group := DefaultGroupName
  40. if len(name) > 0 {
  41. group = name[0]
  42. }
  43. config, err := ConfigFromStr(str)
  44. if err != nil {
  45. return err
  46. }
  47. configs.Set(group, config)
  48. instances.Remove(group)
  49. return nil
  50. }
  51. // GetConfig returns the global configuration with specified group name.
  52. // If <name> is not passed, it returns configuration of the default group name.
  53. func GetConfig(name ...string) (config *Config, ok bool) {
  54. group := DefaultGroupName
  55. if len(name) > 0 {
  56. group = name[0]
  57. }
  58. if v := configs.Get(group); v != nil {
  59. return v.(*Config), true
  60. }
  61. return &Config{}, false
  62. }
  63. // RemoveConfig removes the global configuration with specified group.
  64. // If <name> is not passed, it removes configuration of the default group name.
  65. func RemoveConfig(name ...string) {
  66. group := DefaultGroupName
  67. if len(name) > 0 {
  68. group = name[0]
  69. }
  70. configs.Remove(group)
  71. instances.Remove(group)
  72. intlog.Printf(context.TODO(), `RemoveConfig: %s`, group)
  73. }
  74. // ConfigFromStr parses and returns config from given str.
  75. // Eg: host:port[,db,pass?maxIdle=x&maxActive=x&idleTimeout=x&maxConnLifetime=x]
  76. func ConfigFromStr(str string) (config *Config, err error) {
  77. array, _ := gregex.MatchString(`^([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)\?(.+)$`, str)
  78. if len(array) == 6 {
  79. parse, _ := gstr.Parse(array[5])
  80. config = &Config{
  81. Host: array[1],
  82. Port: gconv.Int(array[2]),
  83. Db: gconv.Int(array[3]),
  84. Pass: array[4],
  85. }
  86. if config.Port == 0 {
  87. config.Port = DefaultRedisPort
  88. }
  89. if err = gconv.Struct(parse, config); err != nil {
  90. return nil, err
  91. }
  92. return
  93. }
  94. array, _ = gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)`, str)
  95. if len(array) == 5 {
  96. config = &Config{
  97. Host: array[1],
  98. Port: gconv.Int(array[2]),
  99. Db: gconv.Int(array[3]),
  100. Pass: array[4],
  101. }
  102. if config.Port == 0 {
  103. config.Port = DefaultRedisPort
  104. }
  105. } else {
  106. err = gerror.NewCodef(gcode.CodeInvalidConfiguration, `invalid redis configuration: "%s"`, str)
  107. }
  108. return
  109. }
  110. // ClearConfig removes all configurations and instances of redis.
  111. func ClearConfig() {
  112. configs.Clear()
  113. instances.Clear()
  114. }