Refactor cache.HTTPClient

This commit is contained in:
Deluan 2024-05-11 19:36:33 -04:00
parent 955a9b43af
commit ec68d69d56
5 changed files with 20 additions and 20 deletions

View File

@ -14,7 +14,7 @@ import (
"github.com/navidrome/navidrome/core/scrobbler"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/cache"
)
const (
@ -47,7 +47,7 @@ func lastFMConstructor(ds model.DataStore) *lastfmAgent {
hc := &http.Client{
Timeout: consts.DefaultHttpClientTimeOut,
}
chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)
chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)
l.client = newClient(l.apiKey, l.secret, l.lang, chc)
return l
}

View File

@ -11,7 +11,7 @@ import (
"github.com/navidrome/navidrome/core/scrobbler"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/cache"
)
const (
@ -35,7 +35,7 @@ func listenBrainzConstructor(ds model.DataStore) *listenBrainzAgent {
hc := &http.Client{
Timeout: consts.DefaultHttpClientTimeOut,
}
chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)
chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)
l.client = newClient(l.baseURL, chc)
return l
}

View File

@ -13,7 +13,7 @@ import (
"github.com/navidrome/navidrome/core/agents"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/utils"
"github.com/navidrome/navidrome/utils/cache"
"github.com/xrash/smetrics"
)
@ -35,7 +35,7 @@ func spotifyConstructor(ds model.DataStore) agents.Interface {
hc := &http.Client{
Timeout: consts.DefaultHttpClientTimeOut,
}
chc := utils.NewCachedHTTPClient(hc, consts.DefaultHttpClientTimeOut)
chc := cache.NewHTTPClient(hc, consts.DefaultHttpClientTimeOut)
l.client = newClient(l.id, l.secret, chc)
return l
}

View File

@ -1,4 +1,4 @@
package utils
package cache
import (
"bufio"
@ -16,7 +16,7 @@ import (
const cacheSizeLimit = 100
type CachedHTTPClient struct {
type HTTPClient struct {
cache *ttlcache.Cache
hc httpDoer
}
@ -32,8 +32,8 @@ type requestData struct {
Body *string
}
func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClient {
c := &CachedHTTPClient{hc: wrapped}
func NewHTTPClient(wrapped httpDoer, ttl time.Duration) *HTTPClient {
c := &HTTPClient{hc: wrapped}
c.cache = ttlcache.NewCache()
c.cache.SetCacheSizeLimit(cacheSizeLimit)
c.cache.SkipTTLExtensionOnHit(true)
@ -55,7 +55,7 @@ func NewCachedHTTPClient(wrapped httpDoer, ttl time.Duration) *CachedHTTPClient
return c
}
func (c *CachedHTTPClient) Do(req *http.Request) (*http.Response, error) {
func (c *HTTPClient) Do(req *http.Request) (*http.Response, error) {
key := c.serializeReq(req)
respStr, err := c.cache.Get(key)
if err != nil {
@ -64,7 +64,7 @@ func (c *CachedHTTPClient) Do(req *http.Request) (*http.Response, error) {
return c.deserializeResponse(req, respStr.(string))
}
func (c *CachedHTTPClient) serializeReq(req *http.Request) string {
func (c *HTTPClient) serializeReq(req *http.Request) string {
data := requestData{
Method: req.Method,
Header: req.Header,
@ -79,7 +79,7 @@ func (c *CachedHTTPClient) serializeReq(req *http.Request) string {
return string(j)
}
func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
func (c *HTTPClient) deserializeReq(reqStr string) (*http.Request, error) {
var data requestData
_ = json.Unmarshal([]byte(reqStr), &data)
var body io.Reader
@ -95,13 +95,13 @@ func (c *CachedHTTPClient) deserializeReq(reqStr string) (*http.Request, error)
return req, nil
}
func (c *CachedHTTPClient) serializeResponse(resp *http.Response) string {
func (c *HTTPClient) serializeResponse(resp *http.Response) string {
var b = &bytes.Buffer{}
_ = resp.Write(b)
return b.String()
}
func (c *CachedHTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {
func (c *HTTPClient) deserializeResponse(req *http.Request, respStr string) (*http.Response, error) {
r := bufio.NewReader(strings.NewReader(respStr))
return http.ReadResponse(r, req)
}

View File

@ -1,4 +1,4 @@
package utils
package cache
import (
"fmt"
@ -12,9 +12,9 @@ import (
. "github.com/onsi/gomega"
)
var _ = Describe("CachedHttpClient", func() {
var _ = Describe("HTTPClient", func() {
Context("GET", func() {
var chc *CachedHTTPClient
var chc *HTTPClient
var ts *httptest.Server
var requestsReceived int
var header string
@ -25,7 +25,7 @@ var _ = Describe("CachedHttpClient", func() {
header = r.Header.Get("head")
_, _ = fmt.Fprintf(w, "Hello, %s", r.URL.Query()["name"])
}))
chc = NewCachedHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
chc = NewHTTPClient(http.DefaultClient, consts.DefaultHttpClientTimeOut)
})
AfterEach(func() {
@ -73,7 +73,7 @@ var _ = Describe("CachedHttpClient", func() {
It("expires responses after TTL", func() {
requestsReceived = 0
chc = NewCachedHTTPClient(http.DefaultClient, 10*time.Millisecond)
chc = NewHTTPClient(http.DefaultClient, 10*time.Millisecond)
r, _ := http.NewRequest("GET", ts.URL+"?name=doe", nil)
_, err := chc.Do(r)