mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 03:27:39 +08:00
feat: clean cache upon indexer setting changes
This commit is contained in:
@@ -1,8 +1,14 @@
|
|||||||
package torznab
|
package torznab
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"polaris/log"
|
||||||
"polaris/pkg/cache"
|
"polaris/pkg/cache"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var cc = cache.NewCache[string, *Response](time.Minute * 30)
|
var cc = cache.NewCache[string, []Result](time.Minute * 30)
|
||||||
|
|
||||||
|
func CleanCache() {
|
||||||
|
log.Debugf("clean all torznab caches")
|
||||||
|
cc = cache.NewCache[string, []Result](time.Minute * 30)
|
||||||
|
}
|
||||||
@@ -81,7 +81,7 @@ func (r *Response) ToResults(indexer *db.TorznabInfo) []Result {
|
|||||||
if slices.Contains(item.Category, "3000") { //exclude audio files
|
if slices.Contains(item.Category, "3000") { //exclude audio files
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
link, err := utils.Link2Magnet(item.Link)
|
link, err := utils.Link2Magnet(item.Link) //TODO time consuming operation
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("converting link to magnet error, error: %v, link: %v", err, item.Link)
|
log.Warnf("converting link to magnet error, error: %v, link: %v", err, item.Link)
|
||||||
continue
|
continue
|
||||||
@@ -141,15 +141,18 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
|||||||
|
|
||||||
cacheRes, ok := cc.Get(key)
|
cacheRes, ok := cc.Get(key)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
log.Debugf("not found in cache, need query again: %v", key)
|
||||||
res, err := doRequest(req)
|
res, err := doRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cc.Set(key, &Response{})
|
cc.Set(key, nil)
|
||||||
return nil, errors.Wrap(err, "do http request")
|
return nil, errors.Wrap(err, "do http request")
|
||||||
}
|
}
|
||||||
cacheRes = res
|
cacheRes = res.ToResults(indexer)
|
||||||
cc.Set(key, cacheRes)
|
cc.Set(key, cacheRes)
|
||||||
|
} else {
|
||||||
|
log.Debugf("found cache match for key: %v", key)
|
||||||
}
|
}
|
||||||
return cacheRes.ToResults(indexer), nil
|
return cacheRes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func doRequest(req *http.Request) (*Response, error) {
|
func doRequest(req *http.Request) (*Response, error) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"polaris/ent/downloadclients"
|
"polaris/ent/downloadclients"
|
||||||
"polaris/log"
|
"polaris/log"
|
||||||
"polaris/pkg/qbittorrent"
|
"polaris/pkg/qbittorrent"
|
||||||
|
"polaris/pkg/torznab"
|
||||||
"polaris/pkg/transmission"
|
"polaris/pkg/transmission"
|
||||||
"polaris/pkg/utils"
|
"polaris/pkg/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -171,6 +172,8 @@ func (s *Server) AddTorznabInfo(c *gin.Context) (interface{}, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "add ")
|
return nil, errors.Wrap(err, "add ")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
torznab.CleanCache() //need to clean exist cache, so next request will do actaul query
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user