gcfg_config.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright GoFrame 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 gcfg
  7. import (
  8. "github.com/gogf/gf/container/gmap"
  9. "github.com/gogf/gf/internal/intlog"
  10. )
  11. var (
  12. // Customized configuration content.
  13. configs = gmap.NewStrStrMap(true)
  14. )
  15. // SetContent sets customized configuration content for specified <file>.
  16. // The <file> is unnecessary param, default is DefaultConfigFile.
  17. func SetContent(content string, file ...string) {
  18. name := DefaultConfigFile
  19. if len(file) > 0 {
  20. name = file[0]
  21. }
  22. // Clear file cache for instances which cached <name>.
  23. instances.LockFunc(func(m map[string]interface{}) {
  24. if configs.Contains(name) {
  25. for _, v := range m {
  26. v.(*Config).jsons.Remove(name)
  27. }
  28. }
  29. configs.Set(name, content)
  30. })
  31. }
  32. // GetContent returns customized configuration content for specified <file>.
  33. // The <file> is unnecessary param, default is DefaultConfigFile.
  34. func GetContent(file ...string) string {
  35. name := DefaultConfigFile
  36. if len(file) > 0 {
  37. name = file[0]
  38. }
  39. return configs.Get(name)
  40. }
  41. // RemoveContent removes the global configuration with specified <file>.
  42. // If <name> is not passed, it removes configuration of the default group name.
  43. func RemoveContent(file ...string) {
  44. name := DefaultConfigFile
  45. if len(file) > 0 {
  46. name = file[0]
  47. }
  48. // Clear file cache for instances which cached <name>.
  49. instances.LockFunc(func(m map[string]interface{}) {
  50. if configs.Contains(name) {
  51. for _, v := range m {
  52. v.(*Config).jsons.Remove(name)
  53. }
  54. configs.Remove(name)
  55. }
  56. })
  57. intlog.Printf(`RemoveContent: %s`, name)
  58. }
  59. // ClearContent removes all global configuration contents.
  60. func ClearContent() {
  61. configs.Clear()
  62. // Clear cache for all instances.
  63. instances.LockFunc(func(m map[string]interface{}) {
  64. for _, v := range m {
  65. v.(*Config).jsons.Clear()
  66. }
  67. })
  68. intlog.Print(`RemoveConfig`)
  69. }