Page Caching


One of the criticisms of dynamic page creation techniques is that they are less scalable and require more server resources than when sending static HTML files to clients . A solution that many sites have adopted is batch processing the pages and saving the results to disk as static HTML files. However, this can only work if the content is not directly dependent on the client each time “ in other words, the page is the same for all requests . This is the case for things like product catalogs and reports , and the update process only needs to be run when the data that the page is built from changes.

ASP.NET includes a new feature called dynamic output caching that provides the same effect automatically, without the need to write the pages to disk. Instead, it caches the dynamically created output (the content that the client receives), and uses this cached copy for subsequent requests. This is even better than writing the content to a disk file, as it removes the need for a disk access each time. ASP.NET will support this on Windows 2000 and Windows XP platforms.

Of course, this will only be useful where the content of the page is the same for all requests for this page. However, ASP.NET is clever “ the cache can be varied based on a set of parameters, either the query string, the browser type, User Controls (see Partial Page Caching with User Controls, later in this chapter), or even a custom value, and ASP.NET will only use the cached copy if the parameters are the same as well. So, for example, pages that change depending on the contents of the query string will be served correctly “ if the contents of the query string are different from those used when the cached copy was created, a new copy is created instead. This new copy is also cached, and is then available for use by clients that provide matching query string values.

An Output Caching Example

Output caching a page is quite straightforward. All that is necessary is to add the proper @OutputCache directive to the page, and ASP.NET will take care of the rest in caching the page. At its simplest, there are two pieces of information to provide to the @OutputCache directive “ how long the cached item should remain in the cache, and what value should be used to vary the cache. Varying the cache means defining the value supplied by the browser that if it changes, a different page will be cached.

  <%@ Page Language="C#" %>   <%@ OutputCache Duration="10" VaryByParam="None" %>     <Script runat="server">   public void Page_Load(){   // Get the Date and Time, once again this should not change after the   // first run   DateTime NowTime = DateTime.Now;   DateTime Expires = NowTime.AddSeconds(10);   CreatedStamp.InnerHtml = NowTime.ToString("r");   ExpiresStamp.InnerHtml = Expires.ToString("r");   }   </Script>   <Font size="3">   Output caching for 10 seconds...   <HR size="1">   Output Cache created: <Font color="red">   <B id="CreatedStamp" runat="server"></B></Font>   <BR>   Output Cache expires: <Font color="red">   <B id="ExpiresStamp" runat="server"></B></Font>   </Font>  

In this example, let's cache the results of the page for 10 seconds. To do this, set the Duration attribute of the @OutputCache directive to 10. Also, supply a value for the VaryByParam attribute as well. This attribute is required, and it defines what query string parameters to vary the cache by. Since the page does not rely on a query string, it should provide the same page regardless of what parameters are passed to the page. The parameter value of none tells ASP.NET to always provide the same page.

When viewing the page, the time when the page was added to the cache is shown, along with when it will expire, as displayed in Figure 4-7:

click to expand
Figure 4-7:

With the page displayed in the browser, press F5 to cause the page to reload. Notice that each time F5 is pressed the same page will be displayed. And after 10 seconds since the first request has elapsed, a new page will be generated and the times will change.

Caching by Browser

Just as you can vary the cache based on a query string or other parameter, you can also vary the cache based on the type of browser making the request. To do this, add the VaryByCustom attribute to the @OutputCache directive. The value for this parameter can be set to browser . This will tell ASP.NET to vary the cache based on the major version of the browser type. This means that IE 6 will get one version of a cached page, IE 5 will get another, and Netscape 6 will get a third. Thus a Netscape browser will not inadvertently get a page from the cache that has been customized to Internet Explorer.

  <%@ OutputCache Duration="10" VaryByParam="*" VaryByCustom="browser" %>  

Remember that VaryByParam is still a required parameter. If there is a parameter value other than browser, then the application must override the GetVaryByCustomString in the global.asax file.

Important

Output caching only works when using ASP.NET on the Windows 2000 and Windows XP platforms.




Professional ASP. NET 1.1
Professional ASP.NET MVC 1.0 (Wrox Programmer to Programmer)
ISBN: 0470384611
EAN: 2147483647
Year: 2006
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net