/// <summary> /// Forces a revalidation of all items in the cache and removes exeeded items /// </summary> public void Cleanup() { if (_cacheItems.IsEmpty) { return; } var utcNow = DateTime.UtcNow; var discardedItems = new List <CacheItem <TKey, TValue> >(); foreach (var cacheItem in _cacheItems.Values) { if ((cacheItem.Policy == null) || (cacheItem.Policy.HasExpired(utcNow) == false)) { continue; } discardedItems.Add(cacheItem); _cacheItems.TryRemove(cacheItem.Key, out _); } if (discardedItems.Count > 0) { ItemsDiscarded?.Invoke(this, new CacheItemsDiscardedEventArgs <TKey, TValue>(discardedItems)); } }
/// <summary> /// Removes all items of the cache /// </summary> public void Clear() { var discardedItems = _cacheItems.Values.ToList(); _cacheItems.Clear(); if (discardedItems.Count > 0) { ItemsDiscarded?.Invoke(this, new CacheItemsDiscardedEventArgs <TKey, TValue>(discardedItems)); } }
/// <summary> /// Removes a value by its key /// </summary> public bool Discard(TKey key) { if (_cacheItems.TryRemove(key, out var item) == false) { return(false); } ItemsDiscarded?.Invoke(this, new CacheItemsDiscardedEventArgs <TKey, TValue>(new[] { item })); return(true); }