Customize Output Caching Logic

Use cache validation callbacks to implement a sliding expiration on cached pages.

Hot Tip

LANGUAGE: C#

ASP.NET VERSIONS: 1.0 | 1.1

 

Customize Output Caching Logic

Use cache validation callbacks to implement a sliding expiration on cached pages.

 

By Jeff Prosise

 

It's well known that ASP.NET developers can use @ OutputCache directives to declaratively add pages to ASP.NET's output cache. The following page uses an @ OutputCache directive to cache itself for 5 seconds at a time. Try it: Load the page in your browser and click Refresh several times. As proof that the page is output-cached, the time displayed on the page only updates every 5 seconds:

 

<%@ OutputCache Duration="5" VaryByParam="None" %>

<html>

  <body>

    <h1><asp:Label ID="Output" RunAt="server" /></h1>

  </body>

</html>

<script language="C#" runat="server">

void Page_Load (Object sender, EventArgs e)

{

    Output.Text = DateTime.Now.ToLongTimeString ();

}

</script>

 

Less widely known is the fact that you can use validation callback handlers to customize the logic for evicting pages from the output cache. The following page uses a validation callback handler to implement a sliding expiration. The page remains in the cache indefinitely as long as it's accessed at least once every 5 seconds, but if more than 5 seconds elapse between requests, the page is evicted from the cache and executed anew:

 

<html>

  <body>

    <h1><asp:Label ID="Output" RunAt="server" /></h1>

  </body>

</html>

<script language="C#" runat="server">

static DateTime _lastRequested;

void Page_Load (Object sender, EventArgs e)

{

    Output.Text = DateTime.Now.ToLongTimeString ();

    Response.Cache.SetCacheability

         (HttpCacheability.Public);

    Response.Cache.AddValidationCallback

         (new HttpCacheValidateHandler (OnValidateCache),

        null);

    _lastRequested = DateTime.Now;

}

void OnValidateCache (HttpContext context, Object data,

    ref HttpValidationStatus status)

{

    int delta = (DateTime.Now - _lastRequested).Seconds;

    status = (delta < 5) ? HttpValidationStatus.Valid :

        HttpValidationStatus.Invalid;

    _lastRequested = DateTime.Now;

}

</script>

 

The call to HttpCachePolicy.AddValidationCallback in Page_Load registers OnValidateCache as a cache validation handler. OnValidateCache, which is called each time the page is requested, compares the current time to the time of the most recent request. If the elapsed time is less than 5 seconds, OnValidateCache sets status to HttpValidationStatus.Valid, indicating the page can be delivered from the cache. If the elapsed time is 5 seconds or greater, OnValidateCache sets status to HttpValidationStatus.Invalid, prompting ASP.NET to evict the page from the cache and execute it as normal.

 

Cache validation callbacks can be used to customize the behavior of ASP.NET's output cache in a variety of ways. A cache validation handler could, for example, decide whether to return HttpValidationStatus.Valid or HttpValidationStatus.Invalid based on the current state of a file, database, or other data source from which the page obtains its content. That's a very effective way to keep a page in the cache until the data source on which it relies changes.

 

Jeff Prosise is author of several books, including Programming Microsoft .NET (Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET. Contact Jeff at [email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish