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 pages 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.
<%@ 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 a ASPX pages. You can also 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 http://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.