Programmatically Invalidating Cached Pages
Often, you want to cache pages, but specific events might require you to stop using the cached page. For example, a page that displays results from a database query should only be cached until the results of the database query change. Similarly, a page that processes a file should be cached until the file is changed. Fortunately, ASP.NET gives you several ways to invalidate cached pages.
The sections that follow describe how to make caching choices before returning a page and how to create a cache page output file dependency.
Determining Whether to Return a Cached Page Prior to Rendering To directly control whether a cached version of a page is used or whether the page is dynamically regenerated, respond to the ValidateCacheOutput event and set a valid for the HttpValidationStatus attribute. Then, from the Page.Load event handler, call the AddValidationCallback method and pass an HttpCacheValidateHandler object with your method.
The following example demonstrates how to create a method to handle the Validate-Page event:
Public Shared Sub Va1idatePage(ByVa1 context As HttpContext, _
ByVal data As [Object], ByRef status As HttpValidationStatus) If Not (context.Request.QueryString("Status") Is Nothing) Then
Dim pageStatus As String = context.Request.QueryString("Status")
If pageStatus = "invalid" Then status = HttpValidationStatus.Invalid ElseIf pageStatus = "ignore" Then status = HttpValidationStatus.IgnoreThisRequest
Else status = HttpValidationStatus.Valid End If
Else status = HttpValidationStatus.Valid End If End Sub
public static void Va1idateCacheOutput(HttpContext context, Object data, ref HttpValidationStatus status)
if (context.Request.QueryString["Status"] != null) {
string pageStatus = context.Request.QueryString["Status"];
if (pageStatus == "invalid")
status = HttpValidationStatus.Invalid; else if (pageStatus == "ignore")
status = HttpValidationStatus.IgnoreThisRequest;
else status = HttpValidationStatus.Valid;
else status = HttpValidationStatus.Valid;
Notice that this code sample uses logic to specify one of the HttpValidationStatus values to control how the page is cached:
■ HttpValidationStatus.Invalid Causes the cache to be invalidated so that the page is dynamically generated. The newly generated page is stored in the cache, replacing the earlier cached version.
■ HttpValidationStatus.IgnoreThisRequest Causes the current page request to be dynamically generated without invalidating the previously cached version of the page. The dynamically generated page output is not cached, and future requests might receive the previously cached output.
■ HttpValidationStatus.Valid Causes ASP.NET to return the cached page.
The following sample demonstrates how to configure that event handler so that it is called when ASP.NET determines whether to use the cached version of the page:
Protected Sub Page_Load(ByVa1 sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load
Response.Cache.AddVa1idationCa11back( _
New HttpCacheValidateHand1er(AddressOf ValidatePage), Nothing)
End Sub
protected void Page_Load(object sender, EventArgs e) {
Response.Cache.AddVa1idationCa11back(
new HttpCacheValidateHand1er(Va1i dateCacheOutput), null);
ASP.NET calls the method you specified when it determines whether to use the cached version of the page or a new, dynamically generated version of the page.
Creating a Cache Page Output Dependency To create a cache page output dependency, call one of the following Response methods:
■ ResponseAddCacheDependency Makes the validity of a cached response dependent on a CacheDependency object.
■ Response.AddCacheItemDependency and Response.AddCacheItemDependencies
Makes the validity of a cached response dependent on one or more other items in the cache.
■ Response.AddFileDependency and Response.AddFileDependencies Makes the validity of a cached response dependent on one or more files.
Post a comment