/* * The entry has changed, so remove ourselves from the cache. */ void DependencyChanged(Object sender, EventArgs e) { if (State == EntryState.AddedToCache) { _cache.Remove(this, CacheItemRemovedReason.DependencyChanged); } }
internal void FlushExpiredItems() { Debug.Trace("CacheExpiresFlush", "Flushing expired items, bucket=" + _bucket + ",now=" + DateTime.UtcNow.ToLocalTime()); ArrayList entriesToRemove = new ArrayList(); DateTime utcNow = DateTime.UtcNow; _lock.AcquireWriterLock(); try { if (_entries == null) { return; } // create list of expired items int c = _cInUse; for (int i = 0; c > 0; i++) { CacheEntry cacheEntry = _entries[i].cacheEntry; if (cacheEntry != null) { c--; if (_entries[i].utcExpires <= utcNow) { entriesToRemove.Add(cacheEntry); RemoveEntryAtIndex(cacheEntry, i); } } } Debug.Validate("CacheValidateExpires", this); } finally { _lock.ReleaseWriterLock(); } // remove expired items from cache CacheInternal cacheInternal = _cacheExpires.CacheInternal; foreach (CacheEntry cacheEntry in entriesToRemove) { Debug.Trace("CacheExpiresFlush", "Flushed item " + cacheEntry.Key + " from bucket " + _bucket); cacheInternal.Remove(cacheEntry, CacheItemRemovedReason.Expired); } }
public override object Remove(string key, CacheItemRemovedReason reason) { CacheKey cacheKey = new CacheKey(key, _isPublic); return(_cacheInternal.Remove(cacheKey, reason)); }
internal int FlushUnderUsedItems(int maxFlush) { if (_cInUse == 0) { return(0); } Debug.Assert(maxFlush > 0, "maxFlush is not greater than 0, instead is " + maxFlush); int flushed = 0; ArrayList entriesToRemove = new ArrayList(); DateTime utcNow = DateTime.UtcNow; int prev, prevNext; DateTime utcDate; int index; lock (this) { // walk the list backwards, removing items for (prev = _entries[0].ref1.prev; prev != 0 && flushed < maxFlush && _cInUse > 0; prev = prevNext) { // set prevNext before possibly freeing an item prevNext = _entries[-prev].ref2.prev; while (prevNext > 0) { Debug.Assert(prevNext != 0, "prevNext != 0"); prevNext = _entries[prevNext].ref1.prev; } // look only at ref2 items Debug.Assert(prev < 0, "prev < 0"); index = -prev; utcDate = _entries[index].utcDate; Debug.Assert(utcDate != DateTime.MinValue, "utcDate != DateTime.MinValue"); if (utcNow - utcDate > CacheUsage.NEWADD_INTERVAL) { CacheEntry cacheEntry = _entries[index].cacheEntry; Debug.Trace("CacheUsageFlushUnderUsedItem", "Flushing underused items, item=" + cacheEntry.Key + ", bucket=" + _bucket); entriesToRemove.Add(cacheEntry); flushed++; } Debug.Assert(-_entries.Length < prevNext && prevNext < _entries.Length, "-_entries.Length < prevNext && prevNext < _entries.Length"); } Debug.Validate("CacheValidateUsage", this); Debug.Dump("CacheUsageFlush", this); } CacheInternal cacheInternal = _cacheUsage.CacheInternal; foreach (CacheEntry cacheEntry in entriesToRemove) { cacheInternal.Remove(cacheEntry, CacheItemRemovedReason.Underused); } Debug.Trace("CacheUsageFlushUnderUsedTotal", "Removed " + entriesToRemove.Count + " underused items; Time=" + DateTime.UtcNow.ToLocalTime()); return(flushed); }
internal static void PollDatabaseForChanges(DatabaseNotifState dbState, bool fromTimer) { SqlDataReader reader = null; SqlConnection sqlConn = null; SqlCommand sqlCmd = null; CacheInternal cacheInternal = HttpRuntime.CacheInternal; bool flag = false; Exception exception = null; SqlException exception2 = null; if (s_shutdown) { return; } if (((dbState._refCount == 0) && fromTimer) && dbState._init) { return; } if (Interlocked.CompareExchange(ref dbState._rqInCallback, 1, 0) != 0) { int num2; if (fromTimer) { return; } HttpContext current = HttpContext.Current; if (current == null) { num2 = 30; } else { num2 = Math.Max(current.Timeout.Seconds / 3, 30); } DateTime time = DateTime.UtcNow.Add(new TimeSpan(0, 0, num2)); do { if (Interlocked.CompareExchange(ref dbState._rqInCallback, 1, 0) == 0) { goto Label_00EA; } Thread.Sleep(250); if (s_shutdown) { return; } }while (Debugger.IsAttached || (DateTime.UtcNow <= time)); throw new HttpException(System.Web.SR.GetString("Cant_connect_sql_cache_dep_database_polling", new object[] { dbState._database })); } Label_00EA: try { try { Interlocked.Increment(ref s_activePolling); dbState.GetConnection(out sqlConn, out sqlCmd); reader = sqlCmd.ExecuteReader(); if (!s_shutdown) { flag = true; Hashtable hashtable = (Hashtable)dbState._tables.Clone(); while (reader.Read()) { string table = reader.GetString(0); int num = reader.GetInt32(1); string moniterKey = GetMoniterKey(dbState._database, table); object obj2 = cacheInternal[moniterKey]; if (obj2 == null) { cacheInternal.UtcAdd(moniterKey, num, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); dbState._tables.Add(table, null); } else if (num != ((int)obj2)) { cacheInternal.UtcInsert(moniterKey, num, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null); } hashtable.Remove(table); } foreach (object obj3 in hashtable.Keys) { dbState._tables.Remove((string)obj3); cacheInternal.Remove(GetMoniterKey(dbState._database, (string)obj3)); } if (dbState._pollSqlError != 0) { dbState._pollSqlError = 0; } } } catch (Exception exception3) { exception = exception3; exception2 = exception3 as SqlException; if (exception2 != null) { dbState._pollSqlError = exception2.Number; } else { dbState._pollSqlError = 0; } } finally { try { if (reader != null) { reader.Close(); } dbState.ReleaseConnection(ref sqlConn, ref sqlCmd, exception != null); } catch { } lock (dbState) { dbState._pollExpt = exception; if ((dbState._notifEnabled && !flag) && ((exception != null) && (dbState._pollSqlError == 0xafc))) { foreach (object obj4 in dbState._tables.Keys) { try { cacheInternal.Remove(GetMoniterKey(dbState._database, (string)obj4)); } catch { } } dbState._tables.Clear(); } dbState._notifEnabled = flag; dbState._utcTablesUpdated = DateTime.UtcNow; } if (!dbState._init) { dbState._init = true; } Interlocked.Decrement(ref s_activePolling); Interlocked.Exchange(ref dbState._rqInCallback, 0); } } catch { throw; } }