Recipe 16.5. Caching Pages Based on Developer-Defined Custom Strings


Problem

You have pages in your application you want to cache but ASP.NET does not provide built-in support for the dependencies you need, such as browser type and full version number.

Solution

Add the @ OutputCache directive at the top of the .aspx file of each page you want to cache. Set the VaryByCustom attribute to the name of a custom string, such as "BrowserFullVersion":

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"          AutoEventWireup="false"  CodeFile="CH16CacheByCustomStringVB.aspx.vb"  Inherits="ASPNetCookbook.VBExamples.CH16CacheByCustomStringVB"  Title="Cache By Custom String" %>     <%@ OutputCache Duration="10" VaryByParam="none"                        VaryByCustom="BrowserFullVersion" %>

Override the GetVaryByCustomString method in global.asax and write code that builds a unique string for the value you have assigned to the VaryByCustom attribute. Examples 16-1 and 16-2 show VB and C# GetVaryByCustomString method to return a full browser version number.

Discussion

ASP.NET provides the ability to control the caching of pages as a function of custom strings that you provide, which gives you the ability to control the caching by variations not directly supported by ASP.NET. In the example we use to illustrate this solution, we show how to cache pages based on the browser type, its major version number (integer portion of version number), and its minor version number (the decimal portion of the version number).

The first step in this recipe is to add the @ OutputCache directive shown earlier to the .aspx file of the page you plan to cache. Set the VaryByCustom attribute to the name of the string that is to be used to determine caching. In our example, we have named the string "BrowserFullVersion".

Next, you need to override the GetVaryByCustomString method in global.asax to return the full browser version when the passed parameter (arg) is set to "BrowserFullVersion". This provides a unique string for each browser and version to allow ASP.NET to differentiate between the browsers and use the cached version of the page accordingly.

This technique is not limited to caching by browser version. You can use almost any information to identify pages that should be cached separately. For example, you could use a value stored in a cookie to determine the uniqueness of a page. The cookie collection is accessed through the Request object in the same manner as the Browser data (context.Request.Cookies).

One thing you cannot use is Session information. The reason for this is that when GetVaryByCustomString is called, session information has not been retrieved from session storage. The session ID is available but not the session data. If you need to use a value related to a specific session, the data will have to be stored in a cookie first and used as described previously.

Unlike other attributes in the @ OutputCache directive, the VaryByCustom attribute can contain only one value. It cannot be set to a semicolon-delimited string, because the entire unparsed value is passed to the GetVaryByCustomString method. Your code in the GetVaryByCustomString will have to perform the parsing if you want to use multiple values for a single page.


See Also

Search for "Caching Versions of a Page, Based on Custom Strings" in the MSDN library.

Example 16-1. GetVaryByCustomString method in global.asax (.vb)

 '''*********************************************************************** ''' <summary> ''' This routine provides the ability to set custom string values to ''' control the page or page fragment caching based on values assigned ''' to the VaryByCustom attribute of the OutputCache directive. ''' </summary> ''' ''' <param name="context">Set to the current Http context</param> ''' <param name="custom"> ''' Set to the custom string that specifies which cached response is ''' used to respond to the current request ''' </param> ''' ''' <returns> ''' Set to a string containing the full browser verion ''' </returns> Public Overrides Function GetVaryByCustomString( _                             ByVal context As System.Web.HttpContext, _ ByVal custom As String) As String   Dim value As String = Nothing   'if argument is requesting the full browser version, build a string   'containing the browser name, major version, and minor version   If (custom.Equals("BrowserFullVersion")) Then     value = "BrowserFullVersion =" & _              context.Request.Browser.Browser & _             context.Request.Browser.MajorVersion.ToString() & "." & _             context.Request.Browser.MinorVersion.ToString()   End If     Return (value) End Function 'GetVaryByCustomString 

Example 16-2. GetVaryByCustomString method in global.asax (.cs)

 '''*********************************************************************** /// <summary> /// This routine provides the ability to set custom string values to /// control the page or page fragment caching based on values assigned /// to the VaryByCustom attribute of the OutputCache directive. /// </summary> /// /// <param name="context">Set to the current Http context</param> /// <param name="custom"> /// Set to the custom string that specifies which cached response is /// used to respond to the current request /// </param> /// /// <returns> /// Set to a string containing the full browser verion /// </returns> public override string GetVaryByCustomString(System.Web.HttpContext context,                                              string custom)  {    String value = null;   // if argument is requesting the full browser version, build a string   // containing the browser name, major version, and minor version   if custom.Equals("BrowserFullVersion")   {     value = "BrowserFullVersion =" + context.Request.Browser.Browser + context.Request.Browser.MajorVersion.ToString() + "." + context.Request.Browser.MinorVersion.ToString();   }   return (value);  } // GetVaryByCustomString 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

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