feat: clean cache upon indexer setting changes

This commit is contained in:
Simon Ding
2024-10-10 09:21:33 +08:00
parent 19f21ddd6e
commit e0d0ab80b6
3 changed files with 17 additions and 5 deletions

View File

@@ -1,8 +1,14 @@
package torznab
import (
"polaris/log"
"polaris/pkg/cache"
"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)
}

View File

@@ -81,7 +81,7 @@ func (r *Response) ToResults(indexer *db.TorznabInfo) []Result {
if slices.Contains(item.Category, "3000") { //exclude audio files
continue
}
link, err := utils.Link2Magnet(item.Link)
link, err := utils.Link2Magnet(item.Link) //TODO time consuming operation
if err != nil {
log.Warnf("converting link to magnet error, error: %v, link: %v", err, item.Link)
continue
@@ -141,15 +141,18 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
cacheRes, ok := cc.Get(key)
if !ok {
log.Debugf("not found in cache, need query again: %v", key)
res, err := doRequest(req)
if err != nil {
cc.Set(key, &Response{})
cc.Set(key, nil)
return nil, errors.Wrap(err, "do http request")
}
cacheRes = res
cacheRes = res.ToResults(indexer)
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) {

View File

@@ -9,6 +9,7 @@ import (
"polaris/ent/downloadclients"
"polaris/log"
"polaris/pkg/qbittorrent"
"polaris/pkg/torznab"
"polaris/pkg/transmission"
"polaris/pkg/utils"
"strconv"
@@ -171,6 +172,8 @@ func (s *Server) AddTorznabInfo(c *gin.Context) (interface{}, error) {
if err != nil {
return nil, errors.Wrap(err, "add ")
}
torznab.CleanCache() //need to clean exist cache, so next request will do actaul query
return nil, nil
}