feat(macros): implement template cache (#2049)

* feat(macros): implement template cache

* fix: typo in error

* fix(macros): set DefaultTTL

* fix: accidentally removed SetTimerResolution

* revert: set NoTTL in MustParse
This commit is contained in:
soup 2025-04-27 14:02:39 +02:00 committed by GitHub
parent bfda849ef5
commit c7efcf1b75
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 52 additions and 12 deletions

View file

@ -10,11 +10,18 @@ import (
"time"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/ttlcache"
"github.com/Masterminds/sprig/v3"
"github.com/dustin/go-humanize"
)
var templateCache = ttlcache.New(
ttlcache.Options[string, *template.Template]{}.
SetTimerResolution(5 * time.Minute).
SetDefaultTTL(15 * time.Minute),
)
type Macro struct {
Artists string
Audio []string
@ -175,16 +182,19 @@ func (m Macro) Parse(text string) (string, error) {
return "", nil
}
// TODO implement template cache
// setup template
tmpl, err := template.New("macro").Funcs(sprig.TxtFuncMap()).Parse(text)
// get template from cache or create new
tmpl, ok := templateCache.Get(text)
if !ok {
var err error
tmpl, err = template.New("macro").Funcs(sprig.TxtFuncMap()).Parse(text)
if err != nil {
return "", errors.Wrap(err, "could parse macro template")
return "", errors.Wrap(err, "could not parse macro template")
}
templateCache.Set(text, tmpl, ttlcache.DefaultTTL)
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m)
err := tmpl.Execute(&tpl, m)
if err != nil {
return "", errors.Wrap(err, "could not parse macro")
}
@ -198,14 +208,19 @@ func (m Macro) MustParse(text string) string {
return ""
}
// setup template
tmpl, err := template.New("macro").Funcs(sprig.TxtFuncMap()).Parse(text)
// get template from cache or create new
tmpl, ok := templateCache.Get(text)
if !ok {
var err error
tmpl, err = template.New("macro").Funcs(sprig.TxtFuncMap()).Parse(text)
if err != nil {
return ""
}
templateCache.Set(text, tmpl, ttlcache.NoTTL)
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m)
err := tmpl.Execute(&tpl, m)
if err != nil {
return ""
}

View file

@ -290,3 +290,28 @@ func TestMacros_Parse(t *testing.T) {
})
}
}
func TestMacros_TemplateCache(t *testing.T) {
t.Parallel()
release := Release{
TorrentName: "Test Movie 2024",
Year: 2024,
}
m := NewMacro(release)
template := "{{.TorrentName}} ({{.Year}})"
// parse and cache
got1, err := m.Parse(template)
assert.NoError(t, err)
assert.Equal(t, "Test Movie 2024 (2024)", got1)
// use cached template
got2, err := m.Parse(template)
assert.NoError(t, err)
assert.Equal(t, "Test Movie 2024 (2024)", got2)
_, ok := templateCache.Get(template)
assert.True(t, ok, "template should be in cache")
}