18 May 2012

Data Cache in ASP.NET

Most of the times we need to store large amounts of data client side is by using the ViewState. It will only exist on the page you add it to though. So if you are jumping around from page to page it won't really work. In that case you really should be using Session or Application Cache depending on your scenario.


ViewState:  
 ViewState is one of the techniques that Asp.net provides in order to save the data for latter use. The important fact about the ViewState is that it can only save the values for page level access. It means that if I save something on page1.aspx using Viewstate I cannot access it on the page2.aspx using the same technique. For this reason ViewState is only used to save the values for the page level access which should be maintained after post backs.Whenever we write anything in the ViewState object a hidden field is created in the page. After the post back it will display the value of the dataset using the ViewState control.

So if you are doing something where you are always on the same page and just doing many PostBacks for things like paging or sorting then ViewState will work fine but do realize you will be passing large amounts of data back and forth to the server each time a PostBack is made.
Example Using ViewState:

// Set it
ViewState["dsViewEmp"] = dsEmployee;


// Get it
DataSet ds = ViewState[" dsViewEmp "] as DataSet;


Session and the Application Cache are accessed the same way. Just replace ViewState with the word Session or Cache.


What is Data Cache?
Data Cache is used to storing frequently used data in the Cache memory. It's much efficient to retrieve data from the data cache instead of database or other sources. We need use System.Web.Caching namespace. The scope of the data caching is within the application domain unlike "session". Every user can able to access this objects.


How data caching is work?
When client request to the server, server execute the stored procedure or function or select statements on the Sql Server database then it returns the response to the browser. If we run again same process will happen on the web server with sql server.
In the very first time retrieve the records from the sql server database and stored into the Cache memory.  After that records will retrieve from Cache memory (IIS). We can set expiration period time after expiration time over the caching again it will hit the sql server database and stored into the cache memory and next retrieve will happen on the data cache memory.


Advantages of Cookies
Following are main advantages of using cookies in web application:
It's very simple to use and implement.
We can avoid database hitting.
We can reduce network traffic.

How to create data cache? 
Cache["Employee"] = dsEmployee


We can create data caching use Cache Keyword. It's located in the System.Web.Caching namespace. It's just like assigning value to the variable


How to Read data from Data Cache?
Dataset dsEmployees = (Dataset) Cache ["Employee"]


This is very similar to assigning object to the Dataset.


Where does Data Cache are stored?
This is one of the interesting things to find out the Data Caching in your local drive. First of all, From "IE Internet Option ", delete Browsing History to clear Cache. 


How to remove a Data Cache? 
We can remove Data Cache manually.

//We need to specify the cache name


   Cache.Remove(String key);



Conclusion:
When it comes to choosing between ViewState and Caching it all comes down to what you are doing. ViewState is an ideal candidate if you are planning to save small values. Mostly values of server controls are saved in ViewState. If you plan to store large datasets, datatables or collections than data caching is an excellent candidate.


ReferenceLinks:
1. http://www.highoncoding.com/Articles/69_View_State_vs_Data_Caching.aspx
http://msdn.microsoft.com/en-us/library/z1hkazw7%28VS.80%29.aspx






No comments:

Post a Comment