Tutorial :I'm losing all my cache… (Items are disapearing from my cache)


I'm currently developing a site in ASP.NET Webforms..

I'm caching things where it makes sence... adding things using High / Normal / Low priority.. telling them to stay in the cache for 2 weeks, 1 week, 4 hours respectably

im showing the current number of cached items on every page (for debug reasons).

sometimes if i travel through the site quickly the number of items in the cache can get upto 2000 items... but if i wait 5 mins and refresh the page the cache goes down to 20 items.. (just what was cached on that page)

is there any way i can find our what's going on? and is there a reason for this that im missing?

im running Win7, 4Gb ram, 64 bit, VS10, .net4,

i have 4 gigs of ram so why should my cache completely empty?

i would say 10% of the cached items are about 4k each the rest would be strings about 100 chars long.

EDIT: I'm using Sliding Expiration

EDIT: I sorted it out, there were one or two items that were VERY BIG and they were set to High Priority.. that and some other smaller changes fixed my problem.


It won't matter how long you tell it to stay in the cache if the app pool is set to recycle every xx minutes. On most Servers the app pool recycles by default every 1440 minutes (24 hours). Your cache can't survive that app pool recycle unless you persist the data.

I don't persist that long (almost never really practical). I also set the app that if the cache has been purged to rebuild itself AS IT NEEDS THE DATA - not all at once.

You could also be using more RAM than the app pool is configured to allow, too much CPU, etc.

And if this is running on a developer machine everytime you reset the app or reload Visual Studio everything is also erased as well.

Look at your App Pools and you will probably see the culprit for the resets.


Just because the system has 4gb RAM does not mean an ASP.NET process can use all of that space, nor does it mean the ASP.NET cache can use all of that space effectively. As I understand and have observed the beast, it will start to clear itself out if it feels it is getting too full, and it sounds like you are caching gads of data.

The way to track this is to use the cache expiration callbacks function to log stuff. It should be able to give you an idea of what is getting dumped and when and perhaps why.


It's likely that your app pool workers are recycling (idle or otherwise) and thus starting up with an empty cache.


Do you have an absolute or sliding expiration set?

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »