Skip to content

Commit 50820dd

Browse files
committed
优化:提高性能
1 parent 1e586a9 commit 50820dd

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

common/types.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"os"
66
"path"
77
"reflect"
8+
"sync"
9+
"html/template"
810

911
"github.com/coscms/tagfast"
1012
)
@@ -14,6 +16,10 @@ var (
1416
LabelFn func(string) string = func(s string) string {
1517
return s
1618
}
19+
20+
//private
21+
cachedTemplate map[string]*template.Template = make(map[string]*template.Template)
22+
lock *sync.RWMutex = new(sync.RWMutex)
1723
)
1824

1925
const (
@@ -58,12 +64,33 @@ const (
5864

5965
// CreateUrl creates the complete url of the desired widget template
6066
func CreateUrl(widget string) string {
67+
//println(widget)
6168
if _, err := os.Stat(widget); os.IsNotExist(err) {
6269
return path.Join(os.Getenv("GOPATH"), "src", PACKAGE_NAME, widget)
6370
}
6471
return widget
6572
}
6673

74+
func CachedTemplate(cachedKey string) (r *template.Template, ok bool) {
75+
lock.RLock()
76+
defer lock.RUnlock()
77+
78+
r, ok = cachedTemplate[cachedKey]
79+
return
80+
}
81+
82+
func SetCachedTemplate(cachedKey string, tmpl *template.Template) bool {
83+
lock.Lock()
84+
defer lock.Unlock()
85+
86+
cachedTemplate[cachedKey] = tmpl
87+
return true
88+
}
89+
90+
func ClearCachedTemplate() {
91+
cachedTemplate = make(map[string]*template.Template)
92+
}
93+
6794
func Tag(t reflect.Type, fieldNo int, tagName string) string {
6895
return tagfast.Tag1(t, fieldNo, tagName)
6996
}

fieldset.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ func (f *FieldSetType) Render(appendData ...map[string]interface{}) template.HTM
3838
}
3939
}
4040
data["append"] = map[string]interface{}{"container": "fieldset"}
41-
err := template.Must(template.ParseFiles(formcommon.CreateUrl(formcommon.TmplDir+"/"+f.tmpl+".html"))).Execute(buf, data)
41+
tpf := formcommon.TmplDir+"/"+f.tmpl+".html"
42+
tpl, ok := formcommon.CachedTemplate(tpf)
43+
if !ok {
44+
tpl = template.Must(template.ParseFiles(formcommon.CreateUrl(tpf)))
45+
formcommon.SetCachedTemplate(tpf, tpl)
46+
}
47+
err := tpl.Execute(buf, data)
4248
if err != nil {
4349
panic(err)
4450
}

forms.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ func NewForm(style string, args ...string) *Form {
5555
action = args[1]
5656
tmplFile = args[2]
5757
}
58-
tmpl, err := template.ParseFiles(formcommon.CreateUrl(tmplFile))
59-
if err != nil {
60-
panic(err)
58+
tmpl, ok := formcommon.CachedTemplate(tmplFile)
59+
if !ok {
60+
tmpl = template.Must(template.ParseFiles(formcommon.CreateUrl(tmplFile)))
61+
formcommon.SetCachedTemplate(tmplFile, tmpl)
6162
}
6263
return &Form{
6364
make([]FormElement, 0),

widgets/widgets.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ package widgets
55

66
import (
77
"bytes"
8-
"github.com/coscms/forms/common"
98
"html/template"
9+
10+
"github.com/coscms/forms/common"
1011
)
1112

1213
// Simple widget object that gets executed at render time.
@@ -28,12 +29,19 @@ func (w *Widget) Render(data interface{}) string {
2829
}
2930

3031
// BaseWidget creates a Widget based on style and inpuType parameters, both defined in the common package.
31-
func BaseWidget(style, inputType, tmpl string) *Widget {
32-
var fpath string = formcommon.TmplDir + "/" + style + "/"
33-
var urls []string = []string{formcommon.CreateUrl(fpath + "generic.tmpl")}
34-
var tpath string = widgetTmpl(inputType, tmpl)
35-
urls = append(urls, formcommon.CreateUrl(fpath+tpath+".html"))
36-
templ := template.Must(template.ParseFiles(urls...))
32+
func BaseWidget(style, inputType, tmplName string) *Widget {
33+
var cachedKey string = style+", "+inputType+", "+tmplName
34+
templ, ok := formcommon.CachedTemplate(cachedKey)
35+
if !ok {
36+
var (
37+
fpath string = formcommon.TmplDir + "/" + style + "/"
38+
urls []string = []string{formcommon.CreateUrl(fpath + "generic.tmpl")}
39+
tpath string = widgetTmpl(inputType, tmplName)
40+
)
41+
urls = append(urls, formcommon.CreateUrl(fpath+tpath+".html"))
42+
templ = template.Must(template.ParseFiles(urls...))
43+
formcommon.SetCachedTemplate(cachedKey, templ)
44+
}
3745
return &Widget{templ}
3846
}
3947

0 commit comments

Comments
 (0)