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
Create a new project. Make it an Empty ASP.NET Web Application named OutputCaching.
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>
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):
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());
}
}
}
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.