mirror of
https://github.com/gabehf/sonarr-anime-importer.git
synced 2026-03-18 11:46:24 -07:00
feat: add response cache to all requests
This commit is contained in:
parent
d652e30b52
commit
5dc7251be0
7 changed files with 83 additions and 7 deletions
|
|
@ -106,6 +106,7 @@ func handleAniListAnimeSearch(idMap *ConcurrentMap, permaSkipIds []string) http.
|
||||||
log.Printf("Error writing error response: %v", writeErr)
|
log.Printf("Error writing error response: %v", writeErr)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
if _, writeErr := w.Write(search); writeErr != nil {
|
if _, writeErr := w.Write(search); writeErr != nil {
|
||||||
log.Printf("Error writing response: %v", writeErr)
|
log.Printf("Error writing response: %v", writeErr)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
go.mod
2
go.mod
|
|
@ -3,3 +3,5 @@ module github.com/gabehf/sonarr-anime-importer
|
||||||
go 1.23.0
|
go 1.23.0
|
||||||
|
|
||||||
require github.com/darenliang/jikan-go v1.2.3
|
require github.com/darenliang/jikan-go v1.2.3
|
||||||
|
|
||||||
|
require github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -1,2 +1,4 @@
|
||||||
github.com/darenliang/jikan-go v1.2.3 h1:Nw6ykJU47QW3rwiIBWHyy1cBNM1Cxsz0AVCdqIN278A=
|
github.com/darenliang/jikan-go v1.2.3 h1:Nw6ykJU47QW3rwiIBWHyy1cBNM1Cxsz0AVCdqIN278A=
|
||||||
github.com/darenliang/jikan-go v1.2.3/go.mod h1:rv7ksvNqc1b0UK7mf1Uc3swPToJXd9EZQLz5C38jk9Q=
|
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
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
@ -28,3 +30,7 @@ func FullAnimeTitle(title, engtitle string) string {
|
||||||
return title
|
return title
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func RequestString(r *http.Request) string {
|
||||||
|
return fmt.Sprintf("%s %s?%s", r.Method, r.URL.Path, r.URL.RawQuery)
|
||||||
|
}
|
||||||
|
|
|
||||||
14
main.go
14
main.go
|
|
@ -11,6 +11,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ResponseItem struct {
|
type ResponseItem struct {
|
||||||
|
|
@ -57,9 +59,15 @@ func main() {
|
||||||
if permaSkipAniListStr != "" {
|
if permaSkipAniListStr != "" {
|
||||||
log.Printf("Always skipping AniList IDs: %v\n", permaSkipAniListIds)
|
log.Printf("Always skipping AniList IDs: %v\n", permaSkipAniListIds)
|
||||||
}
|
}
|
||||||
buildIdMapMiddleware := newRebuildStaleIdMapMiddleware(idMap)
|
log.Printf("Preparing cache...")
|
||||||
http.HandleFunc("/v1/mal/anime", loggerMiddleware(buildIdMapMiddleware(handleMalAnimeSearch(idMap, permaSkipMalIds))))
|
c := cache.New(10*time.Minute, 15*time.Minute)
|
||||||
http.HandleFunc("/v1/anilist/anime", loggerMiddleware(buildIdMapMiddleware(handleAniListAnimeSearch(idMap, permaSkipAniListIds))))
|
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")
|
log.Println("Listening on :3333")
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|
|
||||||
1
mal.go
1
mal.go
|
|
@ -21,6 +21,7 @@ func handleMalAnimeSearch(idMap *ConcurrentMap, permaSkipMalIds []string) http.H
|
||||||
log.Printf("Error writing error response: %v", writeErr)
|
log.Printf("Error writing error response: %v", writeErr)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
if _, writeErr := w.Write([]byte(search)); writeErr != nil {
|
if _, writeErr := w.Write([]byte(search)); writeErr != nil {
|
||||||
log.Printf("Error writing response: %v", writeErr)
|
log.Printf("Error writing response: %v", writeErr)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,31 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
)
|
)
|
||||||
|
|
||||||
func newRebuildStaleIdMapMiddleware(idMap *ConcurrentMap) func(http.Handler) http.Handler {
|
// from https://medium.com/@chrisgregory_83433/chaining-middleware-in-go-918cfbc5644d
|
||||||
return func(next http.Handler) http.Handler {
|
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) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if time.Since(lastBuiltAnimeIdList) > 24*time.Hour {
|
if time.Since(lastBuiltAnimeIdList) > 24*time.Hour {
|
||||||
log.Println("Anime ID association table expired, building new table...")
|
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) {
|
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)
|
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…
Add table
Add a link
Reference in a new issue