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