mirror of
https://github.com/simon-ding/polaris.git
synced 2026-03-08 10:40:45 +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
|
||||
|
||||
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