/// <summary> /// Remove old expired items from the cache /// </summary> /// <remarks> /// </remarks> private void FlushExpired() { // We don't attempt to flush items from the cache when it isn't full, otherwise this could result in a lot // of wasted effort. if (_cache.IsFull) { var keys = _cache.Keys; foreach (var key in keys) { // if we can get the value from the cache but the target of the weak reference is no longer there // then remove from the cache. if (_cache.TryGetValue(key, out var weakReference) && !weakReference.TryGetTarget(out var primitive)) { _cache.Remove(key); } } } }