perf(templates): Cache templates in map
This commit is contained in:
parent
b9e550d6f5
commit
7c32f1aad2
4
main.go
4
main.go
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user