October 2017 : Online course in ASP.NET MVC / Core. Conducted by Bipin Joshi. Read more...
Registration for October 2017 batch of ASP.NET MVC / Core online course has already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

<%@ Page %>

Caching Techniques In ASP.NET - Part I


In order to build scalable web sites it is important to optimize the way your web pages are served to the user. Consider a web site that provides a list of certain products to the user. The products are not going to change frequently. In traditional ASP such product catalog page will be processed each time a new request comes. Event though there is nothing dynamic in the page it will be processed every time. This is clearly not efficient. This is where ASP.NET caching features come handy. Caching allows you to store a particular version of ASP.NET page on the server. The requests to the same page are served with this cached version of page rather than processing the page every time. This is called as Output Caching. There is another flavor that caches data of your applications and called as Data Caching. In the part- I of this article we will examine output caching in detail.

The @OutputCache directive

The OutputCache directive is used to enable caching for a ASP.NET page or even user control. This directive tells ASP.NET the duration in seconds for which the page is to be cached. Following markup shows the usage of the directive:
<%@ OutputCache duration="secs" 
varybucustom="param" %>
Here, the duration attribute specifies the time in seconds for which the page will be cached. The VaryByXXXX attributes tells how ASP.NET should cache multiple versions of the page. We will learn more about this in later sections.

Example 1 - Simple OutputCaching

In this example we will see the simplest form of caching syntax. Here, we are caching page output for 30 seconds. Also, we are caching only one version of the page.
<%@ OutputCache duration="30" varybyparam="none" %>

Example 2 - Caching multiple versions based on form field

Consider an example where want to cache output of a book catalog for different values of categories. Here, you will provide a textbox or dropdownlist to select category. Depending on the category the books are fixed and you will cache that version. This can be achieved by using the VaryByParam attribute.
<%@ OutputCache duration="30" VaryByParam="TextBox1" %>
Note that the form can be reqeusted using POST or GET method.

Example 3 - Caching multiple versions based on browser

You may want to display different versions of ASP.NET pages to different browsers and then cache those versions. This is possible via VaryByCustom attribute as shown below:
<%@ OutputCache Duration="30" VaryByParam="none" 
VaryByCustom="browser" %>

Example 4 - Caching multiple versions based on custom string

In above example we saw how to cache multiple versions based on browser type. You can also cache multiple versions for some custom values using the same attribute. However, you need to override HttpApplication.GetVaryByCustomString method in your Global.asax file as shown below.
Public Overrides Function 
(context As HttpContext, arg As String) 
As String
   select case arg
   case "string1"
   	return "value1"
   case "string2"
   	return "value2"
   case else
	return "defaultvalue"
   end select
End Function
Here, you can also process some logic and decide the return value. Then you can use VaryByCustom attribute as follows:
<%@ OutputCache Duration="30" 
VaryByCustom="string1" %>

Example 5- Caching user controls

Consider a case where you want to display product catalog at the top of the page and current date and time below it. Now, it will be improper to cache the whole page as date and time will be incorrect. In such cases you can put product catalog in a web user control and time display logic will be in the main form. The Outputcache directive will be applied to the user control and not to the web form. You will then host the web user control in the web form. Here, only the output of the user control will be cached.
<%@ Control Language="vb"%>
<%@ OutputCache duration="30" varybyparam="none"%>
The same can be accomplished by using following class level declarative attribute in the user control:
<PartialCaching(30, "none", Nothing, Nothing)>
Public MustInherit Class WebUserControl1
    Inherits System.Web.UI.UserControl


Output caching can be used to build scalable web applications. It is ideal for web pages that do not change too often. We also saw how to use use these techniques for web forms as well as user controls. In Part - II we will see what Data Caching is and how to use it in your applications.

Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 28 September 2002

Tags : ASP.NET Web Forms Performance