Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Getting Bit by Caching

0.00/5 (No votes)
1 May 2009CPOL3 min read 8.5K  
Why it is important to be careful when using the Caching feature.

I must confess, I’ve been bit by ASP.NET caching a couple of times now. It’s not that there’s anything wrong with ASP.NET caching—there isn’t. In fact, it’s not that ASP.NET caching is not important and cool—it is! But what seems like an easy-to-use feature can actually get fairly complex and can easily be misused on a dynamic Website.

Caching refers to saving a copy of a requested page on the server and, the next time that page is requested, serving that copy rather than recreating that page. The performance improvements can be significant on a busy site, particularly if pages require a lot of code and database queries.

ASP.NET provides the OutputCache directive for caching a page. You provide a duration, in seconds, of how long until the cache should be discarded. Once the cache is discarded, any new request for that page will cause the page to be recreated.

ASP.NET
<%@ OutputCache Duration="60" VaryByParam="none" %>

ASP.NET OutputCache directive

There are a lot of options to this directive. For starters, it can appear on pages other than ASPX pages. You can add it to master pages, web controls, etc. You can also have the cache unique to a particular query argument or a whole host of other conditions. For example, if a query argument specifies an ID of the item to be displayed, you wouldn’t want to use the same cache for all items. Otherwise, you’d enter a particular ID but would then see whatever item happened to be in the cache.

I won’t go into great detail about all the options available with the OutputCache directive. Perhaps, I will attempt that in another article. Here, I simply want to make you aware that caching can cause some significant problems if you don’t carefully think things through each place you use it.

I downloaded the Personal Website Starter Kit from www.asp.net, a supposedly official Microsoft site. It’s a cool ASP.NET example (aside from a couple of significant problems). But, I was still fairly new to ASP.NET programming and I spent an enormous amount of time trying to figure out why the picture browser page didn’t work reliably. For the most part, it worked. But sometimes, it would not advance to the selected picture. I studied and studied the code, and couldn’t figure out what was happening.

Finally, I started thinking about caching and, sure enough, someone had added a caching directive at the top of each page and the result was that the page sometimes appeared as it did the last time it was viewed rather than being updated with a new picture.

I don’t mean to scare anyone from using caching. I recommend you use it! But if a widely-used, example website that is presumably from Microsoft uses caching incorrectly, then I think it’s safe to say that it’s not always immediately obvious what the ramifications of caching will be. On a dynamic website, content changes. You need to ensure that the user always gets current content, and prevent caching of pages or objects that need to change. And, possibly, if you get bit by caching, maybe you’ll think of this article and not waste as much debugging time as I did.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)