/// <summary> /// Removes item from cache /// </summary> /// <param name="key">Unique key of finding item.</param> public void Remove(TKey key) { if (_Lock.TryEnterWriteLock(_DefaultLockTimeOut)) { RemovedEntry removedItem = null; try { TValue value; if (_Cache.TryGetValue(key, out value)) { bool removed = _Cache.Remove(key); #if DEBUG //ApplicationEventLog.WriteEntry("Flow", string.Format("GenericCache: removing item {0} of type {1} = removed={2}", key, typeof(TValue).Name, removed), EventLogEntryType.Information); #endif removedItem = new RemovedEntry() { Key = key, Value = value }; } } finally { _Lock.ExitWriteLock(); } if ((removedItem != null) && (RemovedItem != null)) { RemovedItem(removedItem.Key, removedItem.Value); } } else { ApplicationEventLog.WriteEntry("Flow", string.Format("GenericCache:Remove - Failed to acquire lock for key={0} ", key), EventLogEntryType.Error); } }
private void ClearSpaceForNewEntry() { if (_cache.Count >= Size) { LinkedListNode <string> rank = _ranking.Last; CacheEntry entry = _cache[rank.Value]; _cache = _cache.Remove(rank.Value); _ranking.Remove(rank); RemovedEntry?.Invoke(this, new CacheEntryEventArgs <TValue>(entry.Key, entry.Value)); } }
private RemovedEntry RemoveOldestRequestedItem() { RemovedEntry result = null; TValue value; TKey key = _KeyQueue[_KeyQueueHeadIndex]; _KeyQueueHeadIndex = (_KeyQueueHeadIndex + 1) % _Capacity; if (_Cache.TryGetValue(key, out value)) { #if DEBUG //ApplicationEventLog.WriteEntry("Flow", string.Format("GenericCache:Removing old item {0} of type {1}", key, typeof(TValue).Name), EventLogEntryType.Information); #endif _Cache.Remove(key); result = new RemovedEntry() { Key = key, Value = value }; } return(result); }
/// <summary> /// Add item to cache /// </summary> /// <param name="key">Unique key of finding item</param> /// <param name="item">Item to add</param> /// <remarks>If already in cache it gets updated</remarks> public void Add(TKey key, TValue item) { if (_Lock.TryEnterWriteLock(_DefaultLockTimeOut)) { RemovedEntry removedItem = null; try { TValue existingItem; if (_Cache.TryGetValue(key, out existingItem)) { _Cache[key] = item; } else if (key != null) { if ((_Count + 1) > _Capacity) { removedItem = RemoveOldestRequestedItem(); } else { _Count++; } _Cache.Add(key, item); _KeyQueueTailIndex = (_KeyQueueTailIndex + 1) % _Capacity; _KeyQueue[_KeyQueueTailIndex] = key; } } finally { _Lock.ExitWriteLock(); } if ((removedItem != null) && (RemovedItem != null)) { RemovedItem(removedItem.Key, removedItem.Value); } } else { ApplicationEventLog.WriteEntry("Flow", string.Format("GenericCache:Add - Failed to acquire lock for key={0} value={1}", key, item), EventLogEntryType.Error); } }
/// <summary> /// Raises the <see cref="RemovedEntry"/> event. /// </summary> /// <param name="e">An <see cref="DotRas.RasPhoneBookDialogEventArgs"/> containing event data.</param> private void OnRemovedEntry(RasPhoneBookDialogEventArgs e) { RemovedEntry?.Invoke(this, e); }