I am adding a bunch of items to the ASP.NET cache with a specific prefix. I\'d like to be able to iterate over the cache and remove those items.
The way I\'ve tried to d
You can't remove items from a collection whilst you are iterating over it so you need to do something like this:
List itemsToRemove = new List();
IDictionaryEnumerator enumerator = Cache.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
{
itemsToRemove.Add(enumerator.Key.ToString());
}
}
foreach (string itemToRemove in itemsToRemove)
{
Cache.Remove(itemToRemove);
}
This approach is fine and is quicker and easier than cache dependencies.