-
Notifications
You must be signed in to change notification settings - Fork 3
/
gcfg_config.go
71 lines (64 loc) · 1.9 KB
/
gcfg_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2019 gf Author(https://github.com/snail007/gf). 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/snail007/gf.
package gcfg
import "github.com/snail007/gf/container/gmap"
var (
// Customized configuration content.
configs = gmap.NewStrStrMap(true)
)
// SetContent sets customized configuration content for specified <file>.
// The <file> is unnecessary param, default is DEFAULT_CONFIG_FILE.
func SetContent(content string, file ...string) {
name := DEFAULT_CONFIG_FILE
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 {
v.(*Config).jsons.Remove(name)
}
}
configs.Set(name, content)
})
}
// GetContent returns customized configuration content for specified <file>.
// The <file> is unnecessary param, default is DEFAULT_CONFIG_FILE.
func GetContent(file ...string) string {
name := DEFAULT_CONFIG_FILE
if len(file) > 0 {
name = file[0]
}
return configs.Get(name)
}
// RemoveConfig removes the global configuration with specified <file>.
// If <name> is not passed, it removes configuration of the default group name.
func RemoveConfig(file ...string) {
name := DEFAULT_CONFIG_FILE
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 {
v.(*Config).jsons.Remove(name)
}
configs.Remove(name)
}
})
}
// ClearContent removes all global configuration contents.
func ClearContent() {
configs.Clear()
// Clear cache for all instances.
instances.LockFunc(func(m map[string]interface{}) {
for _, v := range m {
v.(*Config).jsons.Clear()
}
})
}