Introduction
ASP.NET Caching has to be the one the most under utilized feature of ASP.NET . Caching is a process of storing frequently used data on the server to fulfill subsequent requests. The browser kind of does something similar for you on the client side, where it caches the webpage ,images etc to display them much faster the next time. ASP.NET caching does the same thing for you on the server side. The frequently used data is stored in the server memory and as you can imagine memory access is a lot faster than recreating the webpage from scratch. This leads to boosting the performance, scalability and availability of the website.
There are 3 different ways you can use ASP.NET caching : output caching, partial page (UserControl) caching and programmatic caching. In this part, we will look at the output caching.
Output caching
Output caching enables to cache the entire dynamic HTTP page in the server memory and enable to retrieve it from the memory instead of recreating it. This means if you are using a master page for your site, for this particular webpage both the master page content and the content page content will be cached.
To achieve output caching is very simple : you need to add an OutputCache page directive at the top of an aspx page:
<% OutputCache Duration=”120” VaryByParam=”None” %>
The above example directive will store the webpage in cache for 120 seconds. The VaryByParam attribute determines which versions of the page are cached. The other available attributes are : VaryByHeader, VaryByControl, VaryByCustom and Location. Let us take a look at each of them
VaryByParam
You can specify which Querystring parameters will cause a new version to be cached:
<% OutputCache Duration=”120” VaryByParam=”productId;manufacturerId;” %>
The above example means store a separate version of the page in cache for each combination of productId and manufacturerId. Bear in mind the number of cached versions for this page will be (number of distinct productId * number of distinct manufacturerId). You can also specify “*” as the value for the VaryByParam and a separate version will be cached for any differences in the Querystring parameters.
VaryByHeader
You can specify VaryByHeader to save a version for each variation in one of more headers that the browser sends.
<% OutputCache Duration=”120” VaryByHeader=”User-Agent” %>
The above example will store a version for each variation of the user-agent (i.e. browsers) in the cache.
VaryByControl
You can specify the VaryByControl attribute to cache the rendered output of the a UserControl
Location
Defines if page output can be cached on a proxy or downstream client. Often omitted, so that caching is performed in the most efficient way. However, it is a good idea to include Location="Any" where you do not actually need to control the location
VaryByCustom
As with most features in ASP.NET if the standard features do not work for you, you can use your own custom logic which offers the most flexibility. For example, say you want to take the OutputCache to vary by a value in a cookie or a session variable or whatever you can use VaryByCustom. There is a special method GetVaryByCustomString that can be added to the Global.asax.
The GetVaryByCustomString gets called each time a page is requested and the function returns a value. A different version of the page is cached for each unique value is returned. A good example is suppose you want to cache a fairly static page but if this page (or its master page) stores the referrer of the page as a session variable (say you have an ad campaign leading to this page, and you are tracking where your visitor came from). If you cache the page as is, then the session variable will always be empty (or the value will be the same as the first time the page was requested and put in cache). Now if you pass this variable to another page (or to another site via your external links) this will pose a problem. You can change the GetVaryByCustomString method to return the value of the session variable and that will take care of the issue.
The GetVaryByCustomString method has an input parameter arg passed in which is the same as what you type as the value for “VaryByCustom”. So in the above example if you use :
<% OutputCache Duration=”120” VaryByHeader=”Referrer” %>
The GetVaryByCustomString can be used as below :
public override string GetVaryByCustomString(HttpContext context, string arg)
{
switch (arg.ToLower())
{
case "referrer":
//Get your session value and return it
return "your_session_value";
break;
case "other":
//You can have other pages that use the VaryByCustom attribute
break;
}
}
Extending <OutputCache>
With ASP.NET 4.0 it is interesting that now you can extend the <OutputCache> directive to work off your own custom ways to cache. This way if you want to store your cache on the disc, cloud or database or anywhere else, you can do it.
In order to achieve this, you will need to create a custom cache provider class (inherited from System.Web.Caching.OutputCacheProvider) and override the Add(), Get(), Remove() and Set() methods.
The <outputCache> sub section under the <caching> section in the web.config has a new subelement <providers> and you can add your own output cache provider in there. Of course as with everything in ASP.NET, you can override this default cache provider by adding the providerName attribute to the OutputCache directive for an individual page itself.
Hope this was a good primer on ASP.NET output caching. In the next part, we will see how to do partial page caching