ryudice
ryudice

Reputation: 37366

Force all ASP.NET cache to expire

Is there a method or something to force the expiration of all of the entries in the Cache collection of the HttpContext?

Upvotes: 9

Views: 5006

Answers (2)

Vernard Sloggett
Vernard Sloggett

Reputation: 334

what about updating the cache to expire ?

protected void btnClearCache_Click(object sender, EventArgs e)
{

    var enumerator = HttpRuntime.Cache.GetEnumerator();

    while (enumerator.MoveNext())
        HttpRuntime.Cache.Insert(enumerator.Key.ToString(), enumerator.Value, 
            null, DateTime.Now , Cache.NoSlidingExpiration);
}

Upvotes: 0

Tejs
Tejs

Reputation: 41236

Try something like this:

var enumerator = HttpRuntime.Cache.GetEnumerator();
Dictionary<string, object> cacheItems = new Dictionary<string, object>();

while (enumerator.MoveNext())
    cacheItems.Add(enumerator.Key.ToString(), enumerator.Value);

foreach (string key in cacheItems.Keys)
    HttpRuntime.Cache.Remove(key);

Upvotes: 18

Related Questions