-
Notifications
You must be signed in to change notification settings - Fork 89
/
ace.go
70 lines (57 loc) · 1.39 KB
/
ace.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
60
61
62
63
64
65
66
67
68
69
70
package ace
import (
"html/template"
"sync"
)
var cache = make(map[string]template.Template)
var cacheMutex = new(sync.RWMutex)
// Load loads and returns an HTML template. Each Ace templates are parsed only once
// and cached if the "DynamicReload" option are not set.
func Load(basePath, innerPath string, opts *Options) (*template.Template, error) {
// Initialize the options.
opts = InitializeOptions(opts)
name := basePath + colon + innerPath
if !opts.DynamicReload {
if tpl, ok := getCache(name); ok {
return &tpl, nil
}
}
// Read files.
src, err := readFiles(basePath, innerPath, opts)
if err != nil {
return nil, err
}
// Parse the source.
rslt, err := ParseSource(src, opts)
if err != nil {
return nil, err
}
// Compile the parsed result.
tpl, err := CompileResult(name, rslt, opts)
if err != nil {
return nil, err
}
if !opts.DynamicReload {
setCache(name, *tpl)
}
return tpl, nil
}
// getCache returns the cached template.
func getCache(name string) (template.Template, bool) {
cacheMutex.RLock()
tpl, ok := cache[name]
cacheMutex.RUnlock()
return tpl, ok
}
// setCache sets the template to the cache.
func setCache(name string, tpl template.Template) {
cacheMutex.Lock()
cache[name] = tpl
cacheMutex.Unlock()
}
// FlushCache clears all cached templates.
func FlushCache() {
cacheMutex.Lock()
cache = make(map[string]template.Template)
cacheMutex.Unlock()
}