perf(templates): Cache templates in map

This commit is contained in:
Nicolai Ort 2024-12-12 17:34:55 +01:00
parent b9e550d6f5
commit 7c32f1aad2
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
2 changed files with 13 additions and 1 deletions

View File

@ -74,7 +74,9 @@ func main() {
}
barcodeGenerator := &services.DefaultBarcodeService{}
staticService := &services.DefaultStaticService{}
staticService := &services.DefaultStaticService{
Cache: make(map[string]string),
}
handler := handlers.DefaultHandler{
Config: config,
BarcodeService: barcodeGenerator,

View File

@ -14,14 +14,24 @@ type StaticService interface {
}
type DefaultStaticService struct {
Cache map[string]string
}
func (s *DefaultStaticService) GetTemplate(locale, templateName string) (string, error) {
if s.Cache[locale+templateName] != "" {
log.Printf("returning cached template %s with locale %s", templateName, locale)
return s.Cache[locale+templateName], nil
}
content, err := os.ReadFile(fmt.Sprintf("static/templates/%s/%s.html", templateName, locale))
if content == nil || err != nil {
log.Printf("error reading template %s with locale %s: %v", templateName, locale, err)
return "", err
}
s.Cache[locale+templateName] = string(content)
return string(content), nil
}