diff --git a/g/frame/gins/gins.go b/g/frame/gins/gins.go index 688c8a1ab..67706c5b9 100644 --- a/g/frame/gins/gins.go +++ b/g/frame/gins/gins.go @@ -10,16 +10,17 @@ package gins import ( "strconv" + "strings" "gitee.com/johng/gf/g/os/gcfg" "gitee.com/johng/gf/g/os/gcmd" "gitee.com/johng/gf/g/os/genv" "gitee.com/johng/gf/g/os/gview" "gitee.com/johng/gf/g/os/gfile" + "gitee.com/johng/gf/g/util/gconv" + "gitee.com/johng/gf/g/os/gfsnotify" "gitee.com/johng/gf/g/database/gdb" "gitee.com/johng/gf/g/container/gmap" "gitee.com/johng/gf/g/database/gredis" - "strings" - "gitee.com/johng/gf/g/util/gconv" ) const ( @@ -163,9 +164,14 @@ func Database(name...string) *gdb.Db { } } } - - if db, err := gdb.Instance(name...); err == nil { + // 这里不能用Instance方法,否则无法自动检测更新 + if db, err := gdb.New(name...); err == nil { Set(dbCacheKey, db) + // 监控配置变化,一单有变化马上清空单例对象,下一次重新获取 + // 无法对特定操作进行判断,不同编辑器的行为引起的操作回调提醒不一样 + gfsnotify.Add(config.GetFilePath(), func(event *gfsnotify.Event) { + Set(dbCacheKey, nil) + }) return db } else { return nil @@ -197,6 +203,11 @@ func Redis(name...string) *gredis.Redis { if len(array) > 1 { redis := gredis.New(array[0], array[1]) Set(redisCacheKey, redis) + // 监控配置变化,一单有变化马上清空单例对象,下一次重新获取 + // 无法对特定操作进行判断,不同编辑器的行为引起的操作回调提醒不一样 + gfsnotify.Add(config.GetFilePath(), func(event *gfsnotify.Event) { + Set(redisCacheKey, nil) + }) return redis } } diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 7d3a5803f..1c0444be8 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -59,11 +59,20 @@ func (c *Config) SetPath(path string) { } } -// 设置配置管理器的配置文件存放目录绝对路径 +// 获取配置管理器的配置文件存放目录绝对路径 func (c *Config) GetPath() string { return c.path.Val() } +// 获取指定文件的绝对路径,默认获取默认的配置文件路径 +func (c *Config) GetFilePath(name...string) string { + path := strings.TrimRight(c.path.Val(), gfile.Separator) + gfile.Separator + if len(name) > 0 { + return path + name[0] + } + return path + gDEFAULT_CONFIG_FILE +} + // 添加配置文件到配置管理器中,第二个参数为非必须,如果不输入表示添加进入默认的配置名称中 func (c *Config) getJson(file []string) *gjson.Json { fpath := c.filePath(file) diff --git a/geg/database/redis/gredis_gins.go b/geg/database/redis/gredis_gins.go index a329a282e..43df80cca 100644 --- a/geg/database/redis/gredis_gins.go +++ b/geg/database/redis/gredis_gins.go @@ -8,6 +8,7 @@ import ( func main() { gins.Config().SetPath("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame") + redis := gins.Redis("cache") redis.Do("SET", "k", "v") v, _ := redis.Do("GET", "k") diff --git a/geg/frame/config.yml b/geg/frame/config.yml index 406c0d8ab..5f48c0d83 100644 --- a/geg/frame/config.yml +++ b/geg/frame/config.yml @@ -24,4 +24,4 @@ database: # Redis数据库配置 redis: disk: 127.0.0.1:6379,0 - cache: 127.0.0.1:6379,1 + cache: 127.0.0.1:6379,2 diff --git a/geg/os/gfsnotify/gfsnotify.go b/geg/os/gfsnotify/gfsnotify.go index b33b6cf98..0ab4144e7 100644 --- a/geg/os/gfsnotify/gfsnotify.go +++ b/geg/os/gfsnotify/gfsnotify.go @@ -6,7 +6,7 @@ import ( ) func main() { - err := gfsnotify.Add("/home/john/Documents/temp", func(event *gfsnotify.Event) { + err := gfsnotify.Add("/home/john/Documents/temp.txt", func(event *gfsnotify.Event) { if event.IsCreate() { log.Println("创建文件 : ", event.Path) } diff --git a/geg/other/test.go b/geg/other/test.go index b20f9b843..1ae0bf7a4 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,9 +2,9 @@ package main import ( "fmt" - "strconv" + "gitee.com/johng/gf/g/frame/gins" ) func main() { - fmt.Println(strconv.Atoi("11")) + fmt.Println(gins.Config().GetString("database.default.0.host")) } \ No newline at end of file