mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 03:27:39 +08:00
feat: change cache implementation
This commit is contained in:
@@ -1,31 +1,8 @@
|
||||
package torznab
|
||||
|
||||
import (
|
||||
"polaris/log"
|
||||
"polaris/pkg/utils"
|
||||
|
||||
"github.com/robfig/cron"
|
||||
|
||||
"polaris/pkg/cache"
|
||||
"time"
|
||||
)
|
||||
|
||||
var cache utils.Map[string, TimedResponse] = utils.Map[string, TimedResponse]{}
|
||||
|
||||
type TimedResponse struct {
|
||||
Response
|
||||
T time.Time
|
||||
}
|
||||
|
||||
func init() {
|
||||
cr := cron.New()
|
||||
cr.AddFunc("@ervery 1m", func() {
|
||||
cache.Range(func(key string, value TimedResponse) bool {
|
||||
if time.Since(value.T) > 30*time.Minute {
|
||||
log.Debugf("delete old cache: %v", key)
|
||||
cache.Delete(key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
})
|
||||
cr.Start()
|
||||
}
|
||||
var cc = cache.NewCache[string, Response](time.Minute * 30)
|
||||
|
||||
@@ -128,7 +128,7 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
||||
req.URL.RawQuery = q.Encode()
|
||||
key := fmt.Sprintf("%s: %s", indexer.Name, keyWord)
|
||||
|
||||
cacheRes, ok := cache.Load(key)
|
||||
cacheRes, ok := cc.Get(key)
|
||||
if !ok {
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
@@ -144,8 +144,8 @@ func Search(indexer *db.TorznabInfo, keyWord string) ([]Result, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "json unmarshal")
|
||||
}
|
||||
cacheRes = TimedResponse{Response: res, T: time.Now()}
|
||||
cache.Store(key, cacheRes)
|
||||
cacheRes = res
|
||||
cc.Set(key, cacheRes)
|
||||
}
|
||||
return cacheRes.ToResults(indexer), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user