Data Cache in ASP.NET


Introduction

This is my first articles. I am going to publish on the C# Corner. I need everyone support who are reading articles. I have read many articles before writing this article. Done some hands on also. Hope I have explained this well, and hope you guys also like it. Please give your suggestion and feedback.

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"] = "DataSet Name"

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 dsEmployee = (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);

Example:

TimeSpan ts = new TimeSpan(0, 0, 10);       

//If cache has null, it will create two cache and it will bind into the gridview       

if (Cache["Employee"] == null)

{           

dtEmployee = new DataTable("Employee");

dtEmployee.Columns.Add("EmpID", typeof(int));

dtEmployee.Columns.Add("EmpName", typeof(string));

DataRow rs = dtEmployee.NewRow();

rs[0] = 10;
rs[1] = "Annathurai";
dtEmployee.Rows.Add(rs);

//To assign datatable to cache memory.           

Cache["Employee"] = dtEmployee; Cache.Insert("Employee", dtEmployee, null, Cache.NoAbsoluteExpiration, ts); Response.Write("Its processing with Database hit");      

}      

else      

{          

//If cache has value, It retrive from cache memory and bind into the gridview          

Response.Write("Its processing from cache");      

}

/Here we are converting cache into datatable       

GridView1.DataSource = (DataTable)Cache["Employee"];      

GridView1.DataBind();


Summary

There are many topics to learn with Data Caching. I have covered a small portion hope this will helps all the beginners to startup. Please give your feedback and suggestion.

History

  • Written on 01-March-2010, Monday

Up Next
    Ebook Download
    View all
    Learn
    View all