One of the most frequent criticisms of the ASP.NET stack is that it’s less efficient than some of its open-source counterparts. However, Microsoft has actually built in a lot of neat technology into the framework related to caching that allows for much more scalability than you might think. Not only is there caching support built directly into ASP.NET, but there are actually 3 different ways to cache an ASP.NET page and related data.
The Output Cache
Using the output cache is probably the easiest implementation of caching in ASP.NET because all you need to do is add a line to your header. This will essentially cache the entire page. This way, when you load the page a second time, it will serve up the generated HTML of the cached page rather than by going through the page lifecycle and reloading the page.
Here’s the line of code you need to add to the top of your .ASPX pages to implement the Output Cache:
<%@ OutputCache Duration="60" VaryByParam="*" %>
The “Duration” property indicates the number of seconds that the cache is good for. The “VaryByParam” property allows you to create separate cached versions of the page if it’s given different querystrings. For example, if you had a content management system where your Page’s looked like http://www.example.org/Default.aspx?PageID=85, you wouldn’t want that to show up the same as http://www.example.org/Default.aspx?PageID=92. They’re separate dynamic pages on the site that’s generated from the page. You don’t want to cache one sub-page then serve that cache for the entire site.
The Data Cache
If you need to cache any sort of information in ASP.NET, such as a DataSet, a DataTable, or even a variable, you can use the ASP.NET DataCache. This works a lot like a session or application variable, but it has some added cache-related methods as well.
Here’s how to store something in the cache in C#:
DataTable dtToBeCached = new DataTable();
Cache["CachedTable"] = dtToBeCached;
Here’s how to turn that cached object back into a DataTable:
DataTable dtToBeCached = new DataTable();
if(Cache["CachedTable"] != null)
{
dtToBeCached = (DataTable)Cache["CachedTable"];
}
If you would like to remove something from the cache at a later time, you can simply use the Cache.Remove() method.
Partial Page Caching:
There are some cases, such as when having a website that allows for users to login, that you would want to cache some parts of page but not the entireity. Let’s say that you cached a page where one user was authenticated and that paged was cached. Then, the authenticated version of that page would be served to everyone, including individuals that aren’t logged in. In that case, you don’t want to use the Output cache onthe entire page.
Instead, you want to make use of the DataCache to store shared data that would be frequently read from the database. The other strategy you can use in this case is to cache ASP.NET user controls that you develop. You cache these much in the same way that you would cache a page on a website, but instead you would add that code to the user control’s header instead of the page’s header.
You might also want to read these articles related to ASP.NET Caching:
- ASP.NET Caching: Techniques and Best Practices (MSDN)
- Advanced Caching Techniques in ASP.NET 2.0 (DevCity)
- Caching Techniques in ASP.NET 2.0 (Bean Software).
- Caching with ASP.NET (4 Guys From Rolla)