缓存
This commit is contained in:
parent
75f6afcddc
commit
a8a0a1810f
8
cache/cache.go
vendored
8
cache/cache.go
vendored
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user