private void ExpireTime(object timer) { lock (m_cache) { List <KeyType> expirations = new List <KeyType>(); foreach (KeyValuePair <KeyType, CacheEntry> kv in m_cache) { TimedCacheEntry ce = kv.Value as TimedCacheEntry; if (m_stopwatch.Elapsed - ce.InsertionTime > m_entryTtl) { expirations.Add(kv.Key); } } foreach (KeyType key in expirations) { CacheEntry ce = m_cache[key]; if (ce.Value is IDisposable) { IDisposable disposableValue = (IDisposable)ce.Value; disposableValue.Dispose(); } m_cache.Remove(key); } } m_timer.Change(m_entryTtl, TimeSpan.FromMilliseconds(-1)); }
private void ClearOutdatedEntries() { while (true) { DateTime minDate = DateTime.Now.AddSeconds(-_timeLimit); List <String> keys = new List <String>(); lock (_cache) { foreach (string key in _cache.Keys) { TimedCacheEntry <T> timedCacheEntry = _cache[key]; if (timedCacheEntry.LastAccessed <= minDate) { keys.Add(key); } } if (keys.Count > 0) { foreach (string key in keys) { _cache.Remove(key); } } } Thread.Sleep(_checkInterval); } }
public string Store(string key, T item) { lock (_cache) { _cache[key] = new TimedCacheEntry <T>(item); } return(key); }
public ValueType Get(KeyType key, bool refreshTimer) { lock (m_cache) { if (refreshTimer) { TimedCacheEntry ce = m_cache[key] as TimedCacheEntry; ce.InsertionTime = m_stopwatch.Elapsed; return(ce.Value); } return(m_cache[key].Value); } }
public T Retrieve(string key) { T item = default(T); lock (_cache) { if (_cache.ContainsKey(key)) { TimedCacheEntry <T> timedCacheEntry = _cache[key]; item = timedCacheEntry.Item; if (_removeOnRetrieve) { _cache.Remove(key); } else { timedCacheEntry.LastAccessed = DateTime.Now; } } } return(item); }