Multimobile Development: Building Applications for any Smartphone
Clearing the Cache Programmatically
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.caching.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Frank Buchan
Hello, and thanks ahead of time for any wisdom you can offer.

I have a very large web application that is working fine, and I use the
Cache object to selectively cache contents of pick lists (derived from a
database) and refresh those cache items (Remove them) whenever the list
changes. This is working well, and shows excellent performance on the
hardware we have in place.

One problem I have run into is that at least once the Cache has gone
slightly out of synch, causing the cached list to be incorrect. While we
don't anticipate this happening very often (once in 3 months thus far, and
then only for oddball reasons that are unlikely to repeat), I would like
very much to find a way to iterate through the Cache and remove all items
without having to kill the ASPNET worker process.

I know that using a For Each construct I can walk the cache and examine
which controls are cached, etc., but what I have been unable to do is
iterate and pick out the key names I have assigned to the Cache object. I
would greatly appreciate if anyone knows either a way to clear the Cache
entirely in one method call (doesn't seem to be one mentioned in the help
system), or what the actual syntax in VB.NET or C# would be to iterate, and
remove each item by its key (the Remove method I grasp, but its getting the
valid keys that is the head scratcher). Since this is going to happen very
seldom, I don't anticipate the need for speed, so much as achievement of the
basic result desired.

Again, thanks for any insights.

Reply to this message...
Vote that this is a GOOD answer...
 
Really good experience at the Apple Store
MonoDroid – looking *awesome*
 
    
James Musson [MSFT] (VIP)
Hi Frank

Another option is to make all of your cache keys dependent one key that you
then use to invalidate them when the time comes. See
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebcachingcach
eclassinserttopic4.asp?frame=true specifically CacheDependency.

Regards

James.

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Frank Buchan" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Frank Buchan
Excellent, James.

This is a much simpler method. I never would have thought of it. I knew the
dependency could point to an file, but wasn't even aware it could point to
another key. :)

"James Musson [MSFT]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
 
    
Sune Pedersen
[Original message clipped]

Hi Frank,

Here is an example, doing the iteration and removing the objects, but be
aware that many other objects are stored in the cache object, im not sure if
removing the objects might cause problems in your environment.

Cache("mycachekey") = "Hello Cache"
Dim ie As IEnumerator
ie = Cache.GetEnumerator
While ie.MoveNext
Dim d As System.Collections.DictionaryEntry
d = CType(ie.Current, DictionaryEntry)
Cache.Remove(d.Key.ToString)
End While
Response.Write(Cache("mycachekey").ToString)

Best Regards
Witcom
Sune Pedersen
GSM: +45 21 91 39 46
Phone: +45 75 52 12 57
E-mail Click here to reveal e-mail address
Internet http://www.witcom.dk

Reply to this message...
Vote that this is a GOOD answer...
 
First volume of Multimobile Development nearly ready to go to press
A mention on Developing for the iPhone and Android: The pros and cons
 
    
Frank Buchan
Thanks, Sune:

Luckily, I anticipated the cache holding more than just my items and
prefixed my cache items with a unique prefix. I modified the code you sent
just slightly to reflect that, and it works like a charm. Now, of course,
I'm almost guaranteed no one ever needs it again. :)

For anyone else who wants this same safe cache clearing mechanism, the
modification was to replace the line:

Cache.Remove(d.Key.ToString)

with...

Dim checkKey as String = d.Key.ToString()
If InStr(checkKey,"uniquePrefix") > 0 then
Cache.Remove(checkKey)
End If

As this was a performance insensitive process, this solution seems to work
great...and it performs fairly well, also.

Again, much thanks.

Frank Buchan

"Sune Pedersen" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
Vote that this is a GOOD answer...
 
First chapters of Multimobile Development book now available on Apress Alpha program
iPad
 
 
System.Collections.DictionaryEntry
System.Collections.IEnumerator
System.Web.Caching.Cache
System.Web.Caching.CacheDependency




Ad
BootFX
Reliable and powerful .NET application framework.
iOS, Android and Windows Phone Development Training and Consultancy
Hosted by RackSRV Communications
 
Multimobile Development: Building Applications for any Smartphone
Copyright © AMX Software Ltd 2008-2010. Portions copyright © Matthew Baxter-Reynolds 2001-2010. All rights reserved.
Contact Us - Terms of Use - Privacy Policy - 4.0.30129.1734