From 47c073aaf338ff3a561ac6f2b491e664bb4442e6 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 2 Apr 2019 16:08:46 +0800 Subject: [PATCH] add Clear function for gcfg; mark Reload function of gcfg as deprecated; update unit test for gins --- g/frame/gins/gins.go | 24 ++++++++++++------------ g/frame/gins/gins_config_test.go | 16 ++++++++-------- g/frame/gins/gins_database_test.go | 2 +- g/frame/gins/gins_redis_test.go | 2 +- g/os/gcfg/gcfg.go | 12 +++++++++--- g/os/gcfg/gcfg_config.go | 5 +++++ g/os/gcfg/gcfg_z_unit_test.go | 2 ++ 7 files changed, 38 insertions(+), 25 deletions(-) diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index d9966b973..34720e475 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -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 @@ -234,31 +234,31 @@ func Redis(name...string) *gredis.Redis { line := gconv.String(v) array, _ := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)\?(.+)`, line) if len(array) == 6 { - parse, _ := gstr.Parse(array[5]) - config := gredis.Config{ + parse, _ := gstr.Parse(array[5]) + 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) }) diff --git a/g/frame/gins/gins_config_test.go b/g/frame/gins/gins_config_test.go index aa31b353f..1bd96d29b 100644 --- a/g/frame/gins/gins_config_test.go +++ b/g/frame/gins/gins_config_test.go @@ -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") diff --git a/g/frame/gins/gins_database_test.go b/g/frame/gins/gins_database_test.go index f91bbb49e..26ec17c10 100644 --- a/g/frame/gins/gins_database_test.go +++ b/g/frame/gins/gins_database_test.go @@ -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) diff --git a/g/frame/gins/gins_redis_test.go b/g/frame/gins/gins_redis_test.go index 5bcc02214..7490c9e94 100644 --- a/g/frame/gins/gins_redis_test.go +++ b/g/frame/gins/gins_redis_test.go @@ -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) diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 0762c9781..6b12987e7 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -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() } diff --git a/g/os/gcfg/gcfg_config.go b/g/os/gcfg/gcfg_config.go index 16fe3b9c6..196fa20be 100644 --- a/g/os/gcfg/gcfg_config.go +++ b/g/os/gcfg/gcfg_config.go @@ -31,4 +31,9 @@ func GetContent(file...string) string { name = file[0] } return configs.Get(name) +} + +// ClearContent removes all global configuration contents. +func ClearContent() { + configs.Clear() } \ No newline at end of file diff --git a/g/os/gcfg/gcfg_z_unit_test.go b/g/os/gcfg/gcfg_z_unit_test.go index 0f1d7c2c2..8331903f1 100644 --- a/g/os/gcfg/gcfg_z_unit_test.go +++ b/g/os/gcfg/gcfg_z_unit_test.go @@ -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)