github.com/gogf/gf/v2@v2.7.4/os/gcfg/gcfg_adapter_file_content.go (about) 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 7 package gcfg 8 9 import ( 10 "context" 11 12 "github.com/gogf/gf/v2/internal/intlog" 13 ) 14 15 // SetContent sets customized configuration content for specified `file`. 16 // The `file` is unnecessary param, default is DefaultConfigFile. 17 func (a *AdapterFile) SetContent(content string, fileNameOrPath ...string) { 18 var usedFileNameOrPath = DefaultConfigFileName 19 if len(fileNameOrPath) > 0 { 20 usedFileNameOrPath = fileNameOrPath[0] 21 } 22 // Clear file cache for instances which cached `name`. 23 localInstances.LockFunc(func(m map[string]interface{}) { 24 if customConfigContentMap.Contains(usedFileNameOrPath) { 25 for _, v := range m { 26 if configInstance, ok := v.(*Config); ok { 27 if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok { 28 fileConfig.jsonMap.Remove(usedFileNameOrPath) 29 } 30 } 31 } 32 } 33 customConfigContentMap.Set(usedFileNameOrPath, content) 34 }) 35 } 36 37 // GetContent returns customized configuration content for specified `file`. 38 // The `file` is unnecessary param, default is DefaultConfigFile. 39 func (a *AdapterFile) GetContent(fileNameOrPath ...string) string { 40 var usedFileNameOrPath = DefaultConfigFileName 41 if len(fileNameOrPath) > 0 { 42 usedFileNameOrPath = fileNameOrPath[0] 43 } 44 return customConfigContentMap.Get(usedFileNameOrPath) 45 } 46 47 // RemoveContent removes the global configuration with specified `file`. 48 // If `name` is not passed, it removes configuration of the default group name. 49 func (a *AdapterFile) RemoveContent(fileNameOrPath ...string) { 50 var usedFileNameOrPath = DefaultConfigFileName 51 if len(fileNameOrPath) > 0 { 52 usedFileNameOrPath = fileNameOrPath[0] 53 } 54 // Clear file cache for instances which cached `name`. 55 localInstances.LockFunc(func(m map[string]interface{}) { 56 if customConfigContentMap.Contains(usedFileNameOrPath) { 57 for _, v := range m { 58 if configInstance, ok := v.(*Config); ok { 59 if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok { 60 fileConfig.jsonMap.Remove(usedFileNameOrPath) 61 } 62 } 63 } 64 customConfigContentMap.Remove(usedFileNameOrPath) 65 } 66 }) 67 68 intlog.Printf(context.TODO(), `RemoveContent: %s`, usedFileNameOrPath) 69 } 70 71 // ClearContent removes all global configuration contents. 72 func (a *AdapterFile) ClearContent() { 73 customConfigContentMap.Clear() 74 // Clear cache for all instances. 75 localInstances.LockFunc(func(m map[string]interface{}) { 76 for _, v := range m { 77 if configInstance, ok := v.(*Config); ok { 78 if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok { 79 fileConfig.jsonMap.Clear() 80 } 81 } 82 } 83 }) 84 intlog.Print(context.TODO(), `RemoveConfig`) 85 }