Showing posts from December, 2009

ASP.NET Cached Items Being Removed

Lately some of the items I've been placing in cache in ASP.NET have been being removed. When I assigned a callback method to trap these removals, I saw that the reason for removal was "underused" -- ASP.NET was removing these items to free up memory because they weren't being used often.

To prevent this from happening, use the following value for the priority parameter of Cache.Add():

HttpRuntime.Cache vs. HttpContext.Current.Cache in ASP.NET

Lately I'd been using the static Current property of the HttpContext object in ASP.NET to access it's Cache property and cache information in a web app. I was adding these cache entries with dependencies, so that when certain files were modified, the cache would trigger a callback function to reload. However, when testing this, I ran into an unforseen complication: when I modified a file I had specified as a dependancy, and the app tried to add the new data to the cache using HttpContext.Current.Cache, it couldn't because there was no current HttpContext.

The solution was to use HttpRuntime.Cache instead. This, like HttpContext.Current.Cache, represents the cache of the entire app, but does not require a reference to a current HttpContext. Also, I've heard it's a little bit faster.