revert in-memory badge cache

This commit is contained in:
Shawn Smith
2020-11-06 23:29:52 +09:00
parent 6cb26c2f6a
commit 232d912e51
2 changed files with 9 additions and 24 deletions

View File

@@ -5,7 +5,6 @@ import (
"log"
"net/http"
"strings"
"sync"
"github.com/dgraph-io/badger/v2"
"github.com/gojp/goreportcard/check"
@@ -15,35 +14,23 @@ func badgePath(grade check.Grade, style string) string {
return fmt.Sprintf("assets/badges/%s_%s.svg", strings.ToLower(string(grade)), strings.ToLower(style))
}
var badgeCache = sync.Map{}
// BadgeHandler handles fetching the badge images
func BadgeHandler(w http.ResponseWriter, r *http.Request, db *badger.DB, repo string) {
resp, err := newChecksResp(db, repo, false)
// See: http://shields.io/#styles
style := r.URL.Query().Get("style")
if style == "" {
style = "flat"
}
var grade check.Grade
g, ok := badgeCache.Load(repo)
if ok {
log.Printf("Fetching badge for %q from cache...", repo)
grade = g.(check.Grade)
} else {
resp, err := newChecksResp(db, repo, false)
if err != nil {
log.Printf("ERROR: fetching badge for %s: %v", repo, err)
url := "https://img.shields.io/badge/go%20report-error-lightgrey.svg?style=" + style
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
return
}
log.Printf("Adding badge for %q to cache...", repo)
badgeCache.Store(repo, resp.Grade)
grade = resp.Grade
if err != nil {
log.Printf("ERROR: fetching badge for %s: %v", repo, err)
url := "https://img.shields.io/badge/go%20report-error-lightgrey.svg?style=" + style
http.Redirect(w, r, url, http.StatusTemporaryRedirect)
return
}
w.Header().Set("Cache-control", "no-store, no-badgeCache, must-revalidate")
http.ServeFile(w, r, badgePath(grade, style))
w.Header().Set("Cache-control", "no-store, no-cache, must-revalidate")
http.ServeFile(w, r, badgePath(resp.Grade, style))
}

View File

@@ -115,8 +115,6 @@ func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, e
LastRefreshHumanized: humanize.Time(t),
}
badgeCache.Store(repo, checkResult.Grade)
respBytes, err := json.Marshal(resp)
if err != nil {
return checksResp{}, fmt.Errorf("could not marshal json: %v", err)