This commit is contained in:
2025-12-07 14:54:35 +01:00
parent ac6a4fd6d3
commit d18a72d492
18 changed files with 725 additions and 307 deletions
+45
View File
@@ -0,0 +1,45 @@
package template_utils
import (
"html/template"
"sync"
)
type TemplateCache struct {
cache map[string]*template.Template
mu sync.RWMutex
}
var globalCache *TemplateCache
func init() {
globalCache = &TemplateCache{
cache: make(map[string]*template.Template),
}
}
func GetTemplate(name string, files ...string) (*template.Template, error) {
globalCache.mu.RLock()
if tmpl, exists := globalCache.cache[name]; exists {
globalCache.mu.RUnlock()
return tmpl, nil
}
globalCache.mu.RUnlock()
tmpl, err := template.ParseFiles(files...)
if err != nil {
return nil, err
}
globalCache.mu.Lock()
globalCache.cache[name] = tmpl
globalCache.mu.Unlock()
return tmpl, nil
}
func ClearCache() {
globalCache.mu.Lock()
globalCache.cache = make(map[string]*template.Template)
globalCache.mu.Unlock()
}