Cache in ASP.Net application
Cache is a temporary storage place inside a computer. This storage place is used to store and access data which is frequently used in an application. Cache is pronounced cash.
This article is about using a cache in a web application. A web application is accessed by many people via the Internet. It is important that the visitors to the web site (web application) are able to experience a fast page load. This enhances user experience and entices visitors to keep coming back. This is one of the major requirements of creating and running a web application.
We will discuss this topic in the context of ensuring faster page upload and less frequent data access. Note that the cache feature is available in almost web application programming languages. For instance PHP has its set of classes and methods to implement cache. You can have a detailed overview of PHP cache at:
http://en.wikipedia.org/wiki/PHP_accelerator
Coming back to ASP.Net Cache, there are two types of implementing Cache. They are
- Page output caching
- Application caching
As mentioned before, cache is used to reduce frequency of data access. This is accomplished by storing a copy of the web application or web page in memory and feeding this copy to the user’s browser whenever a request is made. However, ASP.Net provides different ways of creating and using a cache.
Page output caching
Let us now recall how a web page loads.
When a browser requests a web page, the IIS server (web server) processes the request and creates the cache of the output. This cache is sent to the browser every time the browser requests the page. IIS has to find the directive in the ASP.Net web page.
<%@ OutputCache Duration=”10” VaryByParam=”none” %>
ASP.net provides options to specify the duration of the Cache.
This method of creating Cache is called as page output caching.
A high-traffic Web page that has to display data that is not frequently updated, is suitable for implementing page output caching. Using Webconfig file, page caching can be configured for many pages. Page output caching dramatically increases the performance by eliminating the need for going through the asp.net page life cycle for creating the page output. Note that for the first time page the IIS has to generate an output and send it to the browser. The freshness of the data is dependent on the duration of the cache.
There are two models for page output caching:
Full page caching
Full page caching is used to cache the entire contents of a page.
Partial page caching.
Partial page caching is used to cache the parts of a web page which are dynamic.
Application caching
In many web applications, it may be necessary to cache only that data which is generated from a database. In such circumstances, application caching is used. In this method, we can cache only that data such as DataSet which is created from the database and use it whenever required. The applcation of this method can be in applications where the same data is required to be shown in multiple pages.
We create the cache programmatically using Cache class which includes key/value pairs.
Application Cache programming involves determining whether the required data exists in the cache and if it does use it and if not recreate the cache.
Two instances when we find using cache is beneficial.
When an application processes large amounts of data.
When an application processes data which is updated only occasionally.
How to implement cache?
The following code shows how to implement application caching.
Application caching
DataSet dstNews;
dstNews = Cache["News"] as DataSet;
if (dstNews == null) {
strConString = "Data Source=202.XX.XXX.XX;database=newsabc;User ID=abc123;Password=cd546dc;";
conRes= new SqlConnection( strConString );
dstNews = new DataSet();
//Get Newest 100 news From Database
dadNews = new SqlDataAdapter( "getNews", conRes );
dadNews.SelectCommand.CommandType = CommandType.StoredProcedure;
dadNews.Fill( dstNews, "news_msg");
Cache["News"] = dstResumes;
}
Page output caching
The following statement shows how to implement Page output caching.
<%@ OutputCache Duration=”10” VaryByParam=”none” %>
Comments