-
Notifications
You must be signed in to change notification settings - Fork 13
/
template_cache.go
131 lines (110 loc) · 2.61 KB
/
template_cache.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package handler
import (
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"time"
gopi "github.com/djthorpe/gopi/v3"
multierror "github.com/hashicorp/go-multierror"
)
/////////////////////////////////////////////////////////////////////
// TYPES
type cache struct {
sync.RWMutex
folder string
t map[string]cached
}
type cached struct {
os.FileInfo
*template.Template
}
/////////////////////////////////////////////////////////////////////
// LIFECYCLE
func NewCache(folder string) (*cache, error) {
// Read files in folder
files, err := ioutil.ReadDir(folder)
if err != nil {
return nil, err
}
// Create cache
this := new(cache)
this.t = make(map[string]cached, len(files))
this.folder = folder
// Read templates into cache. Collect parse errors
// into results
var result error
for _, file := range files {
if strings.HasPrefix(file.Name(), ".") {
continue
}
if file.Mode().IsRegular() == false {
continue
}
if tmpl, err := template.ParseFiles(filepath.Join(folder, file.Name())); err != nil {
result = multierror.Append(result, err)
} else {
key := tmpl.Name()
this.t[key] = cached{file, tmpl}
}
}
// Return cache or error
if result != nil {
return nil, result
} else {
return this, nil
}
}
/////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
func (this *cache) Get(name string) (*template.Template, error) {
t := this.get(name)
path := filepath.Join(this.folder, name)
// No template exists with this name, return nil
if t.Template == nil {
return nil, gopi.ErrNotFound.WithPrefix(name)
}
// Check case where template has not changed
info, err := os.Stat(path)
if err != nil {
return t.Template, err
} else if info.ModTime() == t.ModTime() {
return t.Template, nil
}
// Re-parse template and return it
tmpl, err := template.ParseFiles(path)
if err == nil {
return this.set(name, tmpl, info), nil
} else {
return t.Template, err
}
}
func (this *cache) Modified(name string) time.Time {
t := this.get(name)
if t.Template == nil {
return time.Time{}
} else {
return t.ModTime()
}
}
/////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
func (this *cache) get(name string) cached {
this.RWMutex.RLock()
defer this.RWMutex.RUnlock()
if this.t == nil {
return cached{}
} else if t, exists := this.t[name]; exists == false {
return cached{}
} else {
return t
}
}
func (this *cache) set(name string, t *template.Template, info os.FileInfo) *template.Template {
this.RWMutex.Lock()
defer this.RWMutex.Unlock()
this.t[name] = cached{info, t}
return t
}