perf(templates): Cache templates in map
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user