Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

ASP.NET and Caching Dexterity

, 21 Jan 2013
Rate this:
Please Sign up or sign in to vote.
When it comes to build a high-performance and scalable ASP.Net Web applications Caching is inevitable. It has the ability to store objects whether its data object or pages or controls or even parts of a page. The plan is to keep them in memory while initially requested.

When it comes to build a high-performance and scalable ASP.Net Web applications Caching is inevitable. It has the ability to store objects whether its data object or pages or controls or even parts of a page. The plan is to keep them in memory while initially requested. You can store them in web-server or in proxy-server or in browser, your choice. Using this feature prevents you recreating information while it already satisfied in previous request. The motive is reusing the objects when they are needed. 

Basically ASP.Net provides two types of caching. Output caching and another one is traditional application data caching. There are few good practices to cache your data. When the data is going to be used more than once it’s a candidate for caching and if data is general rather than specific to a given request or user, it’s a great candidate for the cache. But the thing sometimes developers often overlook is that they can cache too much and it causes out of memory exception. Therefore, caching should be bounded. In my experience ASP.NET out-of-memory errors caused by overcaching, especially of large datasets.

So how do you use this? – If your components are running within an ASP.NET application, you simply need to include a reference to System.Web.dll in your application project. When you need access to the Cache, use the HttpRuntime.Cache property (the same object is also accessible through Page.Cache and HttpContext.Cache).

The declarative approach of Output cache is kinda like this:

<%@ OutputCache Duration="60" Location="Server" VaryByParam="None" %>

By default Location property sets to "Any" which means this stores the output cache in the client’s browser, on the proxy server (or any other server) that participates in the request, or on the server where the request is processed.

The choices you have are "Server", "Client", "Downstream" or "None".

And the programmatic approach for Output cache is

TimeSpan freshness = new TimeSpan(0,0,0,60);
DateTime now = DateTime.Now;
Response.Cache.SetExpires(now.Add(freshness));
Response.Cache.SetMaxAge(freshness);
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);

Here is an example. Suppose your target is to fill a DataGrid, if the DataSet is already been stored you’ll ignore re-filling it, otherwise you’ll fill the DataSet and will cache it.

 DataView Source;
 // Retrieve the DataView object from Cache. If not exist, then add DataView object to the Cache.
 Source = (DataView)Cache["MyDataSet"];
 if (Source == null) {
    SqlConnection myConnection = new SqlConnection("Server=ServerName; database=Pubs; user id=UID; password=PWD;");
    SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);
  
   DataSet ds = new DataSet();
   myCommand.Fill(ds, "Authors");
  
   Source = new DataView(ds.Tables["Authors"]);
   Cache["MyDataSet"] = Source;
   CacheMsg.Text = "Dataset created explicitly";
 }
 else {
   CacheMsg.Text = "Dataset retrieved from cache";
 }
  
 // Binding the DataView object with DataGrid.
 DataGrid1.DataSource=Source;
 DataGrid1.DataBind();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Md Nazmoon Noor
Software Developer (Senior)
Bangladesh Bangladesh
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberAlenty23-Jan-13 14:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 21 Jan 2013
Article Copyright 2013 by Md Nazmoon Noor
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid