Delete all expired items from the cache.
()
| 947 | |
| 948 | // Delete all expired items from the cache. |
| 949 | func (c *cache) DeleteExpired() { |
| 950 | var evictedItems []keyAndValue |
| 951 | now := time.Now().UnixNano() |
| 952 | c.mu.Lock() |
| 953 | for k, v := range c.items { |
| 954 | // "Inlining" of expired |
| 955 | if v.Expiration > 0 && now > v.Expiration { |
| 956 | ov, evicted := c.delete(k) |
| 957 | if evicted { |
| 958 | evictedItems = append(evictedItems, keyAndValue{k, ov}) |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | c.mu.Unlock() |
| 963 | for _, v := range evictedItems { |
| 964 | c.onEvicted(v.key, v.value) |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | // Sets an (optional) function that is called with the key and value when an |
| 969 | // item is evicted from the cache. (Including when it is deleted manually, but |