Skip to content

Commit

Permalink
feat: clean cache upon indexer setting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-ding committed Oct 10, 2024
1 parent 19f21dd commit e0d0ab8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 7 additions & 1 deletion pkg/torznab/cache.go
Original file line number Diff line number Diff line change
@@ -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)
}
11 changes: 7 additions & 4 deletions pkg/torznab/torznab.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions server/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"polaris/ent/downloadclients"
"polaris/log"
"polaris/pkg/qbittorrent"
"polaris/pkg/torznab"
"polaris/pkg/transmission"
"polaris/pkg/utils"
"strconv"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit e0d0ab8

Please sign in to comment.