Delete all expired items from the cache.
()
| 929 | |
| 930 | // Delete all expired items from the cache. |
| 931 | func (c *cache) DeleteExpired() { |
| 932 | var evictedItems []keyAndValue |
| 933 | now := time.Now().UnixNano() |
| 934 | c.mu.Lock() |
| 935 | for k, v := range c.items { |
| 936 | // "Inlining" of expired |
| 937 | if v.Expiration > 0 && now > v.Expiration { |
| 938 | ov, evicted := c.delete(k) |
| 939 | if evicted { |
| 940 | evictedItems = append(evictedItems, keyAndValue{k, ov}) |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | c.mu.Unlock() |
| 945 | for _, v := range evictedItems { |
| 946 | c.onEvicted(v.key, v.value) |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | // Sets an (optional) function that is called with the key and value when an |
| 951 | // item is evicted from the cache. (Including when it is deleted manually, but |