Skip to main content

What is Page Output Caching with Example?

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").

Syntax
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" %>
HTML
<%@ OutputCache Duration="5" VaryByParam="None" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Output Cache</title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <asp:Label ID="Label5" runat="server" />
    </div>
</form>
</body>
</html>

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.



Download

Comments

Popular posts from this blog

Connected and disconnected architecture in ADO.Net with Example

Connected Architecture of ADO.NET The architecture of ADO.net, in which connection must be opened to access the data retrieved from database is called as connected architecture. Connected architecture was built on the classes connection, command, datareader and transaction.  Connected architecture is when you constantly make trips to the database for any CRUD (Create, Read, Update and Delete) operation you wish to do. This creates more traffic to the database but is normally much faster as you should be doing smaller transactions. Disconnected Architecture in ADO.NET The architecture of ADO.net in which data retrieved from database can be accessed even when connection to database was closed is called as disconnected architecture. Disconnected architecture of ADO.net was built on classes connection, dataadapter, commandbuilder and dataset and dataview. Disconnected architecture is a method of retrieving a recor

HTTPHandler and HTTPModule in ASP.NET

If you want to implement pre-processing logic before a request hits the IIS resources. For instance you would like to apply security mechanism, URL rewriting, filter something in the request, etc. ASP.NET has provided two types of interception HttpModule and HttpHandler .   The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension.  For Example When an .aspx page is requested it is passed to ASP.Net page handler. Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse. HTTPModule: -    It's just like a filter. The Modules are called before and after the handler executes . -    HTTP Modules are objects which also participate the pipeline but they work before and after the HTTP Handler does its job, and produce additional services within the pipeline -  

ASP.NET Page Life Cycle with example

In this article, we are going to discuss the different methods and order they are executed during the load of an .aspx web page. Methods Description Page_PreInit Before page Initialization Page_Init Page Initialization LoadViewState View State Loading LoadPostData Postback Data Processing Page_Load Page Loading RaisePostDataChangedEvent PostBack Change Notification RaisePostBackEvent PostBack Event Handling Page_PreRender Page Pre Rendering Phase SaveViewState View State Saving Page_Render Page Rendering Page_Unload Page Unloading PreInit : The entry point of the page life cycle is the pre-initialization phase called “PreInit”. You can dynamically set the values of master pages and themes in this event. You can also dynamically create controls in this event.  Init : This event fires after each control h