WEBSITE

Caching Page Content

9/20/2010 5:43:51 PM
As you surf the Web, you see all manner of pages. Some sites update their content very often, whereas others change much less often. Some pages have portions that change while other portions of the page remain static. If you have a page whose content changes infrequently, you can cache the output instead of regenerating it every time a request comes in.

At the outset, turning on output caching is easy. To set up caching, place the OutputCache directive on the page. It's a separate directive, like the Page directive, that enables caching and provides certain control over caching behavior. The following exercise introduces caching output.

Creating a cacheable page

  1. Create a new project. Make it an Empty ASP.NET Web Application named OutputCaching.

  2. Add a new Web Form to the project. Name it Default.aspx. Microsoft Visual Studio opens the file for you after you create it. Insert the OutputCache directive near the top, immediately after the Page directive. For now, set the Trace attribute to false. (You turn it on later when you cache user controls.) At the very least, the OutputCache directive needs two things: (1) the Duration attribute to be set and (2) the VaryByParam attribute set to none. The Duration attribute specifies how long the content should be cached. The VaryByParam attribute is for managing the caching of multiple versions of the page. You learn more about these attributes shortly. The following code shows the syntax of the OutputCache directive. This example caches the page's content for 15 seconds. The code following the output directive was generated by Visual Studio:

    <%@ Page Language="C#" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" Trace="false"%>
    <%@ OutputCache Duration="15" VaryByParam="none" %>

    <!DOCTYPE html PUBLIC
    "...">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
    </body>
    </html>

  3. Update the Page_Load method to print the date and time that this page was generated, like so:

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Response.Write("This page was generated and cached at: " +
    DateTime.Now.ToString());
    }
    }

    The first time the content is produced, the Page_Load method runs and produces the following output:



    No matter how many times you refresh the browser (you can do this by pressing F5 while running Windows Internet Explorer within 15 seconds of first accessing the page), ASP.NET will grab the cached content and display that. When 15 seconds has expired, ASP.NET runs the page in the usual way, calling Page_Load, regenerating the content, and caching it again. The following graphic illustrates the new page accessed just moments (no longer than 15 seconds) after the first hit. The date and time are the same as the previous page, even though it's a completely new request (I promise these are two separate requests):



  4. To get an idea about how caching content might improve performance, add a small amount of code to the Page_Load method to put the executing thread to sleep for perhaps 10 seconds (this is to simulate an expensive content-generating routine). Use the System.Threading namespace to access the threading functions:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Threading;

    namespace OutputCache
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Thread.Sleep(10000);
    Response.Write("This page was generated and cached at: " +
    DateTime.Now.ToString());
    }
    }
    }

  5. Surf to the page. Notice how long the page takes to load (about 10 seconds). Immediately refresh the page. Notice that the browser displays the content right away—without the long wait time. Most pages don't take quite as long to load, but you get the idea of how caching content might improve the performance of your Web application. For pages that are expensive to generate and that don't change very often, caching the content represents an enormous performance boost for your Web site, especially as the number of clients increases.

Other  
 
Most View
Audio Technica ATH-FC707 - No Headline, Too Busy Enjoy Music
Exchange Server 2010 : Working with Security and Standard Distribution Groups (part 1) - Creating Security and Standard Distribution Groups
iPhone SDK 3 Programming : XML and RSS
Droid Razr HD - 4G LTE Android Smartphone
8 Photography Apps You Must Try Out! (Part 1)
Choosing The Right Components (Part 3)
Huawei Ascend P1 LTE - One Of The Cheapest 4G Phones
Ready For The Picking
Top Tablet Apps – November 2012 (Part 2)
Toshiba Satellite L855-148 - An Excellent Comprehensive Windows 8 Laptop
Top 10
Sharepoint 2013 : Farm Management - Disable a Timer Job,Start a Timer Job, Set the Schedule for a Timer Job
Sharepoint 2013 : Farm Management - Display Available Timer Jobs on the Farm, Get a Specific Timer Job, Enable a Timer Job
Sharepoint 2013 : Farm Management - Review Workflow Configuration Settings,Modify Workflow Configuration Settings
Sharepoint 2013 : Farm Management - Review SharePoint Designer Settings, Configure SharePoint Designer Settings
Sharepoint 2013 : Farm Management - Remove a Managed Path, Merge Log Files, End the Current Log File
SQL Server 2012 : Policy Based Management - Evaluating Policies
SQL Server 2012 : Defining Policies (part 3) - Creating Policies
SQL Server 2012 : Defining Policies (part 2) - Conditions
SQL Server 2012 : Defining Policies (part 1) - Management Facets
Microsoft Exchange Server 2010 : Configuring Anti-Spam and Message Filtering Options (part 4) - Preventing Internal Servers from Being Filtered