gcfg增加获取配置变量为*gvar.Var; gfsnotify修复'假删除'处理逻辑

This commit is contained in:
John
2018-10-27 16:05:36 +08:00
parent fe2bd315db
commit 2b4bc53d71
3 changed files with 15 additions and 3 deletions

View File

@ -9,6 +9,7 @@
package gcfg
import (
"gitee.com/johng/gf/g/container/gvar"
"gitee.com/johng/gf/g/os/gspath"
"gitee.com/johng/gf/g/os/gfsnotify"
"gitee.com/johng/gf/g/container/gmap"
@ -120,6 +121,14 @@ func (c *Config) Get(pattern string, file...string) interface{} {
return nil
}
// 获得配置项,返回动态变量
func (c *Config) GetVar(pattern string, file...string) *gvar.Var {
if j := c.getJson(file...); j != nil {
return gvar.New(j.Get(pattern))
}
return nil
}
// 获得一个键值对关联数组/哈希表,方便操作,不需要自己做类型转换
// 注意如果获取的值不存在或者类型与json类型不匹配那么将会返回nil
func (c *Config) GetMap(pattern string, file...string) map[string]interface{} {

View File

@ -163,7 +163,7 @@ func (w *Watcher) startWatchLoop() {
// 监听事件
case ev := <- w.watcher.Events:
//glog.Debug("gfsnotify:", ev)
//glog.Debug("gfsnotify: watch loop", ev)
w.events.Push(&Event{
Path : ev.Name,
Op : Op(ev.Op),
@ -193,18 +193,21 @@ func (w *Watcher) startEventLoop() {
go func() {
for {
if v := w.events.Pop(); v != nil {
//glog.Debug("gfsnotidy: event loop", v)
event := v.(*Event)
if event.IsRemove() {
if gfile.Exists(event.Path) {
// 如果是文件删除事件,判断该文件是否存在,如果存在,那么将此事件认为“假删除”,
// 并重新添加监控(底层fsnotify会自动删除掉监控这里重新添加回去)
w.watcher.Add(event.Path)
continue
// 修改时间操作为写入
event.Op = WRITE
} else {
// 如果是真实删除,那么递归删除监控信息
w.Remove(event.Path)
}
}
//glog.Debug("gfsnotidy: event loop callbacks", v)
callbacks := w.getCallbacks(event.Path)
// 如果创建了新的目录,那么将这个目录递归添加到监控中
if event.IsCreate() && gfile.IsDir(event.Path) {

View File

@ -6,6 +6,6 @@ import (
)
func main() {
fmt.Println(g.Config().Get("serverpath"))
fmt.Println(g.Config().GetVar("memcache.0").String())
}