This commit is contained in:
xing 2022-09-19 22:02:34 +08:00
parent 75f6afcddc
commit a8a0a1810f

8
cache/cache.go vendored
View File

@ -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)