views:

102

answers:

1

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

+5  A: 

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);
Tejs
Yup, this should work. I've used this approach before.
Pandincus
Why use a Dictionary to hold the cache items? You only need to hold the keys; it would seem a List<string> would take care of it.
Jesse C. Slicer
Good point. I was just thinking that I needed to keep that data around because you wouldn't be able to remove during a foreach on the enumerator.
Tejs
+1 - Nice, deleted my answer and will use this from now on.
Kyle Rozendo