Looping through keys in ASP.NET cache object?

The cache is just a single thing, so you can't "foreach" over this - you need to loop over a collection, such as the cache's keys.

Up vote 1 down vote favorite share g+ share fb share tw.

Caching in ASP. NET looks like it uses some kind of associative array: // Insert some data into the cache: Cache. Insert("TestCache", someValue); // Retrieve the data like normal: someValue = Cache.

Get("TestCache"); // But, can be done associatively ... someValue = Cache"TestCache"; // Also, null checks can be performed to see if cache exists yet: if(Cache"TestCache" == null) { Cache. Insert(PerformComplicatedFunctionThatNeedsCaching()); } someValue = Cache"TestCache"; As you can see, performing a null check on the cache object is very useful. But I would like to implement a cache clear function that can clear cache values where I don't know the whole key name.

As there seems to be an associative array here, it should be possible (?) Can anyone help me work out a way of looping through the stored cache keys and performing simple logic on them? Here's what I'm after: static void DeleteMatchingCacheKey(string keyName) { // This foreach implementation doesn't work by the way ... foreach(Cache as c) { if(c.Key. Contains(keyName)) { Cache.

Remove(c); } } } asp.net caching associative-array link|improve this question edited Aug 13 '11 at 4:34OffBySome15.6k52646 asked Nov 29 '10 at 10:13Greg4,01711031 90% accept rate.

The cache is just a single thing, so you can't "foreach" over this - you need to loop over a collection, such as the cache's keys. I think what you'd need to do is something like this: foreach (var key in Cache. Keys) { if(key.

Contains(keyName)) { Cache. Remove(key); } } But it's never a good idea to modify a collection whilst looping over it. Maybe instead build up a list of keys to remove in the loop, and then loop over this list, removing each key in turn.

Like this: List keysToRemove = new List(); foreach (var key in Cache. Keys) { if(key. Contains(keyName)) { keysToRemove.

Add(key); } } foreach (var key in keysToRemove) { Cache. Remove(key); } Or do the ol' reverse loop thing described in this answer.

2 System.Web.Caching. Cache class does NOT contain Keys property (checked in . NET 4.0).

You can go through Enumerator only. See stackoverflow.com/questions/704765/… – Cheburek Aug 5 '11 at 10:58 @Cheburek: you are quite correct! Maybe I was thinking of the Session.

I wonder why my answer was accepted? – Graham Clark Aug 5 '11 at 12:02.

Don't use a foreach loop when removing items from any collection type- the foreach loop relies on using an enumerator which will NOT allow you to remove items from the collection (the enumerator will throw an exception if the collection it is iterating over has items added or removed from it). Use a simple while to loop over the cache keys instead: int I = 0; while (i Keys(i). Contaains(keyName){ Cache.

Remove(Cache. Keys(i)) } else{ I ++; } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions