ASP.NET 2.0 : New Caching Techniques - Part II

In the previous article, we saw the SQL Cache Notification and the benefits it provides for invalidating the Cache.

There are other methods which are very useful for enabling / validating cache. We will examine some of them as follows:-

Disk Output Cache
Though Output Cache serves the purpose of avoiding repeated requests to the Server, it cannot withstand Application restarts. Also, Cache is memory intensive and when memory reaches the isolation level, the Cache expires.

With disk output cache, ASP.NET 2.0 can save cached responses to disk in addition to keeping them in memory. When the cached response is removed from the output cache due to memory pressure, it still remains on disk allowing a much larger set of pages to be cached. In addition, disk cached pages survive application restarts, enabling the cache to remain warm for when the application comes back up and begins serving requests. The syntax for Disk Cache enabling in output directive is as follows:-

<%@ OutputCache Duration="3600" VaryByParam="none" DiskCacheable="true" %>

In addition, the disk cache is turned on in configuration and a per-application disk usage limit must be specified. It is important to always set application quota for disk cache as it can easily fill up the disk otherwise.


<?xml version="1.0"?>
<configuration>
<system.web>
<caching>
<outputCache>
<diskCache enabled="true" maxSizePerApp="2" />
</outputCache>
</caching>
</system.web>
</configuration>


In the above settings, the per-application disk usage limit is set to 2 megabytes, as specified in the maxSizePerApp attribute

Disk caching is turned on by default for all pages. To turn disk caching off, specify DiskCacheable=false in the <%@ OutputCache %>directive on the page.

Post Cache Substitution
In .NET 1.X versions if you want a dynamic section inside an Output Cached Page you have to either use Fragment Caching and split the page into Usercontrols and have the dynamic section as an Usercontrol without Caching. This is not always easy since you have break up the whole page into controls for achieving a simple functionality. You felt that right?

Now, you dont have to do that!!!

You have the Post Cache Substitution where you can delegate a call back to the Method which you specify using the Response.WriteSubstitution Method to make the particular section dynamic.

Lets say we have the following method which returns the Current date & time on a page.

static string GetCurrentDate(HttpContext context)
{
return DateTime.Now.ToString();
}


Then, in the ASPX Page's Output Directive, we will specify the Output Cache as follows:-

<%@ OutputCache Duration="60" VaryByParam = "none" %>

Now this ensures that the page will be cached for 60 sections.

Next, we will have the following Code in the Page:-


Time: <%= DateTime.Now.ToString() %>

Exact Time: <% Response.WriteSubstitution(new HttpResponseSubstitutionCallback(GetCurrentDate)); %>


The above two statements intially will display the same Time when you run the page for the first time.

However, when you make subsequent refreshes, you will see that, next to Time: you will find the original time and no chance until 60 seconds.

However, next to Exact Time: you will find that the exact time is displayed which implies that the section is not being Cached, though the other sections of the page are cached.

As you can see from the above code, the Response.WriteSubstitution callback executes on every request, and the result gets inserted into the cached response chain that is served from output cache.

This is a very useful mechanism, particularly if you have portions of your section to be maintained very dynamic.

You can achieve a similar functionality by using the substitution control, as explained in my
Earlier Article.

These are some of the new methods available in Whidbey to leverage the benefits of Dynamic Caching of your web pages.

Cheers and Happy Caching !!!

Print | posted on Friday, June 17, 2005 12:46 PM

Comments on this post

# re: Whidbey: New Caching Techniques - Part II

Requesting Gravatar...
verry good explination on how the diskCache property is used.
Left by Joe on Feb 27, 2007 8:53 AM

# re: ASP.NET 2.0 : New Caching Techniques - Part II

Requesting Gravatar...
Hey, that was interesting,

a gret method for catch

Thanks for bringing this up
Left by web development company on Aug 18, 2009 12:54 PM

# re: ASP.NET 2.0 : New Caching Techniques - Part II

Requesting Gravatar...
a gret method for catch
Left by mario oyunları on Sep 22, 2009 12:10 AM

# re: ASP.NET 2.0 : New Caching Techniques - Part II

Requesting Gravatar...
NCache Express - a free distributed caching solution for .NET is probably the easiest and most efficient way to cache your objects and ASP.NET Session. When distributing data across multiple cache servers, you get scalable performance even at peak load times.

Actually there are many problems with ASP.NET's own caching. Read here

http://24x7aspnet.blogspot.com/2009/06/aspnet-cache-limitations.html

To avoid this, software like above is the best option
Left by steve on Mar 17, 2010 11:32 PM

# re: ASP.NET 2.0 : New Caching Techniques - Part II

Requesting Gravatar...
This feature was pulled after the beta and never released. Maybe best to add that as a disclaimer, or just remove this article.
Left by Tom on Oct 26, 2011 2:23 AM

Your comment:

 (will show your gravatar)