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.aspx. Create this example by adding a copy of OutputCaching.aspx to the C17_Caching website and renaming it OutputCacheLowLevel.aspx.
The content file of OutputCacheLowLevel.aspx 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-8.
Example 17-8. OutputCacheLowLevel.aspx.cs in full
using System;
using System.Web;
using System.Web.UI;
public partial class OutputCachingLowLevel : Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.Now.AddSeconds(10));
Response.Cache.SetCacheability(HttpCacheability.Public);Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access