C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

Caching in .Net


Posted Date: 28 Oct 2004    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: ENMember Level: Bronze    
Rating: 1 out of 5Points: 7



Introduction
Caching allows you to store page output or application data across HTTP requests and reuse it. Thus, the server does not have to recreate information, saving time and resources.
It allows subsequent requests for a particular page to be satisfied from the cache so the code that initially creates the page does not have to be run upon subsequent requests.Using this technique to cache your site's most frequently accessed pages can increase your Web server's throughput, commonly measured in requests per second, substantially.


Page caching

Caching can be performed on a per-page basis, or on a per-user-control basis, letting you store part or whole pages in a cache, and customise their expiration.

Page caching is controlled by the <%@ OutputCache %> directive, which has two compulsory parameters,
Duration: The number of seconds to keep the page in the cache,
VaryByParam; The query string parameters to use when determining page uniqueness.
For example, news articles may have urls like news.aspx?id=2, news.aspx?id=3. In this case, you would want to store a different entry in the cache for news article 2 than for article 3, so you could vary by the “id” parameter.

Object caching

Object caching: This allows you to store dynamic page and user control responses on any HTTP 1.1 cache-capable device in the output stream, from the originating server to the requesting browser. On subsequent requests, the page or user control code is not executed; the cached output is used to satisfy the request.
You can insert a value into the Cache object giving it a unique key and set its absolute or relative expiration time.

For example, we may want to cache some xml resulting from a complicated process, and store it for 12 hours. Each time we use the xml we check if it’s still in the cache:

XmlDataDocument x = null;
if (Cache["xmldata"] == null)
{
x = ComplexProcess();
Cache.Insert("xmldata", x, null, DateTime.MaxValue,
TimeSpan.FromHours(12), CacheItemPriority.High, null);
}
else
{
x = Cache["xmldata"];
}

Summary

Points to be noted:

Prioritise items in the cache: Because the cache stores items in memory, it may need to boot out some items to free up memory. Each time you insert something into the cache, use the overloaded version of Insert that allows you to indicate how important that item is, using one of the CacheItemPriority enumeration values.

Don’t cache everything: Again, because the cache is in-memory, you need to trade-off whether to regenerate items, or store them in memory.

Single point of maintenance: No-one wants to dig through code to find cache settings, so the usual rule applies here. You’ll find it easier in the long run if you store your cache settings in a central location.

Some queries answered
Where is the Cache maintained? App server or Client machine?
Ans; You can enable or disable page output caching for cache-capable devices in the request stream either declaratively or programmatically as well. In the @ OutputCache directive for a page you can use the Location attribute to specify whether the page's output can be cached on proxy servers, browser clients, the originating Web server, or all or none of these. You can do the same programmatically using the HttpCachePolicy.SetCacheability method to specify the appropriate HttpCacheability enumeration value for your page.




Responses

Author: harminder singh    29 Oct 2004Member Level: Bronze   Points : 0
u saved my life ..........
after logout clicking on browser back button it use to take me back to the previous page.
But now i can restrict user from going to previous page and also remove caching from other pages to make my site fast

Thanx
Avi


Author: harminder singh    29 Oct 2004Member Level: Bronze   Points : 0
my comment


Author: jana    29 Oct 2004Member Level: Bronze   Points : 0
thanx for the article


Author: Palwinder singh    05 Nov 2004Member Level: Bronze   Points : 0
Good performance Slogging approach.........Should be there for asp.net


Author: Naveen Dhawan    18 Nov 2004Member Level: Bronze   Points : 0
Caching in .net is really a nice article


Author: critic    22 Nov 2004Member Level: Bronze   Points : 0
This article seem to be already existing at http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=4. Do you own this article ?


Author: Atul Gawali    11 Oct 2005Member Level: Bronze   Points : 0
test


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Advantage of <IFRAME> tag
Previous Resource: How to avoid posting the page when Submit button is clicked?
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use