-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_cache.go
More file actions
236 lines (198 loc) · 5.51 KB
/
template_cache.go
File metadata and controls
236 lines (198 loc) · 5.51 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package parser
import (
"container/list"
"sync"
"text/template"
"time"
)
// CachedTemplate holds a compiled template with metadata
type CachedTemplate struct {
Template *template.Template
LastModified time.Time
AccessTime time.Time
AccessCount int64
Hash string // Hash of the template content for change detection
}
// TemplateCache provides efficient caching of compiled templates
type TemplateCache struct {
templates map[string]*CachedTemplate
lruList *list.List
lruIndex map[string]*list.Element
maxSize int
funcMap template.FuncMap
mu sync.RWMutex
}
// NewTemplateCache creates a new template cache
func NewTemplateCache(maxSize int, funcMap template.FuncMap) *TemplateCache {
return &TemplateCache{
templates: make(map[string]*CachedTemplate),
lruList: list.New(),
lruIndex: make(map[string]*list.Element),
maxSize: maxSize,
funcMap: funcMap,
}
}
// Get retrieves a template from the cache or compiles it if not found
func (c *TemplateCache) Get(name string, loader TemplateLoader) (*template.Template, error) {
c.mu.Lock()
defer c.mu.Unlock()
// Check if template exists in cache
if cached, exists := c.templates[name]; exists {
// Check if template needs to be reloaded
lastMod, err := loader.LastModified(name)
if err != nil {
// If we can't get the modification time, use cached version
c.updateAccess(name, cached)
return cached.Template, nil
}
if lastMod.After(cached.LastModified) {
// Template has been modified, reload it
return c.loadAndCache(name, loader)
}
// Template is up to date, update access time and return
c.updateAccess(name, cached)
return cached.Template, nil
}
// Template not in cache, load and cache it
return c.loadAndCache(name, loader)
}
// loadAndCache loads a template and adds it to the cache
func (c *TemplateCache) loadAndCache(name string, loader TemplateLoader) (*template.Template, error) {
// Load template content
content, err := loader.Load(name)
if err != nil {
return nil, err
}
// Get last modified time
lastMod, err := loader.LastModified(name)
if err != nil {
lastMod = time.Now()
}
// Compile template
tmpl := template.New(name)
if c.funcMap != nil {
tmpl = tmpl.Funcs(c.funcMap)
}
tmpl, err = tmpl.Parse(content)
if err != nil {
return nil, err
}
// Create cached template
cached := &CachedTemplate{
Template: tmpl,
LastModified: lastMod,
AccessTime: time.Now(),
AccessCount: 1,
Hash: "", // No hash available from loader
}
// Add to cache
c.addToCache(name, cached)
return tmpl, nil
}
// addToCache adds a template to the cache with LRU eviction
func (c *TemplateCache) addToCache(name string, cached *CachedTemplate) {
// Remove existing entry if it exists
if existing, exists := c.templates[name]; exists {
c.removeFromLRU(name)
_ = existing
}
// Add new entry
c.templates[name] = cached
element := c.lruList.PushFront(name)
c.lruIndex[name] = element
// Evict least recently used items if cache is full
if c.maxSize > 0 && len(c.templates) > c.maxSize {
c.evictLRU()
}
}
// updateAccess updates the access time and count for a cached template
func (c *TemplateCache) updateAccess(name string, cached *CachedTemplate) {
cached.AccessTime = time.Now()
cached.AccessCount++
// Move to front of LRU list
if element, exists := c.lruIndex[name]; exists {
c.lruList.MoveToFront(element)
}
}
// removeFromLRU removes an item from the LRU tracking
func (c *TemplateCache) removeFromLRU(name string) {
if element, exists := c.lruIndex[name]; exists {
c.lruList.Remove(element)
delete(c.lruIndex, name)
}
}
// evictLRU evicts the least recently used template
func (c *TemplateCache) evictLRU() {
if c.lruList.Len() == 0 {
return
}
// Get the least recently used item
back := c.lruList.Back()
if back != nil {
name := back.Value.(string)
c.lruList.Remove(back)
delete(c.lruIndex, name)
delete(c.templates, name)
}
}
// Remove removes a template from the cache
func (c *TemplateCache) Remove(name string) {
c.mu.Lock()
defer c.mu.Unlock()
if _, exists := c.templates[name]; exists {
delete(c.templates, name)
c.removeFromLRU(name)
}
}
// Set directly sets a template in the cache with the given hash
func (c *TemplateCache) Set(name string, tmpl *template.Template, hash string) {
c.mu.Lock()
defer c.mu.Unlock()
// Create cached template
cached := &CachedTemplate{
Template: tmpl,
LastModified: time.Now(),
AccessTime: time.Now(),
AccessCount: 1,
Hash: hash,
}
// Add to cache
c.addToCache(name, cached)
}
// Clear clears all templates from the cache
func (c *TemplateCache) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
c.templates = make(map[string]*CachedTemplate)
c.lruList = list.New()
c.lruIndex = make(map[string]*list.Element)
}
// GetHash returns the hash of a cached template, or empty string if not found
func (c *TemplateCache) GetHash(name string) string {
c.mu.RLock()
defer c.mu.RUnlock()
if cached, exists := c.templates[name]; exists {
return cached.Hash
}
return ""
}
// Stats returns cache statistics
func (c *TemplateCache) Stats() CacheStats {
c.mu.RLock()
defer c.mu.RUnlock()
stats := CacheStats{
Size: len(c.templates),
MaxSize: c.maxSize,
HitCount: 0,
}
for _, cached := range c.templates {
stats.HitCount += cached.AccessCount
}
return stats
}
// CacheStats holds cache statistics
type CacheStats struct {
Size int // Current number of cached templates
MaxSize int // Maximum cache size (0 = unlimited)
HitCount int64 // Total number of cache hits
}