Sunday, January 25, 2009

Difference between cache, session and viewstate

Cache: Its nothing but a thought kind of memory. In respect to asp.net it’s the memory of the machine/server from where the source-code is running. It is the one way which allow to store complex data for reusability.
Now think a criteria where clients access an ASP.NET page, there are basically two ways to provide them with the information they need:
  • The ASP.NET page can either obtain information from server resources, such as  from data that has been persisted to a database, or
  • The ASP.NET page can obtain information from within the application.
Retrieving information from a resource outside the application will require more processing steps, and will therefore require more time and resources on the server than if the information can be obtained from within the application space.

Now, suppose the information’s which sent to browser’s have already been prepared then how faster the process of web-page.

The ASP.NET 3.5 Framework supports the following types of caching:
Page Output Caching :Page Output Caching caches an entire page.          

Partial Page Caching :Partial Page Caching enables you to get around this problem by enabling you to cache only particular regions of a page.

DataSource Caching:You use DataSource Caching with the different ASP.NET DataSource controls such as the SqlDataSource and ObjectDataSource controls. When you enable caching with a DataSource control, the DataSource control caches the data that it represents.
 
Data Caching :Finally, Data Caching is the fundamental caching mechanism. Behind the scenes, all the other types of caching use Data Caching. You can use Data Caching to cache arbitrary objects in memory. For example, you can use Data Caching to cache a DataSet across multiple pages in a web application.
 
Note: 
The Cache object can also have an expiration which would allow us to reinstitute data into the memory in intervals. Using the same example as above, we can make the cache expire every two hours, and repopulate the data. It would do this every 2 hours throughout the day, allowing the most up to date data to be fetched. Below is an example of how something can be put into the cache: 

Referenced from msdn 
 
public void AddItemToCache(Object sender, EventArgs e) 
    itemRemoved = false; 
    onRemove = new CacheItemRemovedCallback(this.RemovedCallback); 
    if (Cache["Key1"] == null) 
        Cache.Add("Key1", "Value 1", null, DateTime.Now.AddSeconds(60),TimeSpan.Zero, CacheItemPriority.High, onRemove); 
 
 
public void RemoveItemFromCache(Object sender, EventArgs e) 
    if(Cache["Key1"] != null) 
        Cache.Remove("Key1"); 
}
 
 
Session: Its nothing but defined as a period of time shared between the web application and user. Every user has individual session. Items/Objects can be placed into the Session which would only define these object for that user. Session contains key variables which help to identify the related values. This can be thought of as a hash table. Each user would represent a different key node in the hash identifying unique values. The Session variables will be clear by the application which can clear it, as well as through the timeout property in the web config file. Usually the timeout is 20 minutes by default.
 
Session Variables are stored on the server, can hold any type of data including references, they are similar to global variables in a windows application and use HTTP cookies to store a key with which to locate user's session variables.
 
The collection of session variables is indexed by the name of the variable or by an integer index. Session variables are created by referring to the session variable by name. You do not have to declare a session variable or explicitly add it to the collection.
 
Lets get it cleared from following example:
 
Session[“firstName”] = “Gaurav”    //User’s first name
Session[“lastName”] = “Arora”        //User’s last name
 
// Clear the session variable 
Session[“FirstName”] = null; 
 
//Clear all Session variables 
Session.Abandon();  
 
 
Note:By default, ASP.NET session state is enabled for all ASP.NET applications.
 
 
ViewState: Its nothing but it’s a hidden data which is kept by asp.net pages in “_VIEWSTATE” as hidden form field. They track the changes to a web site during post backs.
 
The ViewState of a page can be seen if you view the source code of the running page(Right Click --> View Source). It will be listed under the following tags:
Having a large ViewState will cause a page to download slowly from the users side. When a user clicks on a button and a post back occurs, all the view state information has to be posted back to the server which causes a slower request. Most times this is insignificant, but imagine a person using a dial up connection. If requests, and page loading is taking a couple of minutes, most likely the user will get frustrated, and leave.

Note:
  1. By default, View State is enabled for every control in the ASP.NET Framework.
  2. ViewState does not hold the controls, rather it holds the values of the form controls and their corresponding ID's that would otherwise be lost due to a post back because they do not post with the form.
  3. ViewState is not used to hold session data or to transmit data between pages.
  4. ViewState does not recreate the dynamically created controls of a page.
  5. It does not restore the values to the controls after a post back operation.

No comments:

Post a Comment