Caching in ASP.NET 2.0

Here is the different kind of caching that you can use in your web applications:

· Output Caching

· Fragment Caching

· Data Caching

· Cache Configuration

Output Caching

Performance is improved in an ASP.NET application by caching the rendered markup and serving the cached version of the markup until the cache expires. For example, if you have a page that displays user information from a database, caching will help improve performance by serving the page from memory instead of making a connection to the database on each page request.

You can cache a page by using the OutputCache API or simply by using the @OutputCache directive.

<%@ Page Language=”C#” %>
<%@ OutputCache Duration=”15” VaryByParam=”none” %>

The above cache directive will cache the page for 15 minutes.

So output caching is great for when you want to cache an entire page.

Fragment Caching

In many situations caching the entire page just isn’t going to cut it, for some reason or another you require specific sections of the page to display live information. One way to improve performance is to analyze your page and identify objects that require a substantial overhead to run. You can build a list of these objects that are expensive to run, and then cache them for a period of time using fragment caching.

For example, say your page default.aspx consists of three user controls. After looking over the code, you identified that you can cache one of them. You can simply add the caching directive to the top of the user control:

<%@ Control %>
<%@ OutputCache Duration=”5” VaryByParam=”none” %>

Now keep in mind that the actual page that contains the control is not cached, only the user control. This means that the default.aspx page will be rendered each and every page request, but the user control is only ‘run’ every 15 minutes.

Data Caching

So we know that we can cache an entire page, or a fragment of a page by caching down to the user control level. Wouldn’t it be great if we could cache down to the object level? The good news is you can with ASP.NET Data caching.

The cache consists of a dictionary collection that is private to each application in memory. To insert items in the cache simply provide the collection with a unique name:

Cache[“someKey”] = myObject;

Retrieving the object form the cache:

myObject = (MyObject)Cache[“someKey”];

It is a good time to point out that you should always remember to check for null, and be sure to caste to your datatype.

Cache Configuration

If you are familiar with how caching worked in ASP.NET 1.0, you realize that managing all the cache directions for all your pages could potentially get out of hand. ASP.NET 2.0 introduces Cache Profiles that helps you centrally manage your cache. Cache settings can be inherited by your pages, and overridden if required by using the OutputCache directive.

The page directive looks pretty much the same, expect this time it references a cache profile that you defined in your web.config file.

So each and every page that references the ‘myCacheProfile1’ can be centrally managed in the web.config, this means that any changes to the cache settings in the web.config file will be automatically changed on all your referenced pages.

...S.VinothkumaR.

3 comments:

PelaLusa said...

Might you know how to accept the ASP.Net 2.0 cache objects from Javascript? All I want to do is clear them upon the page unloading.

Robert W.
Vancouver, BC

Deepak said...

Caching is a server-side concept. So, I believe you cannot access those objects through javascript. But what you could however do is expose a web method that clears the cache and invoke that web method through javascript (read ASP.NET AJAX)

Ed said...

Good work Vinoth! ASP.NET is my favorite subject. However, its performance is very good in a single server environment but as we try to scale to a server farm it does not work so well. Distributed caching is very hot issue nowadays. Even Microsoft has realized it and introduced its distributed caching solution. They have also said in one of their presentations that distributed caching is the future of high performance computing. I recommend that developers who wish to create robust applications take a look into it.