mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-10 03:57:39 +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,6 +135,18 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
|||||||
|
|
||||||
cacheRes, ok := cc.Get(key)
|
cacheRes, ok := cc.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
res, err := doRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
cc.Set(key, &Response{})
|
||||||
|
return nil, errors.Wrap(err, "do http request")
|
||||||
|
}
|
||||||
|
cacheRes = res
|
||||||
|
cc.Set(key, cacheRes)
|
||||||
|
}
|
||||||
|
return cacheRes.ToResults(indexer), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func doRequest(req *http.Request) (*Response, error) {
|
||||||
resp, err := http.DefaultClient.Do(req)
|
resp, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "do http")
|
return nil, errors.Wrap(err, "do http")
|
||||||
@@ -149,10 +161,7 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "xml unmarshal data: %v", string(data))
|
return nil, errors.Wrapf(err, "xml unmarshal data: %v", string(data))
|
||||||
}
|
}
|
||||||
cacheRes = res
|
return &res, nil
|
||||||
cc.Set(key, cacheRes)
|
|
||||||
}
|
|
||||||
return cacheRes.ToResults(indexer), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Result struct {
|
type Result struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user