Search - Articles - Dev Forums - Favorites - Member Login
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009


Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Build the right apps the right way with powerful development tools.
Visual Studio 2010. Learn more...
Caching with ASP.Net (Part 1)

Author: Faraz

In this article you will learn about the caching with ASP.Net 1.x. This is the two part series; in the first part I will discuss the Output Caching.

ASP.Net has taken some dramatic steps forward with caching. Many developers who first learn about caching see it as a bit of frill-but nothing could be further from the truth. Used intelligently, caching could provide a two-fold, three–fold, or even ten-fold performance improvement by retaining important data for just a short period of time.

 

In ASP.Net there are two types of caching:

  • Output caching.
  • Data caching.

In this article I will discuss about the Output caching. Output caching is the simplest type of caching. It stores a copy of the final rendered HTML page that is sent to the client. The next client that submits a request for this page doesn’t actually run the page. Instead, the final HTML output is sent automatically. The time that would have been required to run the page and its code is completely reclaimed.

 

To work with Output cache, insert the Output cache directives at the top of the .aspx file:

 

OutputCache Duration="10" VaryByParam="Version" Location="Client" VaryByCustom="Browser" VaryByHeader="Accept-Language"

Now I discuss the cache attribute one by one. Firstly, we talk about “Duration” attribute. The duration attribute instructs ASP.Net to cache the page for the time period that is mention in its value. In the above example, the page will cache for 10 seconds. This means that the first time you access the page; the page contents will be displayed. If you access the page short time later, however the page will not be updated. Instead, ASP.Net will automatically send the cached HTML output to you, until it expires in 10 seconds. When the cached page expires, ASP.Net will run the page code again, generate a new cached copy, and use that for the next 10 seconds.

For example, add the following code to your ASP.Net web application:

 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here

    lblShowCache.Text = "The time is now:
" & DateTime.Now.ToString()

 

End Sub

The above wills how the system Date and time on your web form. Run the application and see the result.

Now we come on to the “VaryByParam” attribute. This attribute is used to give the arguments to the Query string. For example, create an ASP.Net page that displays the current time and date add the three buttons cmdLarge, cmdNormal and cmdSmall. The single event handler handles the click event for all three buttons. The code for OnClick event is as follows:

 

Sub cmdVersion_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdVersion.Click

 

   Response.Redirect(Request.Path & "?Version=" & _ CType(sender,Control).ID)

 

End Sub

Now add the following code in the Page_load procedure:

 

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

'Put user code to initialize the page here

lblShowCache.Text = "The time is now:
" & DateTime.Now.ToString()

 

   Select Case Request.QueryString("Version")

     Case "cmdLarge"

       lblShowCache.Font.Size = FontUnit.Larger

     Case "cmdNormal"

       lblShowCache.Font.Size = FontUnit.Medium

     Case "cmdSmall"

       lblShowCache.Font.Size = FontUnit.Small

   End Select

 

End Sub

Now what this code does is, when you click on any one of the button from the three. It passes the argument to the “VaryByParam” i.e. the ID of the button control (cmdLarge, cmdNormal, cmdSmall). On the Page_load event the value is passed to the query string and font is set accordingly.

Now we look into the “Location” attribute. This attribute is used to cache the page exclusively on the client side. In this case, the browser stores a copy, and will automatically use this page if the client browsers back to the page or retypes the page URL. However, if the user clicks the Refresh button, the cached copy will be abandoned and the page will be re-requested from the server, which will run the appropriate code once again. The location attribute specifies a value from the System.Web.UI.OutputCacheLocation enumeration.

 

“VaryByCustom” attribute is used for custom caching. It cache the different versions of a page based on the browser type. That way the, Mozilla browser will always receive Mozilla-optimized pages, and the Internet Explorer user will receive the IE-optimized HTML. To set up this sort of logic, use the “VaryByCustom” in Output cache directive as we have done earlier and create a procedure that will generate the custom caching string. Note that the procedure must be coded in the global.asax application file:

Public Overrides Function GetVaryByCustomString(ByVal context As System.Web.HttpContext, ByVal arg As String) As String

 

    If arg = "Browser" Then

      Dim browserName As String

      browserName = context.Request.Browser.Browser

      Return browserName

    End If

 

End Function

The GetVaryByCustomString function passes the VaryByCustom name in the arg parameter. This allows you to create an application that implements several different types of custom caching in the same function. Each different type would use a different VaryByCustom name. your GetVaryByCustomString function would examine VaryByCustom name, and then return the appropriate caching string. If the caching strings for different request matches, ASP.Net will reuse the cached copy of the page. Or to look at it another way, ASP.Net will create and store a separate cached version of the page for each caching string it encounters.

The last attribute is “VaryByHeader”. It allows you to store seprate version of a page based on the value of an HTTP header received with the request. You can specify a single header or a list of headers separated by semicolons. This technique could be used with multi-lingual sites, to cache different versions of a page based on the client browser language.

 

Summary:

 

In this part, I have discussed the five attribute that are involved in using the Output cache directive. I have also discussed some examples that can help you in understanding the directive.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Writing and Reading Data in a DataTable as XML in ASP.NET 2.0

Disclaimer - Privacy
© 2002-2010 DevASP.net