File: ace.go

package info (click to toggle)
golang-github-yosssi-ace 0.0.4%2Bgit20160102.51.71afeb7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 468 kB
  • ctags: 249
  • sloc: makefile: 3; sh: 1
file content (70 lines) | stat: -rw-r--r-- 1,445 bytes parent folder | download | duplicates (3)
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()
}