User Id :    Password :      New Member   Forgot Password  
 
Caching in ASP.Net
Description Caching is a feature of ASP.NET that can dramatically improve the performance of your application by storing the page output or application data across HTTP requests.   No. of Views     4405
  Rating     4
Author Najmul Hoda   Posted On     21 Jan 2010
Tags ASP.NET,Session Management    

Sample Code   Download Code

Introduction

Cachingis a feature of ASP.NET that can dramatically improve the performanceof your application by storing the page output or application dataacross HTTP requests.

This article cover:

* What is Caching ?
* Benefits of Caching
* Introducing the Problems that Caching Solves
* Types of Caching

What is caching?

* Caching is a feature that stores data in memory, allowing incoming requests to be served from memory directly.
* In the context of a web application, caching is used to retain pagesor data across HTTP request and reuse them without the expense ofrecreating them.


Benefits of Caching

The following are the benefits of using Caching:

* Faster page rendering
* Minimization of database hits
* Minimization of the consumption of server resources


Types of Caching

* Page Output Caching
* Page Fragment Caching
* Data Caching

* Page Output Caching:

Pagelevel, or output caching, caches the HTML output of dynamic requests toASP.NET Web pages. The way ASP.NET implements this is through an OutputCache engine. Each time an incoming ASP.NET page request comes in, thisengine checks to see if the page being requested has a cached outputentry. 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.

Outputcaching is easy to implement. By simply using the @OuputCache pagedirective, ASP.NET Web pages can take advantage of this powerfultechnique.


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

TheVaryByParam parameter is used to indicate whether any GET (QueryString)or POST (via a form submit with method="POST") parameters should beused in varying what gets cached.

* Page Fragment Caching

In addition to output caching an entire page, ASP. Net provides Partial-Page Output
Caching, or Page Fragment Caching. Page Fragment Caching allows specific regions of pages to be cached.

Page Fragment Caching is easy to implement. By simply using the @OuputCache
page directive, ASP.NET Web pages can take advantage of this powerful technique.
The syntax looks like this:


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

TheVaryByParam parameter is used to indicate whether any GET (QueryString)or POST (via a form submit with method="POST") parameters should beused in varying what gets cached.

The VaryByControl parameter allows different cache entries to be made based on the values for a specified control.


* Data Caching

ASP.Net provides a full-featured cache engine, Programmatic or data cachingtakes advantage of the .NET Runtime cache engine to store any data orobject between responses. That is, you can store objects into a cache,similar to the storing of objects in Application scope in classic ASP.

TheASP. Net cache is private to each application and stores objects inmemory. The lifetime of the cache is equivalent to the lifetime of theapplication.

To store a value in the cache, use syntax like this:




Cache[“myKey"] = myValue; // C# 
Cache(“myKey") = myValue ' VB.NET
To retrieve a value, simply reverse the syntax like this:

myValue = Cache[“myKey"]; // C# 
myValue = Cache(“myKey") ' VB.NET
Thanks

About Author

About Author He is a team lead, having 8+ years of experience. He loves writing technical article for .Net developers. Najmul Hoda
No Photo
 
Country United States
Company BirlaSoft
Home Page http://www.magixchat.com

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

 
 
Posted By Ramona on 24 Jul 2011 at 11:13 PM
 
Very nice article! No doubt asp.net caching improves performance but only at smaller level. It can't work efficiently at larger level because it is not scalable and may be it left with some data integrity and consistency issues. I'd recommend asp.net caching when scalability is not our major concern but if t is then I'd rather refer Distributed cache (like NCache, AppFabric). Distributed caching is highly scalable and it solves all the data integrity and consistency issues.
 
 
 
Posted By Manoj Garg on 08 Apr 2010 at 07:09 AM
 
Excellent Work.
 
 
 
Posted By hii on 15 Mar 2010 at 04:37 AM
 
good
 
 
 
Posted By Lovleen on 22 Feb 2010 at 01:25 AM
 
Really doing gr8 work.
 
 
   
Write your comment here.
Comment
Verification Code