github.com/zhongdalu/gf@v1.0.0/g/os/gcfg/gcfg_config.go (about)

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