From 2b4bc53d71123d86f5da2f357cc5ec4372dc2a41 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 27 Oct 2018 16:05:36 +0800 Subject: [PATCH] =?UTF-8?q?gcfg=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E5=8F=98=E9=87=8F=E4=B8=BA*gvar.Var;=20gfsno?= =?UTF-8?q?tify=E4=BF=AE=E5=A4=8D'=E5=81=87=E5=88=A0=E9=99=A4'=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gcfg/gcfg.go | 9 +++++++++ g/os/gfsnotify/gfsnotify.go | 7 +++++-- geg/os/gcfg/gcfg3.go | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 622cfcf98..256e2b038 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -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{} { diff --git a/g/os/gfsnotify/gfsnotify.go b/g/os/gfsnotify/gfsnotify.go index 44bb1923f..8c0263ccf 100644 --- a/g/os/gfsnotify/gfsnotify.go +++ b/g/os/gfsnotify/gfsnotify.go @@ -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) { diff --git a/geg/os/gcfg/gcfg3.go b/geg/os/gcfg/gcfg3.go index a6b690f34..6b8522504 100644 --- a/geg/os/gcfg/gcfg3.go +++ b/geg/os/gcfg/gcfg3.go @@ -6,6 +6,6 @@ import ( ) func main() { - fmt.Println(g.Config().Get("serverpath")) + fmt.Println(g.Config().GetVar("memcache.0").String()) }