Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Framework  »  Advanced Caching Techniques in ASP.NET 2.0  »  ASP.NET 2.0
 »  Home  »  Data Programming  »  ADO.NET  »  Advanced Caching Techniques in ASP.NET 2.0  »  ASP.NET 2.0
 »  Home  »  Data Programming  »  SQL Server  »  Advanced Caching Techniques in ASP.NET 2.0  »  ASP.NET 2.0
 »  Home  »  Data Programming  »  XML  »  Advanced Caching Techniques in ASP.NET 2.0  »  ASP.NET 2.0
 »  Home  »  Visual Studio 2005  »  Advanced Caching Techniques in ASP.NET 2.0  »  ASP.NET 2.0
 »  Home  »  Web Development  »  ASP.NET  »  Advanced Caching Techniques in ASP.NET 2.0  »  ASP.NET 2.0
Advanced Caching Techniques in ASP.NET 2.0
by Sandeep Joshi | Published  05/05/2006 | .NET Framework ADO.NET SQL Server XML Visual Studio 2005 ASP.NET | Rating:
ASP.NET 2.0

A new wave: ASP.NET 2.0 Caching

One of the biggest changes in the ASP.NET 2.0 framework concerns how you access database data in an ASP.NET page. The ASP.NET 2.0 framework includes a new set of controls, known collectively as the DataSource controls, which enable you to represent a data source such as a database or an XML file.
In the ASP.NET 1.1 framework, the controls are bounded with DataSet or DataReader. In the ASP.NET 2.0 framework, we bind a control to a DataSource control instead. The DataSource control is having a better functionality and greater capabilities. It minimizes the code to be written to develop a database driven page. In ASP.NET 2.0 following three DataSource controls are there:
SqlDataSource: Represents any SQL data source, i.e. Microsoft SQL Server or an Oracle database.
AccessDataSource: A specialized SqlDataSource control designed for working with a MS Access.
ObjectDataSource: Represents a custom business object that acts as a data source.

Caching with the DataSource Controls

Th DataSource controls provide direct support for caching the database data. The DataSource control properties can be defined to cache automatically the data represented by a DataSource control in memory.

For example, if you want to cache the Northwind database table in memory for at least 10 minutes, you can declare a SqlDataSource control using following code:

ConnectionString="Server=localhost;database=Northwind"
SelectCommand="SELECT CategoryId FROM Categories"Runat="server" />

The EnableCaching property controls automatic caching of the data retrieved by the SelectCommand. The CacheDuration property specifies the duration of caching.

By default, the SqlDataSource will cache data using an absolute expiration policy. This can also be configured to use a sliding expiration policy.

SQL Cache Invalidation: The Most Awaited


SQL Cache Invalidation is one of the most anticipated new features of the ASP.NET 2.0 framework. It is a mechanism which enables you to automatically update data in the cache whenever the data changes in the underlying database.

How it works?


SQL Cache Invalidation works by constantly polling the database to check for changes. After every definite milliseconds period, the ASP.NET framework checks whether or not there have been any changes to the database. If the ASP.NET framework detects any changes, then any items added to the cache that depend on the database are removed from the cache (they are invalidated).
SQL Cache Invalidation only works with Microsoft SQL Server version 7 and higher. This is not supported in MS Access or Oracle.
Configuring SQL Cache Invalidation
To configure SQL Cache Invalidation, we have to perform following steps:
1.Configure SQL Server to support SQL Cache Invalidation
2.Configure Application’s Web Configuration File
Configuring SQL Server for SQL Cache Invalidation
To configure SQL Server for Cache Invalidation we can either use the aspnet_regsql command line tool, or SqlCacheDependencyAdmin class. We’ll configure using aspnet_regsql tool.

Enabling SQL Cache Invalidation with aspnet_regsql tool


The aspnet_regsql tool is a command line utility to enable SQL Cache Invalidation. The aspnet_regsql tool is located in your Windows\Microsoft.NET\Framework\[version] folder. This tool can be directly run using “Visual Studio .NET Whidbey Command Prompt” option in the Programs Visual studio tools Menu. If you run this tool directly, this will start a wizard which looks like the figure 1a. However, to set the SQL Cache Invalidation, we need to use it with certain arguments.
In order to support SQL Cache Invalidation when using the Northwind database, you need to execute the following command.aspnet_regsql -E -d Northwind -ed
The -E option causes the aspnet_regsql tool to use integrated security when connecting to your database server. The -d option selects the Northwind database. Finally, the -ed option enables the database for SQL Cache Invalidation.
This command creates a new table named AspNet_SqlCacheTablesForChangeNotification in the database. This table contains a list of all of the database tables that are enabled for SQL Cache Invalidation. The command also adds a set of stored procedures to the database.After this, you need select the particular table in the database to which you want to enable the Cache Invalidation. The following command will do that:
aspnet_regsql -E -d Northwind -t Categories –et
The -t option selects a database table. The -et option enables a database table for SQL Cache Invalidation. For multiple tables, you need to execute the command for each table.
This command adds a trigger to the database table. The trigger fires whenever you make a modification to the table and it updates the AspNet_SqlCacheTablesForChangeNotification table.

Application Web Configuration Settings for SQL Cache Invalidation


The next step is to configure the ASP.NET framework to poll the databases that you have enabled for SQL Cache Invalidation. Following listing is the web.config file for your application.
Web.Config File listing

In the Web configuration file, the <CONNECTIONSTRINGS>section is used to create a database connection string to the Northwind database named CacheSqlServer.
The section is used to configure the SQL Cache Invalidation polling. You can specify different polling intervals for different databases in the section.

Comments    Submit Comment

Comment #1  (Posted by an unknown user on 05/10/2006)
Rating
Hi Sandeep,

Your article was really nice and neat. But a query on the performance side for caching.

Queries:
1. "SQL Cache Invalidation works by constantly polling the database to check for changes", isn't this a hit on the performance itself. Agreed that it wont bring tons of data but querying the database in the enterprise environment will itself be a major hit on performance. Can we configure this querying time?
2. I am not sure how database works internally but still the query. Does the database have to do extra processing to keep the track of all the changes done for the cache or irrespective of it? If yes, that will be another performance hit. If not then much better.

My email id is melvyn_taraporewalla@yahoo.com. I will await your response.

Regards,

Melvyn Taraporewalla
 
Comment #2  (Posted by rav on 05/11/2006)
Rating
thanks for sharing
 
Comment #3  (Posted by an unknown user on 05/12/2006)
Rating
Should not be labeled as an advanced article.
 
Comment #4  (Posted by Vibhu on 05/17/2006)
Rating
Pretty nice article
 
Comment #5  (Posted by rosekiller on 05/21/2006)
Rating
hi, man. I like it.
 
Comment #6  (Posted by Sandeep Joshi on 06/28/2006)
Rating
Answers to the Queries:
1. "SQL Cache Invalidation works by constantly polling the database to check for changes", isn't this a hit on the performance itself. Agreed that it wont bring tons of data but querying the database in the enterprise environment will itself be a major hit on performance. Can we configure this querying time?
RE: In SQL Cache Invalidation, the polling happen only when you use SQL Server 2000 or SQL Server 7.It is a continuous thread which is run by ASP.NET to poll the database. When you enable Polling for SQL Database, you can also configure the polling time in the web.config file.

For SQL Server 2005, the polling doesn't happen at all. It uses the SQL Server 'Service Broker' object to do all the invalidation magic. Above all there is no manual steps required to enable the notification for SQL server 2005 database.


2. I am not sure how database works internally but still the query. Does the database have to do extra processing to keep the track of all the changes done for the cache or irrespective of it?

Re: When you enable the SQL cache invalidation in SQL server 2000 for a database, it will create a new table(AspNet_SQLCacheTablesForChangeNotification) in that database. This table will contain 3 columns(tableName,notificationCreated abd ChangeId). Whenever a row is added/deleted/modified in a table for which you have enabled the notification, a new row will be added to the table.
This is the table which is monitored/polled by ASP.NET process for tracking the changes. So no additional work and it is handled smartly.

 
Comment #7  (Posted by an unknown user on 11/29/2006)
Rating
It’s really excellent
 
Comment #8  (Posted by an unknown user on 01/14/2007)
Rating
Well only theory,it would have be better if any interesting sample application demo included in this article.
 
Comment #9  (Posted by an unknown user on 03/02/2007)
Rating
Good Work, and yes use cache in your favor programatically is advanced ^^
 
Comment #10  (Posted by jhansi on 03/13/2007)
Rating
Article is good. If it has one example on fragment caching , it would be excellent.
 
Comment #11  (Posted by dami on 04/05/2007)
Rating
nice article. if you explained with proper program examples it would have been much better.
 
Comment #12  (Posted by an unknown user on 04/24/2007)
Rating
HI Sandeep,

I created a service broker which I am using for a queue and now I need a notification in my application whenever data is inserted in the queue. So can we do this with caching concept? (I am using sqlserver 2005 and asp.net 2005 in my application.)

My email ID is shaileshag2002@gmail.com I will await for your response.

Thanks,
Shailesh
 
Comment #13  (Posted by an unknown user on 05/03/2007)
Rating
Hi,
How can we do similar functionality with Oracle Database.
Thanks in advance
Regards
sridhar
 
Comment #14  (Posted by an unknown user on 09/30/2007)
Rating
good
 
Sponsored Links