/
cache.go
61 lines (48 loc) · 1.41 KB
/
cache.go
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
package templates
import (
"context"
htmlTemplate "html/template"
"io"
"sync"
"github.com/sirupsen/logrus"
"github.com/ShoshinNikita/budget-manager/internal/pkg/request_id"
)
// TemplateCacheExecutor caches templates after reading them from the disk
type TemplateCacheExecutor struct {
log logrus.FieldLogger
templates map[string]*htmlTemplate.Template
mu sync.RWMutex
}
var _ TemplateExecutor = (*TemplateCacheExecutor)(nil)
func NewTemplateCacheExecutor(log logrus.FieldLogger) *TemplateCacheExecutor {
return &TemplateCacheExecutor{
log: log,
//
templates: make(map[string]*htmlTemplate.Template),
}
}
func (ex *TemplateCacheExecutor) Get(ctx context.Context, template Template) *htmlTemplate.Template {
log := request_id.FromContextToLogger(ctx, ex.log)
log = log.WithField("path", template.Path)
ex.mu.RLock()
tpl, ok := ex.templates[template.Path]
ex.mu.RUnlock()
if ok {
log.Debug("use template from cache")
return tpl
}
// Have to load template from disk
log.Debug("load template from disk")
tpl, err := loadTemplateWithDeps(template)
if err != nil {
ex.log.WithError(err).Panic("couldn't load template")
}
ex.mu.Lock()
ex.templates[template.Path] = tpl
ex.mu.Unlock()
return tpl
}
func (ex *TemplateCacheExecutor) Execute(ctx context.Context, template Template, w io.Writer, data interface{}) error {
tpl := ex.Get(ctx, template)
return executeTemplate(ex.log, tpl, w, data)
}