· Output Caching
· Fragment Caching
· Data Caching
· Cache Configuration
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.
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.
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.
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.