add Clear function for gcfg; mark Reload function of gcfg as deprecated; update unit test for gins

This commit is contained in:
John 2019-04-02 16:08:46 +08:00
parent 07476a4349
commit 47c073aaf3
7 changed files with 38 additions and 25 deletions

View File

@ -204,7 +204,7 @@ func Database(name...string) gdb.DB {
} }
gdb.AddConfigGroup(group, cg) gdb.AddConfigGroup(group, cg)
} }
addConfigMonitor(key) addConfigMonitor(key, config)
} }
if db, err := gdb.New(name...); err == nil { if db, err := gdb.New(name...); err == nil {
return db return db
@ -235,30 +235,30 @@ func Redis(name...string) *gredis.Redis {
array, _ := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)\?(.+)`, line) array, _ := gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)\?(.+)`, line)
if len(array) == 6 { if len(array) == 6 {
parse, _ := gstr.Parse(array[5]) parse, _ := gstr.Parse(array[5])
config := gredis.Config{ redisConfig := gredis.Config{
Host : array[1], Host : array[1],
Port : gconv.Int(array[2]), Port : gconv.Int(array[2]),
Db : gconv.Int(array[3]), Db : gconv.Int(array[3]),
Pass : array[4], Pass : array[4],
} }
if v, ok := parse["maxIdle"]; ok { if v, ok := parse["maxIdle"]; ok {
config.MaxIdle = gconv.Int(v) redisConfig.MaxIdle = gconv.Int(v)
} }
if v, ok := parse["maxActive"]; ok { if v, ok := parse["maxActive"]; ok {
config.MaxActive = gconv.Int(v) redisConfig.MaxActive = gconv.Int(v)
} }
if v, ok := parse["idleTimeout"]; ok { 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 { if v, ok := parse["maxConnLifetime"]; ok {
config.MaxConnLifetime = gconv.TimeDuration(v)*time.Second redisConfig.MaxConnLifetime = gconv.TimeDuration(v)*time.Second
} }
addConfigMonitor(key) addConfigMonitor(key, config)
return gredis.New(config) return gredis.New(redisConfig)
} }
array, _ = gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, line) array, _ = gregex.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, line)
if len(array) == 5 { if len(array) == 5 {
addConfigMonitor(key) addConfigMonitor(key, config)
return gredis.New(gredis.Config{ return gredis.New(gredis.Config{
Host : array[1], Host : array[1],
Port : gconv.Int(array[2]), Port : gconv.Int(array[2]),
@ -283,9 +283,9 @@ func Redis(name...string) *gredis.Redis {
} }
// 添加对单例对象的配置文件inotify监控 // 添加对单例对象的配置文件inotify监控
func addConfigMonitor(key string) { func addConfigMonitor(key string, config *gcfg.Config) {
// 使用gfsnotify进行文件监控当配置文件有任何变化时清空对象单例缓存 // 使用gfsnotify进行文件监控当配置文件有任何变化时清空对象单例缓存
if path := Config().GetFilePath(); path != "" { if path := config.GetFilePath(); path != "" {
gfsnotify.Add(path, func(event *gfsnotify.Event) { gfsnotify.Add(path, func(event *gfsnotify.Event) {
instances.Remove(key) instances.Remove(key)
}) })

View File

@ -57,7 +57,7 @@ test = "v=1"
err := gfile.PutContents(path, config) err := gfile.PutContents(path, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(path) 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("test"), "v=1")
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.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") 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) err := gfile.PutContents(path, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(path) 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("test"), "v=1")
gtest.Assert(gins.Config().Get("database.default.1.host"), "127.0.0.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") 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) err := gfile.PutContents(path, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(path) 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("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("database.default.1.host"), "127.0.0.1")
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0") 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) err := gfile.PutContents(path, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(path) 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("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("database.default.1.host"), "127.0.0.1")
gtest.Assert(gins.Config("test.toml").Get("redis.disk"), "127.0.0.1:6379,0") 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) err := gfile.PutContents(file, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(file) defer gfile.Remove(file)
defer gins.Config().Reload() defer gins.Config().Clear()
gtest.Assert(gins.Config().AddPath(path), nil) gtest.Assert(gins.Config().AddPath(path), nil)
gtest.Assert(gins.Config().Get("test"), "v=1") 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("database.default.1.host"), "127.0.0.1")
@ -127,7 +127,7 @@ test = "v=1"
err := gfile.PutContents(file, config) err := gfile.PutContents(file, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(file) defer gfile.Remove(file)
defer gins.Config().Reload() defer gins.Config().Clear()
gtest.Assert(gins.Config().AddPath(path), nil) gtest.Assert(gins.Config().AddPath(path), nil)
gtest.Assert(gins.Config().Get("test"), "v=1") 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("database.default.1.host"), "127.0.0.1")
@ -141,7 +141,7 @@ test = "v=1"
err := gfile.PutContents(file, config) err := gfile.PutContents(file, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(file) 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").AddPath(path), nil)
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1") gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")
@ -156,7 +156,7 @@ test = "v=1"
err := gfile.PutContents(file, config) err := gfile.PutContents(file, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(file) 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").AddPath(path), nil)
gtest.Assert(gins.Config("test.toml").Get("test"), "v=1") gtest.Assert(gins.Config("test.toml").Get("test"), "v=1")

View File

@ -53,7 +53,7 @@ test = "v=2"
err := gfile.PutContents(path, config) err := gfile.PutContents(path, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(path) defer gfile.Remove(path)
defer gins.Config().Reload() defer gins.Config().Clear()
// for gfsnotify callbacks to refresh cache of config file // for gfsnotify callbacks to refresh cache of config file
time.Sleep(500*time.Millisecond) time.Sleep(500*time.Millisecond)

View File

@ -53,7 +53,7 @@ test = "v=3"
err := gfile.PutContents(path, config) err := gfile.PutContents(path, config)
gtest.Assert(err, nil) gtest.Assert(err, nil)
defer gfile.Remove(path) defer gfile.Remove(path)
defer gins.Config().Reload() defer gins.Config().Clear()
// for gfsnotify callbacks to refresh cache of config file // for gfsnotify callbacks to refresh cache of config file
time.Sleep(500*time.Millisecond) time.Sleep(500*time.Millisecond)

View File

@ -131,7 +131,7 @@ func (c *Config) SetPath(path string) error {
// 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。 // 开启比较耗性能,也不建议允许键名中存在分隔符,最好在应用端避免这种情况。
func (c *Config) SetViolenceCheck(check bool) { func (c *Config) SetViolenceCheck(check bool) {
c.vc.Set(check) 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") return errors.New("config file not found")
} }
// 清空当前配置文件缓存,强制重新从磁盘文件读取配置文件内容 // Deprecated. See Clear.
func (c *Config) Reload() { 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() c.jsons.Clear()
} }

View File

@ -32,3 +32,8 @@ func GetContent(file...string) string {
} }
return configs.Get(name) return configs.Get(name)
} }
// ClearContent removes all global configuration contents.
func ClearContent() {
configs.Clear()
}

View File

@ -88,6 +88,8 @@ array = [1,2,3]
cache = "127.0.0.1:6379,1" cache = "127.0.0.1:6379,1"
` `
gcfg.SetContent(content) gcfg.SetContent(content)
defer gcfg.ClearContent()
gtest.Case(t, func() { gtest.Case(t, func() {
c := gcfg.New(".") c := gcfg.New(".")
gtest.Assert(c.Get("v1"), 1) gtest.Assert(c.Get("v1"), 1)