Skip to content

Commit

Permalink
hot fix issue in tpl auto update
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Feb 14, 2019
1 parent ca6c079 commit 6f02ad6
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 7 deletions.
11 changes: 7 additions & 4 deletions g/os/gfcache/gfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
}
Expand Down
4 changes: 2 additions & 2 deletions g/os/gfcache/gfcache_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ 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
}

// 添加文件监控
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) {
Expand Down
6 changes: 6 additions & 0 deletions g/os/gview/gview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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()))
Expand Down
13 changes: 12 additions & 1 deletion g/text/gregex/gregex.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions geg/net/ghttp/server/template/tpl2/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions geg/net/ghttp/server/template/tpl2/public/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello gf!
1 change: 1 addition & 0 deletions geg/net/ghttp/server/template/tpl2/template/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
123456

0 comments on commit 6f02ad6

Please sign in to comment.