mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 19:47:47 +08:00
feat: cache errored request
This commit is contained in:
@@ -5,4 +5,4 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cc = cache.NewCache[string, Response](time.Minute * 30)
|
var cc = cache.NewCache[string, *Response](time.Minute * 30)
|
||||||
|
|||||||
@@ -135,19 +135,10 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
|||||||
|
|
||||||
cacheRes, ok := cc.Get(key)
|
cacheRes, ok := cc.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
resp, err := http.DefaultClient.Do(req)
|
res, err := doRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "do http")
|
cc.Set(key, &Response{})
|
||||||
}
|
return nil, errors.Wrap(err, "do http request")
|
||||||
defer resp.Body.Close()
|
|
||||||
data, err := io.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "read http body")
|
|
||||||
}
|
|
||||||
var res Response
|
|
||||||
err = xml.Unmarshal(data, &res)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrapf(err, "xml unmarshal data: %v", string(data))
|
|
||||||
}
|
}
|
||||||
cacheRes = res
|
cacheRes = res
|
||||||
cc.Set(key, cacheRes)
|
cc.Set(key, cacheRes)
|
||||||
@@ -155,6 +146,24 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
|||||||
return cacheRes.ToResults(indexer), nil
|
return cacheRes.ToResults(indexer), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func doRequest(req *http.Request) (*Response, error) {
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "do http")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
data, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "read http body")
|
||||||
|
}
|
||||||
|
var res Response
|
||||||
|
err = xml.Unmarshal(data, &res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrapf(err, "xml unmarshal data: %v", string(data))
|
||||||
|
}
|
||||||
|
return &res, nil
|
||||||
|
}
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Link string `json:"link"`
|
Link string `json:"link"`
|
||||||
|
|||||||
Reference in New Issue
Block a user