mirror of
https://gitee.com/johng/gf
synced 2026-06-07 10:22:11 +08:00
gins单例管理增加配置文件监控功能
This commit is contained in:
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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"))
|
||||
}
|
||||
Reference in New Issue
Block a user