/// <summary> /// Implemented in derived classes to add a new entry to the cache /// </summary> /// <param name="key">The key for the new entry</param> /// <param name="data">The data for the new entry</param> /// <param name="cachePriority">The entry priority</param> /// <returns>The newly created cache entry</returns> protected override AbstractCacheEntry AddEntry(string key, byte[] data, CachePriority cachePriority) { var entry = new MemoryCacheEntry(key, data, cachePriority); _cache.AddOrUpdate(key, entry, (k, e) => entry); return(entry); }
public int Compare(MemoryCacheEntry entry1, MemoryCacheEntry entry2) { var ret = DateTime.Compare (entry1.UtcLastUpdateUsage , entry2.UtcLastUpdateUsage); if (ret == 0) return entry1.UsageEntryRef.DateTimeIndex - entry2.UsageEntryRef.DateTimeIndex; return ret; }
public int Compare(MemoryCacheEntry entry1, MemoryCacheEntry entry2) { var ret = DateTime.Compare(entry1.UtcLastUpdateUsage, entry2.UtcLastUpdateUsage); if (ret == 0) { return(entry1.UsageEntryRef.DateTimeIndex - entry2.UsageEntryRef.DateTimeIndex); } return(ret); }
/// <summary> /// Clears the ClockCache of all entries. /// </summary> public void Clear() { m_readerWriterLock.EnterWriteLock(); try { m_currentSize = 0; m_beforeClockHand = null; m_dictionary.Clear(); } finally { m_readerWriterLock.ExitWriteLock(); } }
public new void Add (MemoryCacheEntry entry) { var now = DateTime.UtcNow; if (now == prevDateTime) dateTimeIndex++; else dateTimeIndex = 0; prevDateTime = now; entry.UtcLastUpdateUsage = now; entry.UsageEntryRef = new UsageEntryRef (); entry.UsageEntryRef.DateTimeIndex = dateTimeIndex; base.Add (entry); }
public void Add(MemoryCacheEntry entry) { var now = DateTime.UtcNow; if (now == prevDateTime) { dateTimeIndex++; } else { dateTimeIndex = 0; } prevDateTime = now; entry.UtcLastUpdateUsage = now; entry.UsageEntryRef = new UsageEntryRef(); entry.UsageEntryRef.DateTimeIndex = dateTimeIndex; base.Add(entry); }
/// <summary> /// Removes a specific key from the cache. /// </summary> /// <param name="key">Key to remove</param> /// <returns>True if the key was removed from the cache</returns> public bool Remove(TKey key) { while (!m_readerWriterLock.TryEnterReadLock(0)) { } try { MemoryCacheEntry <TKey, TValue> entry; if (!m_dictionary.TryGetValue(key, out entry)) { return(false); } // Unlink the entry. entry.Previous.Next = entry.Next; entry.Next.Previous = entry.Previous; m_dictionary.Remove(entry.Key); m_currentSize--; if (ReferenceEquals(entry, m_beforeClockHand)) { // We removed what beforeClockHand pointed to, // so advance it to an extant entry. m_beforeClockHand = m_beforeClockHand.Next; if (m_currentSize == 0) { // Cache is now empty. m_beforeClockHand = null; } } return(true); } finally { m_readerWriterLock.ExitReadLock(); } }
/// <summary> /// Adds the specified key and value to the dictionary. /// </summary> /// <param name="key">Key to add.</param> /// <param name="value">Value to associate with key.</param> /// <param name="overwrite">If true, will overwrite an existing key.</param> private void Add(long key, TValue value, bool overwrite) { if (null != this.readerWriterLock) { this.readerWriterLock.EnterWriteLock(); } try { MemoryCacheEntry extantEntry; if (this.dictionary.TryGetValue(key, out extantEntry)) { if (!overwrite) { return; } // We already have an entry for this key. Update the entry // with the new value and exit. extantEntry.Value = value; } else if (this.dictionary.Count < this.maxSize) { // The cache is still growing -- we do not need an eviction to add this entry. MemoryCacheEntry newEntry = new MemoryCacheEntry(key, value); this.dictionary.Add(key, newEntry); this.sortedList.Add(newEntry); } } finally { if (null != this.readerWriterLock) { this.readerWriterLock.ExitWriteLock(); } } }
public void Update(MemoryCacheEntry entry) { base.Remove(entry); entry.UtcLastUpdateUsage = DateTime.UtcNow; base.Add(entry); }
public void Remove(MemoryCacheEntry entry) { base.Remove(entry); entry.UsageEntryRef = UsageEntryRef.INVALID; }
public DateTime GetDateTime(MemoryCacheEntry entry) { return(entry.UtcLastUpdateUsage); }
public void Update (MemoryCacheEntry entry) { base.Remove (entry); entry.UtcLastUpdateUsage = DateTime.UtcNow; base.Add (entry); }
public new void Remove (MemoryCacheEntry entry) { base.Remove (entry); entry.UsageEntryRef = UsageEntryRef.INVALID; }
public DateTime GetDateTime (MemoryCacheEntry entry) { return entry.UtcLastUpdateUsage; }
/// <summary> /// Adds the specified key and value to the dictionary. /// </summary> /// <param name="key">Key to add</param> /// <param name="value">Value to associate with key</param> /// <param name="overwrite">If true, will overwrite an existing key</param> /// <returns>True if the key was successfully added; false if the key was already in the dictionary</returns> private bool AddHelper(TKey key, TValue value, bool overwrite) { m_readerWriterLock.EnterWriteLock(); try { MemoryCacheEntry <TKey, TValue> extantEntry; if (m_dictionary.TryGetValue(key, out extantEntry)) { if (!overwrite) { return(false); } // We already have an entry for this key. Update the entry // with the new value and exit. extantEntry.Value = value; } else if (m_currentSize < m_maxSize) { // The cache is still growing -- we do not need an eviction to add this entry. MemoryCacheEntry <TKey, TValue> newEntry = new MemoryCacheEntry <TKey, TValue>(key, value); m_dictionary[key] = newEntry; if (null == m_beforeClockHand) { // currentSize transitioning from 0 to 1. newEntry.Next = newEntry; newEntry.Previous = newEntry; } else { // currentSize transitioning from >= 1 to maxSize. newEntry.Next = m_beforeClockHand.Next; newEntry.Previous = m_beforeClockHand; m_beforeClockHand.Next.Previous = newEntry; m_beforeClockHand.Next = newEntry; } m_beforeClockHand = newEntry; m_currentSize++; Debug.Assert(m_currentSize == m_dictionary.Count); } else { // The cache is full and we need to evict in order to add this entry. // beforeClockHand.Next is the clock hand in the clock replacement algorithm. MemoryCacheEntry <TKey, TValue> clockHand = m_beforeClockHand.Next; while (clockHand.Referenced) { clockHand.Referenced = false; clockHand = clockHand.Next; } // clockHand now points to the entry we will evict. We will re-use // the existing cache entry. m_dictionary.Remove(clockHand.Key); clockHand.Key = key; clockHand.Value = value; clockHand.Referenced = false; m_dictionary[key] = clockHand; // Save the new clock hand position. m_beforeClockHand = clockHand; } } finally { m_readerWriterLock.ExitWriteLock(); } return(true); }
/// <summary> /// Implemented in derived classes to add a new entry to the cache /// </summary> /// <param name="key">The key for the new entry</param> /// <param name="data">The data for the new entry</param> /// <param name="cachePriority">The entry priority</param> /// <returns>The newly created cache entry</returns> protected override AbstractCacheEntry AddEntry(string key, byte[] data, CachePriority cachePriority) { var entry = new MemoryCacheEntry(key, data, cachePriority); _cache.AddOrUpdate(key, entry, (k, e) => entry); return entry; }