diff --git a/g/os/gfcache/gfcache.go b/g/os/gfcache/gfcache.go index 332f873e077..a62e460b257 100644 --- a/g/os/gfcache/gfcache.go +++ b/g/os/gfcache/gfcache.go @@ -12,6 +12,7 @@ package gfcache import ( "github.com/gogf/gf/g/container/gmap" "github.com/gogf/gf/g/container/gtype" + "github.com/gogf/gf/g/internal/cmdenv" ) type Cache struct { @@ -21,17 +22,19 @@ type Cache struct { } const ( - // 默认的缓存容量(不限制) - gDEFAULT_CACHE_CAP = 0 + // 默认的缓存容量(10MB) + gDEFAULT_CACHE_CAP = 10*1024*1024 ) var ( + // 默认的缓存容量 + cacheCap = cmdenv.Get("gf.gfcache.cap", gDEFAULT_CACHE_CAP).Int() // 默认的文件缓存对象 - cache = New() + cache = New() ) func New(cap ... int) *Cache { - c := gDEFAULT_CACHE_CAP + c := cacheCap if len(cap) > 0 { c = cap[0] } diff --git a/g/os/gfcache/gfcache_cache.go b/g/os/gfcache/gfcache_cache.go index 72e7bff5c37..315d0c6acfc 100644 --- a/g/os/gfcache/gfcache_cache.go +++ b/g/os/gfcache/gfcache_cache.go @@ -41,8 +41,8 @@ func (c *Cache) GetBinContents(path string) []byte { // 读取到内容,并且没有超过缓存容量限制时才会执行缓存 if len(b) > 0 && (c.cap.Val() == 0 || c.size.Val() < c.cap.Val()) { c.size.Add(len(b)) - c.cache.Set(path, b) c.addMonitor(path) + c.cache.Set(path, b) } return b } @@ -50,7 +50,7 @@ func (c *Cache) GetBinContents(path string) []byte { // 添加文件监控 func (c *Cache) addMonitor(path string) { // 防止多goroutine同时调用 - if c.cache.Get(path) != nil { + if c.cache.Contains(path) { return } gfsnotify.Add(path, func(event *gfsnotify.Event) { diff --git a/g/os/gview/gview.go b/g/os/gview/gview.go index a6cba4cc8a7..ba813bcd13a 100644 --- a/g/os/gview/gview.go +++ b/g/os/gview/gview.go @@ -107,6 +107,9 @@ func New(path...string) *View { // 设置模板目录绝对路径 func (view *View) SetPath(path string) error { realPath := gfile.RealPath(path) + if realPath == "" { + realPath = gfile.RealPath(gfile.MainPkgPath() + gfile.Separator + path) + } if realPath == "" { err := errors.New(fmt.Sprintf(`path "%s" does not exist`, path)) glog.Error(fmt.Sprintf(`[gview] SetPath failed: %s`, err.Error())) @@ -121,6 +124,9 @@ func (view *View) SetPath(path string) error { // 添加模板目录搜索路径 func (view *View) AddPath(path string) error { realPath := gfile.RealPath(path) + if realPath == "" { + realPath = gfile.RealPath(gfile.MainPkgPath() + gfile.Separator + path) + } if realPath == "" { err := errors.New(fmt.Sprintf(`path "%s" does not exist`, path)) glog.Error(fmt.Sprintf(`[gview] AddPath failed: %s`, err.Error())) diff --git a/g/text/gregex/gregex.go b/g/text/gregex/gregex.go index f956419ed81..ea51489be8e 100644 --- a/g/text/gregex/gregex.go +++ b/g/text/gregex/gregex.go @@ -102,4 +102,15 @@ func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) st return []byte(replaceFunc(string(bytes))) }) return string(bytes), err -} \ No newline at end of file +} + +// Split slices s into substrings separated by the expression and returns a slice of +// the substrings between those expression matches. +// +// 通过一个正则表达式分隔字符串. +func Split(pattern string, src string) []string { + if r, err := getRegexp(pattern); err == nil { + return r.Split(src, -1) + } + return nil +} diff --git a/geg/net/ghttp/server/template/tpl2/main.go b/geg/net/ghttp/server/template/tpl2/main.go new file mode 100644 index 00000000000..f4b315f4401 --- /dev/null +++ b/geg/net/ghttp/server/template/tpl2/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "github.com/gogf/gf/g" + "github.com/gogf/gf/g/frame/gins" + "github.com/gogf/gf/g/net/ghttp" +) + +func main() { + s := g.Server() + s.SetServerRoot("public") + s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER) + s.SetErrorLogEnabled(true) + s.SetAccessLogEnabled(true) + s.SetPort(2333) + + v := g.View() + v.AddPath("template") + + s.BindHandler("/", func(r *ghttp.Request) { + content, _ := gins.View().Parse("test.html", nil) + r.Response.Write(content) + }) + + s.Run() +} diff --git a/geg/net/ghttp/server/template/tpl2/public/test.html b/geg/net/ghttp/server/template/tpl2/public/test.html new file mode 100644 index 00000000000..29c9de3286b --- /dev/null +++ b/geg/net/ghttp/server/template/tpl2/public/test.html @@ -0,0 +1 @@ +hello gf! \ No newline at end of file diff --git a/geg/net/ghttp/server/template/tpl2/template/test.html b/geg/net/ghttp/server/template/tpl2/template/test.html new file mode 100644 index 00000000000..4632e068d58 --- /dev/null +++ b/geg/net/ghttp/server/template/tpl2/template/test.html @@ -0,0 +1 @@ +123456 \ No newline at end of file