feat: add response cache to all requests

main
Gabe Farrell 7 months ago
parent d652e30b52
commit 5dc7251be0

@ -106,6 +106,7 @@ func handleAniListAnimeSearch(idMap *ConcurrentMap, permaSkipIds []string) http.
log.Printf("Error writing error response: %v", writeErr)
}
} else {
w.WriteHeader(http.StatusOK)
if _, writeErr := w.Write(search); writeErr != nil {
log.Printf("Error writing response: %v", writeErr)
}

@ -3,3 +3,5 @@ module github.com/gabehf/sonarr-anime-importer
go 1.23.0
require github.com/darenliang/jikan-go v1.2.3
require github.com/patrickmn/go-cache v2.1.0+incompatible

@ -1,2 +1,4 @@
github.com/darenliang/jikan-go v1.2.3 h1:Nw6ykJU47QW3rwiIBWHyy1cBNM1Cxsz0AVCdqIN278A=
github.com/darenliang/jikan-go v1.2.3/go.mod h1:rv7ksvNqc1b0UK7mf1Uc3swPToJXd9EZQLz5C38jk9Q=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=

@ -1,6 +1,8 @@
package main
import (
"fmt"
"net/http"
"net/url"
"strconv"
)
@ -28,3 +30,7 @@ func FullAnimeTitle(title, engtitle string) string {
return title
}
}
func RequestString(r *http.Request) string {
return fmt.Sprintf("%s %s?%s", r.Method, r.URL.Path, r.URL.RawQuery)
}

@ -11,6 +11,8 @@ import (
"strings"
"sync"
"time"
"github.com/patrickmn/go-cache"
)
type ResponseItem struct {
@ -57,9 +59,15 @@ func main() {
if permaSkipAniListStr != "" {
log.Printf("Always skipping AniList IDs: %v\n", permaSkipAniListIds)
}
buildIdMapMiddleware := newRebuildStaleIdMapMiddleware(idMap)
http.HandleFunc("/v1/mal/anime", loggerMiddleware(buildIdMapMiddleware(handleMalAnimeSearch(idMap, permaSkipMalIds))))
http.HandleFunc("/v1/anilist/anime", loggerMiddleware(buildIdMapMiddleware(handleAniListAnimeSearch(idMap, permaSkipAniListIds))))
log.Printf("Preparing cache...")
c := cache.New(10*time.Minute, 15*time.Minute)
middleware := []Middleware{
loggerMiddleware,
newCacheMiddleware(c),
newRebuildStaleIdMapMiddleware(idMap),
}
http.HandleFunc("/v1/mal/anime", ChainMiddleware(handleMalAnimeSearch(idMap, permaSkipMalIds), middleware...))
http.HandleFunc("/v1/anilist/anime", ChainMiddleware(handleAniListAnimeSearch(idMap, permaSkipAniListIds), middleware...))
log.Println("Listening on :3333")
srv := &http.Server{

@ -21,6 +21,7 @@ func handleMalAnimeSearch(idMap *ConcurrentMap, permaSkipMalIds []string) http.H
log.Printf("Error writing error response: %v", writeErr)
}
} else {
w.WriteHeader(http.StatusOK)
if _, writeErr := w.Write([]byte(search)); writeErr != nil {
log.Printf("Error writing response: %v", writeErr)
}

@ -1,13 +1,31 @@
package main
import (
"bytes"
"log"
"net/http"
"time"
"github.com/patrickmn/go-cache"
)
func newRebuildStaleIdMapMiddleware(idMap *ConcurrentMap) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
// from https://medium.com/@chrisgregory_83433/chaining-middleware-in-go-918cfbc5644d
type Middleware func(http.HandlerFunc) http.HandlerFunc
func ChainMiddleware(h http.HandlerFunc, m ...Middleware) http.HandlerFunc {
if len(m) < 1 {
return h
}
wrapped := h
// loop in reverse to preserve middleware order
for i := len(m) - 1; i >= 0; i-- {
wrapped = m[i](wrapped)
}
return wrapped
}
func newRebuildStaleIdMapMiddleware(idMap *ConcurrentMap) func(http.HandlerFunc) http.HandlerFunc {
return func(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if time.Since(lastBuiltAnimeIdList) > 24*time.Hour {
log.Println("Anime ID association table expired, building new table...")
@ -18,9 +36,47 @@ func newRebuildStaleIdMapMiddleware(idMap *ConcurrentMap) func(http.Handler) htt
}
}
func loggerMiddleware(next http.Handler) http.HandlerFunc {
func loggerMiddleware(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s?%s", r.Method, r.URL.Path, r.URL.RawQuery)
log.Print(RequestString(r))
next.ServeHTTP(w, r)
})
}
type cacheResponseWriter struct {
http.ResponseWriter
status int
body *bytes.Buffer
}
func (w *cacheResponseWriter) WriteHeader(statusCode int) {
w.status = statusCode
w.ResponseWriter.WriteHeader(statusCode)
}
func (w *cacheResponseWriter) Write(b []byte) (int, error) {
w.body.Write(b) // Capture body
return w.ResponseWriter.Write(b)
}
func newCacheMiddleware(c *cache.Cache) func(http.HandlerFunc) http.HandlerFunc {
return func(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
key := RequestString(r)
if cachedResp, found := c.Get(key); found {
log.Println("Responding with cached response")
w.WriteHeader(http.StatusOK)
w.Write(cachedResp.([]byte))
return
}
crw := &cacheResponseWriter{
ResponseWriter: w,
body: &bytes.Buffer{},
}
next.ServeHTTP(crw, r)
if crw.status == http.StatusOK {
c.Set(key, crw.body.Bytes(), cache.DefaultExpiration)
}
})
}
}

Loading…
Cancel
Save