mirror of
https://gitee.com/johng/gf.git
synced 2024-12-01 11:48:09 +08:00
add Clear function for gcfg; mark Reload function of gcfg as deprecated; update unit test for gins
This commit is contained in:
parent
07476a4349
commit
47c073aaf3
@ -204,7 +204,7 @@ func Database(name...string) gdb.DB {
|
||||
}
|
||||
gdb.AddConfigGroup(group, cg)
|
||||
}
|
||||
addConfigMonitor(key)
|
||||
addConfigMonitor(key, config)
|
||||
}
|
||||
if db, err := gdb.New(name...); err == nil {
|
||||
return db
|
||||
@ -235,30 +235,30 @@ func Redis(name...string) *gredis.Redis {
|
||||
array, _ := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)\?(.+)`, line)
|
||||
if len(array) == 6 {
|
||||
parse, _ := gstr.Parse(array[5])
|
||||
config := gredis.Config{
|
||||
redisConfig := gredis.Config{
|
||||
Host : array[1],
|
||||
Port : gconv.Int(array[2]),
|
||||
Db : gconv.Int(array[3]),
|
||||
Pass : array[4],
|
||||
}
|
||||
if v, ok := parse["maxIdle"]; ok {
|
||||
config.MaxIdle = gconv.Int(v)
|
||||
redisConfig.MaxIdle = gconv.Int(v)
|
||||
}
|
||||
if v, ok := parse["maxActive"]; ok {
|
||||
config.MaxActive = gconv.Int(v)
|
||||
redisConfig.MaxActive = gconv.Int(v)
|
||||
}
|
||||
if v, ok := parse["idleTimeout"]; ok {
|
||||
config.IdleTimeout = gconv.TimeDuration(v)*time.Second
|
||||
redisConfig.IdleTimeout = gconv.TimeDuration(v)*time.Second
|
||||
}
|
||||
if v, ok := parse["maxConnLifetime"]; ok {
|
||||
config.MaxConnLifetime = gconv.TimeDuration(v)*time.Second
|
||||
redisConfig.MaxConnLifetime = gconv.TimeDuration(v)*time.Second
|
||||
}
|
||||
addConfigMonitor(key)
|
||||
return gredis.New(config)
|
||||
addConfigMonitor(key, config)
|
||||
return gredis.New(redisConfig)
|
||||
}
|
||||
array, _ = gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, line)
|
||||
if len(array) == 5 {
|
||||
addConfigMonitor(key)
|
||||
addConfigMonitor(key, config)
|
||||
return gredis.New(gredis.Config{
|
||||
Host : array[1],
|
||||
Port : gconv.Int(array[2]),
|
||||
@ -283,9 +283,9 @@ func Redis(name...string) *gredis.Redis {
|
||||
}
|
||||
|
||||
// 添加对单例对象的配置文件inotify监控
|
||||
func addConfigMonitor(key string) {
|
||||
func addConfigMonitor(key string, config *gcfg.Config) {
|
||||
// 使用gfsnotify进行文件监控,当配置文件有任何变化时,清空对象单例缓存
|
||||
if path := Config().GetFilePath(); path != "" {
|
||||
if path := config.GetFilePath(); path != "" {
|
||||
gfsnotify.Add(path, func(event *gfsnotify.Event) {
|
||||
instances.Remove(key)
|
||||
})
|
||||
|
@ -57,7 +57,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
@ -71,7 +71,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config().Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
@ -84,7 +84,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
@ -97,7 +97,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("database.default.1.host"), "127.0.0.1")
|
||||
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0")
|
||||
@ -113,7 +113,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
gtest.Assert(gins.Config().AddPath(path), nil)
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
@ -127,7 +127,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
gtest.Assert(gins.Config().AddPath(path), nil)
|
||||
gtest.Assert(gins.Config().Get("test"), "v=1")
|
||||
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.1")
|
||||
@ -141,7 +141,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config("test.toml").Reload()
|
||||
defer gins.Config("test.toml").Clear()
|
||||
|
||||
gtest.Assert(gins.Config("test.toml").AddPath(path), nil)
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
@ -156,7 +156,7 @@ test = "v=1"
|
||||
err := gfile.PutContents(file, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(file)
|
||||
defer gins.Config("test.toml").Reload()
|
||||
defer gins.Config("test.toml").Clear()
|
||||
|
||||
gtest.Assert(gins.Config("test.toml").AddPath(path), nil)
|
||||
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
|
||||
|
@ -53,7 +53,7 @@ test = "v=2"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
@ -53,7 +53,7 @@ test = "v=3"
|
||||
err := gfile.PutContents(path, config)
|
||||
gtest.Assert(err, nil)
|
||||
defer gfile.Remove(path)
|
||||
defer gins.Config().Reload()
|
||||
defer gins.Config().Clear()
|
||||
|
||||
// for gfsnotify callbacks to refresh cache of config file
|
||||
time.Sleep(500*time.Millisecond)
|
||||
|
@ -131,7 +131,7 @@ func (c *Config) SetPath(path string) error {
|
||||
// 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。
|
||||
func (c *Config) SetViolenceCheck(check bool) {
|
||||
c.vc.Set(check)
|
||||
c.Reload()
|
||||
c.Clear()
|
||||
}
|
||||
|
||||
// 添加配置管理器的配置文件搜索路径
|
||||
@ -437,8 +437,14 @@ func (c *Config) GetToStruct(pattern string, objPointer interface{}, file...stri
|
||||
return errors.New("config file not found")
|
||||
}
|
||||
|
||||
// 清空当前配置文件缓存,强制重新从磁盘文件读取配置文件内容
|
||||
func (c *Config) Reload() {
|
||||
// Deprecated. See Clear.
|
||||
func (c *Config) Clear() {
|
||||
c.jsons.Clear()
|
||||
}
|
||||
|
||||
// Clear removes all parsed configuration files content cache,
|
||||
// which will force reload configuration content from file.
|
||||
func (c *Config) Clear() {
|
||||
c.jsons.Clear()
|
||||
}
|
||||
|
||||
|
@ -32,3 +32,8 @@ func GetContent(file...string) string {
|
||||
}
|
||||
return configs.Get(name)
|
||||
}
|
||||
|
||||
// ClearContent removes all global configuration contents.
|
||||
func ClearContent() {
|
||||
configs.Clear()
|
||||
}
|
@ -88,6 +88,8 @@ array = [1,2,3]
|
||||
cache = "127.0.0.1:6379,1"
|
||||
`
|
||||
gcfg.SetContent(content)
|
||||
defer gcfg.ClearContent()
|
||||
|
||||
gtest.Case(t, func() {
|
||||
c := gcfg.New(".")
|
||||
gtest.Assert(c.Get("v1"), 1)
|
||||
|
Loading…
Reference in New Issue
Block a user