forked from ShiningRush/fastflow
/
tpl.go
59 lines (51 loc) · 1.15 KB
/
tpl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package render
import (
"sync"
"text/template"
"github.com/golang/groupcache/lru"
)
type TplProvider struct {
cache *lru.Cache
rwMutex sync.RWMutex
}
func NewCachedTplProvider(maxSize int) *TplProvider {
cache := lru.New(maxSize)
return &TplProvider{
cache: cache,
rwMutex: sync.RWMutex{},
}
}
func (c *TplProvider) cacheGetTpl(tplText string) (*template.Template, bool) {
c.rwMutex.RLock()
defer c.rwMutex.RUnlock()
v, ok := c.cache.Get(tplText)
if !ok {
return nil, false
}
return v.(*template.Template), true
}
func (c *TplProvider) cacheSetTpl(tplText string, template *template.Template) {
c.rwMutex.Lock()
defer c.rwMutex.Unlock()
c.cache.Add(tplText, template)
}
func (c *TplProvider) GetTpl(tplText string) (*template.Template, error) {
tpl, ok := c.cacheGetTpl(tplText)
if ok {
return tpl, nil
}
tpl, err := c.parseTpl(tplText)
if err != nil {
return nil, err
}
c.cacheSetTpl(tplText, tpl)
return tpl, err
}
func (c *TplProvider) parseTpl(tplText string) (*template.Template, error) {
tpl, err := template.New(tplText).Parse(tplText)
if err != nil {
return nil, err
}
tpl.Option("missingkey=error")
return tpl, err
}