Section 17.5. The HttpCachePolicy Class

17.5. The HttpCachePolicy Class

Just as the OutputCache 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 next example, OutputCacheLowLevel . Create this example by copying the OutputCaching example from early in this chapter, as shown in Figure 17-2.

The content file of OutputCacheLowLevel is functionally unchanged from the previous example, except for removing the OutputCache directive from the top of the file. If you leave the OutputCache directive in, it will override the calls using Response.Cache .

The code-behind file has two additional lines added to the Page_Load method, highlighted in Example 17-26.

Example 17-26. default.aspx.cs for OutputCacheLowLevel
 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page {     protected void Page_Load(object sender, EventArgs e)     {  Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));        Response.Cache.SetCacheability(HttpCacheability.Public);  lblTime.Text = "This page was loaded at " +            DateTime.Now.ToLongTimeString(  );        lblUserName.Text = Request.Params["username"];        lblState.Text = Request.Params["state"];     } } 

The first highlighted line in Example 17-26 sets the cache duration to 10 seconds. It is equivalent to a Duration parameter in an OutputCache directive.

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

Table 17-6. 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

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 the following:



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 , it will enable sliding expiration. Sliding expiration forces the clock to restart, 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 will be 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 



Programming ASP. NET
Programming ASP.NET 3.5
ISBN: 0596529562
EAN: 2147483647
Year: 2003
Pages: 173

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