Pages

Thursday, December 11, 2008

ASP.NET Caching Part 1

There are various kinds of cachings available in ASP.NET. In fact i would say Caching is very cool feature in .net to improve application performance.

Different kinds of caching that you can use in your web applications


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=”30” VaryByParam=”none” %>

The above cache directive will cache the page for 30 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=”15” 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.



Keep watching for more information

No comments: