From a8a0a1810fa8b24fa8479e7c9fbcc1dbae8c17f8 Mon Sep 17 00:00:00 2001 From: xing Date: Mon, 19 Sep 2022 22:02:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cache/cache.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cache/cache.go b/cache/cache.go index f07e545..97da17f 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -22,10 +22,16 @@ func NewSliceCache[T any](fun func() ([]T, error), duration time.Duration) *Slic } } +func (c *SliceCache[T]) FlushCache() { + c.mutex.Lock() + defer c.mutex.Unlock() + c.data = nil +} + func (c *SliceCache[T]) GetCache() []T { l := len(c.data) expired := time.Duration(c.setTime.Unix())+c.expireTime/time.Second < time.Duration(time.Now().Unix()) - if l > 0 && expired || l < 1 { + if l < 1 || (l > 0 && c.expireTime > 0 && expired) { r, err := c.setCacheFunc() if err != nil { log.Printf("set cache err[%s]", err)