improve template cache feature for gview

This commit is contained in:
John
2019-11-28 14:29:58 +08:00
parent 7e43aa6b9d
commit bb45d8d578
2 changed files with 16 additions and 7 deletions

View File

@ -44,6 +44,9 @@ func (view *View) SetConfig(config Config) error {
if len(config.Delimiters) > 1 {
view.SetDelimiters(config.Delimiters[0], config.Delimiters[1])
}
// Clear global template object cache.
// It's just cache, do not hesitate clearing it.
templates.Clear()
return nil
}
@ -199,6 +202,8 @@ func (view *View) SetDelimiters(left, right string) {
// The <name> is the function name which can be called in template content.
func (view *View) BindFunc(name string, function interface{}) {
view.funcMap[name] = function
// Clear global template object cache.
templates.Clear()
}
// BindFuncMap registers customized global template functions by map to current view object.
@ -208,6 +213,8 @@ func (view *View) BindFuncMap(funcMap FuncMap) {
for k, v := range funcMap {
view.funcMap[k] = v
}
// Clear global template object cache.
templates.Clear()
}
// SetI18n binds i18n manager to current view engine.

View File

@ -146,8 +146,9 @@ func (view *View) ParseDefault(params ...Params) (result string, err error) {
// and returns the parsed content in []byte.
func (view *View) ParseContent(content string, params ...Params) (string, error) {
err := (error)(nil)
tpl := templates.GetOrSetFuncLock(gCONTENT_TEMPLATE_NAME, func() interface{} {
return template.New(gCONTENT_TEMPLATE_NAME).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap)
key := fmt.Sprintf("%s_%v", gCONTENT_TEMPLATE_NAME, view.delimiters)
tpl := templates.GetOrSetFuncLock(key, func() interface{} {
return template.New(key).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap)
}).(*template.Template)
// Using memory lock to ensure concurrent safety for content parsing.
hash := strconv.FormatUint(ghash.DJBHash64([]byte(content)), 10)
@ -202,8 +203,9 @@ func (view *View) ParseContent(content string, params ...Params) (string, error)
// with the same given <path>. It will also automatically refresh the template cache
// if the template files under <path> changes (recursively).
func (view *View) getTemplate(path string, pattern string) (tpl *template.Template, err error) {
r := templates.GetOrSetFuncLock(path, func() interface{} {
tpl = template.New(path).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap)
key := fmt.Sprintf("%s_%v", path, view.delimiters)
result := templates.GetOrSetFuncLock(key, func() interface{} {
tpl = template.New(key).Delims(view.delimiters[0], view.delimiters[1]).Funcs(view.funcMap)
// Firstly checking the resource manager.
if !gres.IsEmpty() {
if files := gres.ScanDirFile(path, pattern, true); len(files) > 0 {
@ -219,7 +221,7 @@ func (view *View) getTemplate(path string, pattern string) (tpl *template.Templa
}
// Secondly checking the file system.
files := ([]string)(nil)
var files []string
files, err = gfile.ScanDir(path, pattern, true)
if err != nil {
return nil
@ -229,8 +231,8 @@ func (view *View) getTemplate(path string, pattern string) (tpl *template.Templa
}
return tpl
})
if r != nil {
return r.(*template.Template), nil
if result != nil {
return result.(*template.Template), nil
}
return
}