Introduction to ASP.NET Cache

No.of Views875
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  21 Jun 2010 11:06:19
Tag : ASP.NET , Caching
Caching is process of saving html and data in browser cache. Some data are difficult to obtain it assumes lot of resource of the server.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Caching is process of saving html and data in browser cache. Some data are difficult to obatin it assumes lot of resouceof the server. The best way to store it in cache. So we can retrive from cache instead doing round trip to the server.There are also some portion of the site does not change frequently so caching is a good idea to store this portion ofwebsite in cahce rather then loadind it from server.

ASP.NET also provide caching features.ASP.NET Caching API provides great features for caching.ASP.NET provide three types of caching.

  1. Page Output Caching
  2. Page Fragment Caching
  3. Data Caching

1.Page Output Caching

In ASP.NET you can do page output caching with output directive,Here is the code for output caching

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

 This means that page will cache for 120 second and during that time page will not change with get or post parameters.After 120 second page cache will be flush and then page content can be change by get or post parameters.

2.Page Fragment Caching

We all know that it is impratical to cache whole page of website. So it is better cache a portion of a website thenwhole page. ASP.NET caching API also provide that feature.From Following code asp.net can cache categoryid parameter of get and post method.You can also cache whole control in asp.net which can be used as portion of parameter.

<%@ OutputCache Duration="120" VaryByParam="none" VaryByControl="ViewCategory" %>

 This will cache whole user control called viewcategory.

3. Data Caching

For that ASP.NET Caching API provides two classes in System.Web.Cache name space.The first class, Cache, is the classwill be using to add and remove items from the data cache. The second class, CacheDependency,is used when assigning a cache dependency to an item in the data cache.
 

To add an item to the cache you can simply do:

' In VB.NET
Cache("name") = value


// In C#
Cache[
"name"] = value;


to get value

'IN VB.NET
value = Cache(
"name")

- or -

//IN C#
value = Cache.Get(
"name")

 That's all,Happy Programming.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Expert
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
By:WaqasDate Of Posted:4/20/2011 5:21:32 AM
Distributed cache
cache is asp.net is stand alone so its a good option for smaller web farms only. but in a larger web garden you need to have an in-memory cache for results and performance. http://www.alachisoft.com/ncache/asp-net-cache.html
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top