JavaScript Editor Javascript validator     Javascripts

Main Page


Previous Section Next Section

18.4 The HttpCachePolicy Class

Just as the OutputCache page directive provides a high-level API for implementing caching, a low-level API is available through the HttpCachePolicy class. This class is contained within the System.Web namespace. It uses HTTP headers to control the caching. The HttpCachePolicy class mirrors the functionality provided by the page directive. It also provides additional low-level control, comparable to the type of control provided for object caching.

To use the HttpCachePolicy class to control output caching, do not include an OutputCache directive in the page file. Instead, use the Response.Cache syntax, as shown in the highlighted lines in Example 18-24 (for VB.NET) or Example 18-25 (for C#). (Example 18-25 includes only the script block, since the HTML is identical to that in Example 18-24. Note that these examples are similar to Example 18-3 and Example 18-4.)

Example 18-24. Output caching using HttpCachePolicy Class in VB.NET,vbOutputCache-03.aspx
<%@ Page Language="VB" %>

<script runat="server">
   sub Page_Load(ByVal Sender as Object, _
               ByVal e as EventArgs)
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(10))
      Response.Cache.SetCacheability(HttpCacheability.Public)

      lblMsg.Text = "This page was loaded at " & _
                    DateTime.Now.ToString("T")
      lblUserName.Text = Request.Params("username")
      lblState.Text = Request.Params("state")
   end sub
</script>

<html>
   <body>
   <form runat="server">

      <h1>Output Caching</h1>

      <asp:Label
         id="lblMsg"
         runat="server"/>

      <br/>
      <br/>

      UserName:&nbsp;&nbsp;&nbsp;
      <asp:Label
         id="lblUserName"
         runat="server"/>

      <br/>

      State:&nbsp;&nbsp;&nbsp;
      <asp:Label
         id="lblState"
         runat="server"/>

   </form>
   </body>
</html>
Example 18-25. Output Caching Using HttpCachePolicy Class in C#, csOutputCache-03.aspx
<%@ Page Language="C#" %>

<script runat="server">
   void Page_Load(Object Source, EventArgs E)
   {
      Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
      Response.Cache.SetCacheability(HttpCacheability.Public);

      lblMsg.Text = "This page was loaded at " + 
                     DateTime.Now.ToString("T");
      lblUserName.Text = Request.Params["username"];
      lblState.Text = Request.Params["state"];

   }
</script>

The first highlighted line in Example 18-21 and Example 18-22 sets the cache duration to 10 seconds. It is equivalent to a Duration parameter in an OutputCache page directive.

The second line corresponds to the Location parameter in the OutputCache directive. Table 18-4 compares the SetCacheability values, which are members of the HttpCacheability enumeration, with the Location values.

Table 18-4. SetCacheability versus Location

Location value

SetCacheability values

SetCacheability description

Client

Private

Default value. Response is cacheable on the client. Useful if page requires authentication.

Downstream

Public

Also uses SetNoServerCaching method to disallow caching on the web server.

Server

Server

Response is cached on the web server.

None

NoCache

Disables caching.

Any

Public

Response is cacheable by clients and shared (proxy) caches.

There are many other HttpCachePolicy methods and properties available. Some of the more common ones include:

SetMaxAge

Another method, in addition to SetExpires, to set an expiration. Accepts a TimeSpan value. The following line of code would set the expiration time to 45 seconds:

Response.Cache.SetMaxAge(new TimeSpan(0,0,45))
SetNoServerCaching

Disables all further server caching. For example:

Response.Cache.SetNoServerCaching(  )
SetSlidingExpiration

A method to enable sliding expiration. Takes a Boolean parameter. If true, enables sliding expiration. Sliding expiration forces the clock to start over, so to speak, every time the cache is accessed. So, if SetMaxAge (described above) is set to 30 seconds, every time the cache is accessed, the 30-second clock is reset to zero. As long as the cache is accessed at least every 30 seconds, it will never expire. The following statement, for example, enables sliding expiration of the cache:

Response.Cache.SetSlidingExpiration(true)
VaryByParams

This property is the equivalent of the VaryByParam parameter in the OutputCache directive (note the slight difference in spelling). It forces a separate cache for each unique combination of parameters passed to the server in the page request.

To duplicate the VaryByParam parameter in the following OutputCache directive:

<%@ OutputCache Duration="60" VaryByParam="state;city" %>

you would use the following lines of code:

Response.Cache.VaryByParams.Item("state")=true
Response.Cache.VaryByParams.Item("city")=true
    Previous Section Next Section


    JavaScript Editor Javascript validator     Javascripts 
    http://www.cashadvance-loans.net/florida/cash-advance-pembroke-pines-fl/



    ©