Tuesday, September 27, 2011

What is Page Output Caching with Example?

Posted by Rahul Kharde at 3:36 AM
In this article, we will take a look at the Page Output Cache directive, which is by far the easiest way of caching content with ASP.NET

What is Cache?
A cache is a type of dynamic and high speed memory that is used to supplement the function of the central processing unit and the physical disk storage. The cache acts as a buffer when the CPU tries to access data from the disk so the data traveling from the CPU and physical disks can have synchronized speed. Disk reading and writing process is generally slower than CPU function.
ASP.NET introduces another key/value pair object Cache. In addition to simply storing key/value pairs, Cache adds additional functionality specifically designed to store transient data:
  • Dependencies: A key added to the Cache can set up dependency relationships that can force that key to be removed from the Cache. The supported dependencies include key, file, and time.
  • Automatic Expiration: Underused items added to the Cache that have no dependencies will automatically expire if they are underused.
  • Support for Callback: Code paths can be added that will be called when an item is removed from the Cache, giving us an opportunity to update the Cache or not remove the item.
Type of Cache
ASP.NET supports three types of caching for Web-based applications:
  • Page Output Cache
  • Page Fragment Cache
  • Data Caching
Page Output Caching

Output Caching is particularly useful when you have very static pages. It is nothing but the HTML output of dynamic requests to ASP.NET Web pages. Each time an incoming ASP.NET page request comes in this engine checks to see if the page being requested has a cached output entry. If it does, this cached HTML is sent as a response; otherwise, the page is dynamically rendered, it's output is stored in the Output Cache engine.

Configure Output Cache for a Single Page in Declarative Way you can control the output cache’s behavior in a declarative way by using the @Output Cache page directive. The only required parameter for the output cache is the Duration all the other parameters can be used if you want to. Let’s describe some of the parameters

Duration: Required. Time, in seconds, the page should be cached. Must be a positive intege

Location: Specifies where the output should be cached. If specified, must be one of: Any, Client, Downstream, None, Server or Server and Client.

VaryByParam: Required. The names of the variables in the Request, which should result in, separate cache entries. "none" can be used to specify no variation. "*" can be used to create new cache entries for every different set of variables. Separate variables with ";".

VaryByHeader: Varies cache entries based on variations in a specified header.

VaryByCustom: Allows custom variations to be specified in the global.asax (for example, "Browser").

Here is a very simple example of a page which will show us the difference between a cached page and a non-cached page. Try creating a new project, and change the OutputCache.aspx page to contain the following code
<%@ OutputCache Duration="100" VaryByParam="None" %>
<%@ OutputCache Duration="5" VaryByParam="None" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Output Cache</title>
<form id="form1" runat="server">
        <asp:Label ID="Label5" runat="server" />

Code Behind
protected void Page_Load(object sender, EventArgs e)
    Label1.Text = DateTime.Now.ToString();
This is all standard stuff, except the line with the DateTime. It simply outputs the current date and time to the page. Try running the project and reload the page a couple of times. As you will see, the time is only refreshed every 5 seconds.


If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!

Kindly Bookmark and Share it:



Have any question? Feel Free To Post Below:


Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top