From 1aa3dca2c63030651d8e6a4bbe24fef7604612f6 Mon Sep 17 00:00:00 2001 From: Simon Ding Date: Wed, 7 Aug 2024 11:20:21 +0800 Subject: [PATCH] update --- pkg/cache/cache.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 552c7d9..738667c 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -4,35 +4,20 @@ 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 { @@ -45,6 +30,15 @@ func (c *Cache[T, S]) Set(key T, value S) { } func (c *Cache[T, S]) Get(key T) (S, bool) { + 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 + }) + v, ok := c.m.Load(key) if !ok { return getZero[S](), ok