github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  package gcfg
     8  
     9  import (
    10  	"context"
    11  
    12  	"github.com/wangyougui/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, file ...string) {
    18  	name := DefaultConfigFileName
    19  	if len(file) > 0 {
    20  		name = file[0]
    21  	}
    22  	// Clear file cache for instances which cached `name`.
    23  	localInstances.LockFunc(func(m map[string]interface{}) {
    24  		if customConfigContentMap.Contains(name) {
    25  			for _, v := range m {
    26  				if configInstance, ok := v.(*Config); ok {
    27  					if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok {
    28  						fileConfig.jsonMap.Remove(name)
    29  					}
    30  				}
    31  			}
    32  		}
    33  		customConfigContentMap.Set(name, 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(file ...string) string {
    40  	name := DefaultConfigFileName
    41  	if len(file) > 0 {
    42  		name = file[0]
    43  	}
    44  	return customConfigContentMap.Get(name)
    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(file ...string) {
    50  	name := DefaultConfigFileName
    51  	if len(file) > 0 {
    52  		name = file[0]
    53  	}
    54  	// Clear file cache for instances which cached `name`.
    55  	localInstances.LockFunc(func(m map[string]interface{}) {
    56  		if customConfigContentMap.Contains(name) {
    57  			for _, v := range m {
    58  				if configInstance, ok := v.(*Config); ok {
    59  					if fileConfig, ok := configInstance.GetAdapter().(*AdapterFile); ok {
    60  						fileConfig.jsonMap.Remove(name)
    61  					}
    62  				}
    63  			}
    64  			customConfigContentMap.Remove(name)
    65  		}
    66  	})
    67  
    68  	intlog.Printf(context.TODO(), `RemoveContent: %s`, name)
    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  }