gf/os/gcfg/gcfg_config.go

79 lines
2.0 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcfg
import (
"github.com/gogf/gf/container/gmap"
"github.com/gogf/gf/internal/intlog"
)
var (
2019-06-19 09:06:52 +08:00
// Customized configuration content.
configs = gmap.NewStrStrMap(true)
)
// SetContent sets customized configuration content for specified <file>.
// The <file> is unnecessary param, default is DefaultConfigFile.
func SetContent(content string, file ...string) {
name := DefaultConfigFile
2019-06-19 09:06:52 +08:00
if len(file) > 0 {
name = file[0]
}
// Clear file cache for instances which cached <name>.
instances.LockFunc(func(m map[string]interface{}) {
if configs.Contains(name) {
for _, v := range m {
2021-01-27 00:55:45 +08:00
v.(*Config).jsonMap.Remove(name)
2019-06-19 09:06:52 +08:00
}
}
configs.Set(name, content)
})
}
// GetContent returns customized configuration content for specified <file>.
// The <file> is unnecessary param, default is DefaultConfigFile.
func GetContent(file ...string) string {
name := DefaultConfigFile
2019-06-19 09:06:52 +08:00
if len(file) > 0 {
name = file[0]
}
return configs.Get(name)
}
// RemoveContent removes the global configuration with specified <file>.
// If <name> is not passed, it removes configuration of the default group name.
func RemoveContent(file ...string) {
name := DefaultConfigFile
if len(file) > 0 {
name = file[0]
}
// Clear file cache for instances which cached <name>.
instances.LockFunc(func(m map[string]interface{}) {
if configs.Contains(name) {
for _, v := range m {
2021-01-27 00:55:45 +08:00
v.(*Config).jsonMap.Remove(name)
}
configs.Remove(name)
}
})
intlog.Printf(`RemoveContent: %s`, name)
}
// ClearContent removes all global configuration contents.
func ClearContent() {
2019-06-19 09:06:52 +08:00
configs.Clear()
// Clear cache for all instances.
instances.LockFunc(func(m map[string]interface{}) {
for _, v := range m {
2021-01-27 00:55:45 +08:00
v.(*Config).jsonMap.Clear()
}
})
intlog.Print(`RemoveConfig`)
2019-06-19 09:06:52 +08:00
}