Compare commits

...

1 Commits

Author SHA1 Message Date
6f02ad60eb hot fix issue in tpl auto update 2019-02-14 13:38:52 +08:00
7 changed files with 55 additions and 7 deletions

View File

@ -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]
}

View File

@ -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) {

View File

@ -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()))

View File

@ -102,4 +102,15 @@ func ReplaceStringFunc(pattern string, src string, replaceFunc func(s string) st
return []byte(replaceFunc(string(bytes)))
})
return string(bytes), err
}
}
// 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
}

View File

@ -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()
}

View File

@ -0,0 +1 @@
hello gf!

View File

@ -0,0 +1 @@
123456