改进gfpool

This commit is contained in:
john
2018-09-25 17:25:38 +08:00
parent 6d8e053635
commit 91b316eabf
3 changed files with 17 additions and 3 deletions

View File

@ -86,8 +86,10 @@ func OpenWithFlag(path string, flag int) (*os.File, error) {
// 判断所给路径文件/文件夹是否存在
func Exists(path string) bool {
_, err := os.Stat(path)
return os.IsExist(err)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return true
}
return false
}
// 判断所给路径是否为文件夹

View File

@ -15,6 +15,7 @@ import (
"gitee.com/johng/gf/g/os/gfile"
"gitee.com/johng/gf/g/container/gmap"
"gitee.com/johng/gf/g/os/gfsnotify"
"gitee.com/johng/gf/g/os/glog"
)
// 文件目录搜索管理对象
@ -41,12 +42,15 @@ func (sp *SPath) Set(path string) error {
}
}
if r != "" && gfile.IsDir(r) {
r = strings.TrimRight(r, gfile.Separator)
sp.mu.Lock()
sp.paths = []string{strings.TrimRight(r, gfile.Separator)}
sp.paths = []string{r}
sp.mu.Unlock()
sp.cache.Clear()
glog.Debug("gspath.SetPath: ", r)
return nil
}
glog.Debug("gspath.SetPath failed: ", path)
return errors.New("invalid path:" + path)
}
@ -60,11 +64,14 @@ func (sp *SPath) Add(path string) error {
}
}
if r != "" && gfile.IsDir(r) {
r = strings.TrimRight(r, gfile.Separator)
sp.mu.Lock()
sp.paths = append(sp.paths, r)
sp.mu.Unlock()
glog.Debug("gspath.Add: ", r)
return nil
}
glog.Debug("gspath.Add failed: ", path)
return errors.New("invalid path:" + path)
}
@ -100,6 +107,7 @@ func (sp *SPath) Size() int {
// 添加文件监控,当文件删除时,同时也删除搜索结果缓存
func (sp *SPath) addMonitor(name, path string) {
gfsnotify.Add(path, func(event *gfsnotify.Event) {
glog.Debug("gspath.monitor:", event)
if event.IsRemove() {
sp.cache.Remove(name)
}

View File

@ -3,9 +3,13 @@ package main
import (
"gitee.com/johng/gf/g/os/glog"
"time"
"gitee.com/johng/gf/g"
)
func main() {
g.Config().AddPath("eeee")
g.Config().AddPath(".")
glog.SetPath(g.Config().GetString("logPath"))
glog.SetPath("/tmp/test-logs")
for {
glog.Println("1")