cacheExpire expires any entries that haven't been used recently
()
| 219 | |
| 220 | // cacheExpire expires any entries that haven't been used recently |
| 221 | func (c *Cache) cacheExpire() { |
| 222 | c.mu.Lock() |
| 223 | defer c.mu.Unlock() |
| 224 | now := time.Now() |
| 225 | for key, entry := range c.cache { |
| 226 | if entry.pinCount <= 0 && now.Sub(entry.lastUsed) > c.expireDuration { |
| 227 | c.finalize(entry.value) |
| 228 | delete(c.cache, key) |
| 229 | } |
| 230 | } |
| 231 | if len(c.cache) != 0 { |
| 232 | time.AfterFunc(c.expireInterval, c.cacheExpire) |
| 233 | c.expireRunning = true |
| 234 | } else { |
| 235 | c.expireRunning = false |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // Clear removes everything from the cache |
| 240 | func (c *Cache) Clear() { |