17 August, 2011

Programmatically Invalidating Cached Pages in Asp.net

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.

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 MycacheValidate (Manualy created event) event and set a valid value
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 ValidatePage
event.
.aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ OutputCache Duration="30" VaryByParam="mode" %>


    Validating Cache


    
Cache Time: Real Time:
.cs file
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page 
{
    // load event will run every 30 seconds , because it will be cached for 30 seconds.
    protected void Page_Load(object sender, EventArgs e)
    {

        lblTime.Text = DateTime.Now.ToString();
        // this will add callback method. so method defined here will be called every time before cache data is loded.
        // so in this method, we can decide whether to load previously cache page(VALID) or remove cache and load page again (INVALIDATING cache).
        Response.Cache.AddValidationCallback(new HttpCacheValidateHandler(MycacheValidate), null);

    }

    public static string GetTime(HttpContext context)
    {

        return DateTime.Now.ToString();
        
    }

    // this method will be run every time before caching. to check cache status whether to cache page or not ??
    public static void MycacheValidate(HttpContext context,Object data, ref HttpValidationStatus vStatus)
    {
        //you can use quary string here, and decide cache behaviour

        vStatus = HttpValidationStatus.Invalid; // remove cache. load whole page
        //vStatus = HttpValidationStatus.Valid;   // dont invalidate. cache it ok.
        //vStatus = HttpValidationStatus.IgnoreThisRequest;  // not cache in currunt request.ignore it. will cache next requests.

    }
}

Notice that this code sample uses logic to specify one of the HttpValidationStatus values to
control how the page is cached:

 HttpValidationStatus.Invalid This 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.Ignore ThisRequest This 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 This causes ASP.NET to return the cached page.

No comments:

Post a Comment