Add redis for caching, first try during a train ride so expect it to not be working yet

This commit is contained in:
Moritz Marquardt 2024-03-24 20:24:32 +01:00
parent b8b9886ee1
commit 5b6eecc75f
12 changed files with 149 additions and 32 deletions

View file

@ -1,6 +1,7 @@
package upstream
import (
"encoding/json"
"strconv"
"strings"
"time"
@ -29,7 +30,12 @@ func (o *Options) getRedirects(giteaClient *gitea.Client, redirectsCache cache.I
// Check for cached redirects
if cachedValue, ok := redirectsCache.Get(cacheKey); ok {
redirects = cachedValue.([]Redirect)
redirects := []Redirect{}
err := json.Unmarshal([]byte(cachedValue), redirects)
if err != nil {
log.Error().Err(err).Msgf("could not parse redirects for key %s", cacheKey)
// It's okay to continue, the array stays empty.
}
} else {
// Get _redirects file and parse
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, redirectsConfig)
@ -58,7 +64,12 @@ func (o *Options) getRedirects(giteaClient *gitea.Client, redirectsCache cache.I
})
}
}
_ = redirectsCache.Set(cacheKey, redirects, redirectsCacheTimeout)
redirectsJson, err := json.Marshal(redirects)
if err != nil {
log.Error().Err(err).Msgf("could not store redirects for key %s", cacheKey)
} else {
_ = redirectsCache.Set(cacheKey, string(redirectsJson), redirectsCacheTimeout)
}
}
return redirects
}