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

@ -20,7 +20,7 @@ const canonicalDomainConfig = ".domains"
func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain, mainDomainSuffix string, canonicalDomainCache cache.ICache) (domain string, valid bool) {
// Check if this request is cached.
if cachedValue, ok := canonicalDomainCache.Get(o.TargetOwner + "/" + o.TargetRepo + "/" + o.TargetBranch); ok {
domains := cachedValue.([]string)
domains := strings.Split(cachedValue, "\n")
for _, domain := range domains {
if domain == actualDomain {
valid = true
@ -33,6 +33,7 @@ func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain,
body, err := giteaClient.GiteaRawContent(o.TargetOwner, o.TargetRepo, o.TargetBranch, canonicalDomainConfig)
if err != nil && !errors.Is(err, gitea.ErrorNotFound) {
log.Error().Err(err).Msgf("could not read %s of %s/%s", canonicalDomainConfig, o.TargetOwner, o.TargetRepo)
// TODO: WTF we just continue?!
}
var domains []string
@ -62,7 +63,7 @@ func (o *Options) CheckCanonicalDomain(giteaClient *gitea.Client, actualDomain,
}
// Add result to cache.
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, domains, canonicalDomainCacheTimeout)
_ = canonicalDomainCache.Set(o.TargetOwner+"/"+o.TargetRepo+"/"+o.TargetBranch, strings.Join(domains, "\n"), canonicalDomainCacheTimeout)
// Return the first domain from the list and return if any of the domains
// matched the requested domain.

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
}