Using the HttpCachePolicy Class to Enable Output Caching

HttpCachePolicy Class to Enable Output Caching"-->

only for RuBoard

Using the HttpCachePolicy Class to Enable Output Caching

As previously mentioned, the @OutputCache directive is derived from the HttpCachePolicy class, which gives you lower-level access to the output cache. The HttpCachePolicy class is exposed through the HttpResponse.Cache property; and it is a member of the System.Web namespace. You would want to use the HttpCachePolicy rather than the @OutputCache when you need to dynamically set the output cache settings. For instance, if you want to dynamically set how long the item should remain in the cache.

The @OutputCache directive exposes nearly all the functionality available from using the HttpCachePolicy class, but the class does offer more functionality than @OutputCache . For instance, when using the @OutputCache directive, you can only set the cache expiration time in seconds. The HttpCachePolicy class enables you to set either an absolute ” DateTime.Parse ("11:59:00PM") or a relative expiration time ” DateTime.Now.AddMinutes(2) .

Because I went over nearly all the attributes for the HttpCachePolicy class in the @OutputCache directive section, I will not go into great detail about each here. However, I will provide a code example of implementing caching using the HttpCachePolicy class in Listing 16.10.

Listing 16.10 Implementing Output Caching Using the HttpCachePolicy Class
 [VisualBasic.NET]  1: <script language="vb" runat="server" >  2:  3:  public sub Page_Load(sender as Object, e as EventArgs)  4:  5:   Response.Cache.SetExpires(DateTime.Now.AddMinutes(2))  6:   Response.Cache.SetCacheability(HttpCacheability.Public)  7:  8:  end sub  9: 10: </script> [C#.NET] 01: <script language="c#" runat="server" > 02: 03:  void Page_Load(Object sender, EventArgs e) { 04: 05:   Response.Cache.SetExpires(DateTime.Now.AddMinutes(2)); 06:   Response.Cache.SetCacheability(HttpCacheability.Public); 07: 08:  }  9: 10: </script> [VisualBasic.NET & C#.NET] 11: <html> 12: <body> 13: <b>This page was cached at</b>: <%=DateTime.Now.ToString("G")%> 14: </body> 15: </html> 

In Listing 16.10, caching is enabled on lines 5 and 6. On line 5, the HttpCachePolicy.Set Expires method sets the cache duration ( SetExpires will be discussed in the following section). On line 6, the SetCacheability method sets the cache to public visibility, passing in a member of the HttpCacheability enumeration. (These objects are covered in the following section.)

The following list details members of the HttpCachePolicy class. I will not give a full code example for each member in this section because I went over the effect of using these members in the @OutputCache directive section, just under a different context.

  • SetExpires ” Used to set an absolute date and time that the cache shall expire. In Listing 16.8 I used DateTime.Now.AddMinutes(2) as the expiration time, which takes the current time and adds 2 minutes. Alternatively, you can put in a hard-coded value such as "12:00:00AM".ToDateTime() . This is comparable to the @OutputCache 's Duration attribute, but takes an absolute date and time rather than a value of seconds.

  • SetCacheability ” Used to control where documents are cached, whether only on the server or on both clients and the server. This is similar to the @OuputCache's Location attribute. The value passed into this method must be a member of the HttpCachePolicy enumeration (a member of System.Web ). A list and description of each value follows :

    • HttpCachePolicy.Server Documents are only cached on the server that processed the request

    • HttpCachePolicy.Public Documents can be cached on by the client, downstream server, or the original server that processed the request.

    • HttpCachePolicy.Private Documents can only be cached on the client.

    • HttpCachePolicy.NoCache Prevents caching from taking place.

  • SetMaxAge ” Used to set the maximum time span the cache item should be considered valid. The parameter for this method should be a TimeSpan type, such as

     Response.Cache.SetMaxAge(TimeSpan) 
  • SetNoServerCaching ” Used to restrict any caching from taking place on the server that processed the request. This method expects no parameters:

     Response.Cache.SetNoServerCaching() 
  • SetSlidingExpiration ” Used to turn sliding expiration on or off. This method expects a Boolean value as a parameter:

     Response.Cache.SetSlidingExpiration(true  false) 
  • SetVaryByCustom ” Used to set the VaryByCustom string. This method expects a string as a parameter (for example, Platform):

     Response.Cache.SetVaryByCustom(string) 
  • HttpCacheVaryByHeaders ” Class used to control the cache to vary by a Header(s).

  • HttpCacheVaryByParams ” Class used to control the cache to vary by a parameter(s).

only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

Similar book on Amazon

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