From 1f46c1d231dfe36543e99d3a28ceb87b0fefae4a Mon Sep 17 00:00:00 2001 From: JessonChan Date: Fri, 4 Mar 2016 12:00:43 +0800 Subject: [PATCH] add template read lock when dev mode --- controller.go | 33 ++++++++++++++++++--------------- template.go | 27 +++++++++++++++++---------- template_test.go | 8 ++++---- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/controller.go b/controller.go index a2943d42..54da81ec 100644 --- a/controller.go +++ b/controller.go @@ -199,6 +199,7 @@ func (c *Controller) RenderString() (string, error) { func (c *Controller) RenderBytes() ([]byte, error) { //if the controller has set layout, then first get the tplname's content set the content to the layout var buf bytes.Buffer + var err error if c.Layout != "" { if c.TplName == "" { c.TplName = strings.ToLower(c.controllerName) + "/" + strings.ToLower(c.actionName) + "." + c.TplExt @@ -216,13 +217,14 @@ func (c *Controller) RenderBytes() ([]byte, error) { } BuildTemplate(BConfig.WebConfig.ViewsPath, buildFiles...) } - if _, ok := BeeTemplates[c.TplName]; !ok { + if t := getTemplateByName(c.TplName); t == nil { panic("can't find templatefile in the path:" + c.TplName) - } - err := BeeTemplates[c.TplName].ExecuteTemplate(&buf, c.TplName, c.Data) - if err != nil { - Trace("template Execute err:", err) - return nil, err + } else { + err = t.ExecuteTemplate(&buf, c.TplName, c.Data) + if err != nil { + Trace("template Execute err:", err) + return nil, err + } } c.Data["LayoutContent"] = template.HTML(buf.String()) @@ -234,7 +236,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { } buf.Reset() - err = BeeTemplates[sectionTpl].ExecuteTemplate(&buf, sectionTpl, c.Data) + err = getTemplateByName(sectionTpl).ExecuteTemplate(&buf, sectionTpl, c.Data) if err != nil { Trace("template Execute err:", err) return nil, err @@ -244,7 +246,7 @@ func (c *Controller) RenderBytes() ([]byte, error) { } buf.Reset() - err = BeeTemplates[c.Layout].ExecuteTemplate(&buf, c.Layout, c.Data) + err = getTemplateByName(c.Layout).ExecuteTemplate(&buf, c.Layout, c.Data) if err != nil { Trace("template Execute err:", err) return nil, err @@ -258,14 +260,15 @@ func (c *Controller) RenderBytes() ([]byte, error) { if BConfig.RunMode == DEV { BuildTemplate(BConfig.WebConfig.ViewsPath, c.TplName) } - if _, ok := BeeTemplates[c.TplName]; !ok { + if t := getTemplateByName(c.TplName); t == nil { panic("can't find templatefile in the path:" + c.TplName) - } - buf.Reset() - err := BeeTemplates[c.TplName].ExecuteTemplate(&buf, c.TplName, c.Data) - if err != nil { - Trace("template Execute err:", err) - return nil, err + } else { + buf.Reset() + err = t.ExecuteTemplate(&buf, c.TplName, c.Data) + if err != nil { + Trace("template Execute err:", err) + return nil, err + } } return buf.Bytes(), nil } diff --git a/template.go b/template.go index 9954cf32..da79d383 100644 --- a/template.go +++ b/template.go @@ -30,13 +30,21 @@ import ( var ( beegoTplFuncMap = make(template.FuncMap) - // BeeTemplates caching map and supported template file extensions. - BeeTemplates = make(map[string]*template.Template) - templatesLock sync.Mutex - // BeeTemplateExt stores the template extension which will build - BeeTemplateExt = []string{"tpl", "html"} + // beeTemplates caching map and supported template file extensions. + beeTemplates = make(map[string]*template.Template) + templatesLock sync.RWMutex + // beeTemplateExt stores the template extension which will build + beeTemplateExt = []string{"tpl", "html"} ) +func getTemplateByName(name string) *template.Template { + if BConfig.RunMode == "dev" { + templatesLock.RLock() + defer templatesLock.RUnlock() + } + return beeTemplates[name] +} + func init() { beegoTplFuncMap["dateformat"] = DateFormat beegoTplFuncMap["date"] = Date @@ -55,7 +63,6 @@ func init() { beegoTplFuncMap["config"] = GetConfig beegoTplFuncMap["map_get"] = MapGet - // go1.2 added template funcs // Comparisons beegoTplFuncMap["eq"] = eq // == beegoTplFuncMap["ge"] = ge // >= @@ -103,7 +110,7 @@ func (tf *templateFile) visit(paths string, f os.FileInfo, err error) error { // HasTemplateExt return this path contains supported template extension of beego or not. func HasTemplateExt(paths string) bool { - for _, v := range BeeTemplateExt { + for _, v := range beeTemplateExt { if strings.HasSuffix(paths, "."+v) { return true } @@ -113,12 +120,12 @@ func HasTemplateExt(paths string) bool { // AddTemplateExt add new extension for template. func AddTemplateExt(ext string) { - for _, v := range BeeTemplateExt { + for _, v := range beeTemplateExt { if v == ext { return } } - BeeTemplateExt = append(BeeTemplateExt, ext) + beeTemplateExt = append(beeTemplateExt, ext) } // BuildTemplate will build all template files in a directory. @@ -149,7 +156,7 @@ func BuildTemplate(dir string, files ...string) error { if err != nil { Trace("parse template err:", file, err) } else { - BeeTemplates[file] = t + beeTemplates[file] = t } templatesLock.Unlock() } diff --git a/template_test.go b/template_test.go index 2e222efc..4f13736c 100644 --- a/template_test.go +++ b/template_test.go @@ -70,10 +70,10 @@ func TestTemplate(t *testing.T) { if err := BuildTemplate(dir); err != nil { t.Fatal(err) } - if len(BeeTemplates) != 3 { - t.Fatalf("should be 3 but got %v", len(BeeTemplates)) + if len(beeTemplates) != 3 { + t.Fatalf("should be 3 but got %v", len(beeTemplates)) } - if err := BeeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil { + if err := beeTemplates["index.tpl"].ExecuteTemplate(os.Stdout, "index.tpl", nil); err != nil { t.Fatal(err) } for _, name := range files { @@ -126,7 +126,7 @@ func TestRelativeTemplate(t *testing.T) { if err := BuildTemplate(dir, files[1]); err != nil { t.Fatal(err) } - if err := BeeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil { + if err := beeTemplates["easyui/rbac/user.tpl"].ExecuteTemplate(os.Stdout, "easyui/rbac/user.tpl", nil); err != nil { t.Fatal(err) } for _, name := range files {