Caching is an important topic for any web application. Caching is storing data in server memory so that it can be used quickly by a web request.
In asp.net there are 3 different ways to cache data
- Output caching – caching the output of a page
- Fragment caching – Caching a user control
- Data caching – caching strings and objects
In this article, asp.net web development team will try to understand what output caching or data caching is, and how web applications with large datasets use this technique to speed up fetching up data from a database server and sending it to a browser.
Let us start with something very simple.
Suppose I want to insert a string into a cache object with some dependency, I can do it like this
Cache[“FirstName”] = “Irfan”;
The above code simply addsa string to the cache.
The question is how long does it remain in the server memory. Answer is, until there is no pressure on memory for other objects.
Now, suppose I want a cached item to refresh when a dependency like a file, or a db table or another cached item is changed. How can we do that? C# provides the CacheDependency class using which we add any of the above mentioned cached dependencies.
Let us study them one by one
1. File Dependency
Suppose I want to insert an object into cache with a dependency on a text file.
In the above code, the Employees.txt is a file dependency and a 5 minutes time dependency is also added. It means the file Employees.txt is polled every five minutes, and whenever there is change in that file, the cache is updated.
2. SQL Dependency
A cached item can be dependent on a database table. i.e. Whenever there is a change in the table, the cached item is refreshed.
For this purpose, we need to enable first sql dependency using the aspnet_regsql.exe command line tool.
aspnet_regsql -ed -E -d CompanyDB
In the above, CompanyDB is the name of the database.
Next, we have to enable caching for out Employee table.
aspnet_regsql -et -E -d CompanyDB -t Employee
The above line enables caching on Employee table
Next, we have to add Sql Cache Dependency by adding a connection string in the connection strings section in web.config
<sqlCacheDependencypollTime=”20000″ enabled=”true” >
<add connectionStringName=”ConnectionString” name=”School”/>
Here, we have set the poll time as 20000 which means asp.net will check the database for every 20 seconds.
The database section under <caching> node tell the name of the database.
Now, the final step is to use caching in our code
private void BindCacheData()
// if cache is empty fetch from the database
if (Cache[“Users”] == null)
// Create a cache dependency
SqlCacheDependencysqlDep = new SqlCacheDependency(“CompanyDB”, “Employee”);
string connectionString = ConfigurationManager.ConnectionStrings[
SqlConnectioncompanyDBSqlConnection = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(“SELECT FirstName, LastName ” +
“FROM Employee”, companyDBSqlConnection);
DataSet ds = new DataSet();
// put in the cache object
Cache.Insert(“Employee”, ds, sqlDep);
gvEmployee.DataSource = Cache[“Users”] as DataSet;
3. Cache Dependency
A cached item can be refreshed depending upon another cached item. This means a cached item A can be dependent on a cached item B.
protected void AddToCache(object sender, EventArgs e)
// create two items in cache
string itemA = “This is ItemA”;
string itemB = “This is ItemB”;
Cache.Insert(“ItemA”, itemA, null, DateTime.Now.AddMinutes(20),
Cache.Insert(“ItemB”, itemB, null, DateTime.Now.AddMinutes(20),
In the above, I have created two cached items ItemA and ItemB. Then I add them to Cache using the insert method. The last parameter is a callback method CacheItemRemovedCallBack which is fired when any of the two cached items is removed.
private void CacheItemRemovedCallBack (string key, object value,
// remove the item from the cache
if (key == “ItemA” )
else if (key.Equals == “ItemB”)
CacheItemRemovedCallBack method takes 3 parameters, key, value and reason. Key is the name of the cached item. Value is its value and the reason is of type CacheItemRemoveReason enumeration which identifies the reason the item was remove from the cache.
protected void RemoveCacheItem(object sender, EventArgs e)
The above code removes an item from the cache. As soon as the item is removed from the cache, the CacheItemRemovedCallBack is fired removing the other item from the cache.
Impact of the Solution
Caching is an important server side state management technique. In scenarios, where frequent fetching of data from a table in db can hamper the performance, data caching a boon. Applications like e-Commerce, CMS and others can greatly benefit from this technique.
Server side state management is essential for web applications transmitting huge amount of data. Application, Session and Caching are the server side statement management techniques out of which is Caching is crucial when dealing with data. Identifying the performance bottlenecks and overcoming them available features and techniques can greatly improve the way a web application works. Caching data with the required dependency and polling time can increase your applications performance to a great extent.