Thursday, January 15, 2009

Caching in ASP.NET

Caching is mechanism of keeping content in memory for future use. It helps the applications operate with high performance and scalability. Web applications operate with higher performance by reducing the amount of work needed to obtain information from its data source, such as a database,Web service etc.

Caching is more useful when we want to store large amount of static data.We can also store portions of the page as well.

ASP.NET supports three types of caching for Web-based applications:
Page Level Caching (called Output Caching)
Page Fragment Caching (often called Partial-Page Output Caching)
Programmatic or Data Caching


Page Level Caching:

Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. The way ASP.NET implements this (roughly) is through an Output Cache engine. Each time an incoming ASP.NET page request comes in, this engine checks to see if the page being requested has a cached output entry. If it does, this cached HTML is sent as a response; otherwise, the page is dynamically rendered, it's output is stored in the Output Cache engine.
Output Caching is particularly useful when you have very static pages.
The syntax

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

The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache. When the duration expires, the cache becomes invalid

VaryByParam mentions how and when the data to be cached ,if we want to cached the data based on “NAME” in query string we mention as

VarByParam=”Name”

If VaryByParam attribute is None which means the page content to be served is the same regardless of the parameters passed through the querystring

If we want to vary the cached output based on type of browser.

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

Page Fragment Caching :


Page Fragment Caching allows specific regions of pages to be cached

Sometimes we might want to cache just portions of a page. For example, we might have a header for our page which will have the same content for all users. There might be some text/image in the header which might change everyday. In that case, we will want to cache this header for a duration of a day.
The solution is to put the header contents into a user control and then specify that the user control content should be cached. This technique is called fragment caching.
To specify that a user control should be cached, we use the @OutputCache directive just like we used it for the page.

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


Programtic or Data Caching :

The Cache object in ASP.NET provides the ability to store application data in a manner similar to the storing of data in the Application object. The Cache object, unlike the Application object, lets you specify that the cached data is to be replaced at a specified time or whenever there is a change to the original source of the data.
When the DataTable in the DataSet is stored in the Cache object, three parameters are passed to the Insert method of the Cache object. The first parameter is the "key" value used to access the data in the cache. A constant is used here since the key value is needed in several places in the code. The second parameter is the DataTable containing the book data. The third parameter is the dependency
context.Cache.Insert(Key,Datasetname,dependency) to get data from cache context.Cache.ITem(Key,Datasetname)

When data in the dependency is changed then cache object will also be refreshed.we can also mention duration for which the data is changed in the cache.

.The benefits

A reduced number of round trips to the data source, such as the database server, keeping the server resources more available for other operations.
An increase in the number of users supported, due to a faster response time to each user's request.
.The risks

Easily filling a computer's memory, which is relatively small, if you put a large amount of data in cache. As the memory gets full, the performance starts to decline, eventually leading to an unacceptable response time from the server.
Problems in a server farm environment, when we cache information in the server's memory, where various Web pages for the same user session may be served by different Web servers.
No guarantee of faster performance. It all depends on how effectively you manage objects in memory

No comments:

Post a Comment