Friday, June 17, 2005 8:27 AM
Well, as explained in my previous post, Caching has been improved a lot in Whidbey thanks to the SQL Cache Notification and other new mechanisms which provide way to create robust applications leveraging the power of Caching to improve performance.
SQL Cache Notification
When you enable Output Cache or Fragment Cache for your page, the data gets cached for the specified duration, regardless of whether the data has changed in the database.
The New SQL cache invalidation enables you to make the cache entry dependent on the database, so the cache entry will only be cleared when data in the database is changed.
This way is very useful since you can create the dependency to your Database Table and then set the Cache such that, when the Data is updated, you can ensure that the Cache will get invalidated and subsequent requests gets the New Data. This is particularly useful in scenarios where you show time-sensitive reports such as a Railway Reservation Status or Stock Market Status.
To enable SQL Cache Notification, there are 2 methods.
1. Polling-based Invalidation
This method can be useful when we are targetting SQL Server 2000 / SQL Server 7.0 Database versions since in SQL Server 2005, we have the Notification-based Cache Validation which is even simpler.
The Polling-based Invalidation works by polling to check if a table has been updated since the page was cached.
To enable this, Polling must be enabled for the Database. To do that, the following command needs to be run from the Visual Studio Command Prommpt:-
aspnet_regsql.exe -S ".\SQLExpress" -E -d "pubs" -ed
Where "pubs" is the name of the Database to be enabled for Polling.
This command needs to be executed once for each Database for which we would like to enable Polling.
Next, Polling must be enabled for the Table that we would like to keep checking.
To do that, the following command needs to be run from the Visual Studio Command Prompt:-
aspnet_regsql.exe -S ".\SQLExpress" -E -d "pubs" -et -t "authors"
Where "pubs" is the Database name and "authors" is the name of the Database Table.
Finally, we need to register the notification in the Web.config file of the application as follows:-
<system.web>
<caching>
<sqlCacheDependency enabled="true" pollTime="1000" >
<databases>
<add name="PubsDB" connectionStringName="Pubs" />
</databases>
</sqlCacheDependency>
</caching>
</system.web>
The pollTime specifies the interval the application takes between consequent polling to the database.
Then, a SQL Dependency can be set at the Page level as follows:-
<%@ OutputCache Duration="999999" SqlDependency="Pubs:Authors" VaryByParam="none" %>
Or, directly for a DataSource, as follows:-
<asp:SqlDataSource EnableCaching="true" CacheDuration="Infinite" SqlCacheDependency="PubsDB:Authors" ... />
Thus, the SQL Dependency has been created for the Authors table in the Pubs Database and once the Data is changed in Authors Table, the Cache gets invalidated.
2. Sql Server 2005 Notification-based Cache Invalidation
SQL Server 2005 offers a simpler way to enable SQL Cache Dependency without requiring to enable Polling for Database/Table and registering in the Web.Config file.
A notification based dependency is configured on the OutputCache directive using the string CommandNotification. This value indicates to ASP.NET that a notification based dependency should be created for the page or datasource control.
To Enable Notification-based Cache for a page, the following is an example:-
<%@ OutputCache Duration="999999" SqlDependency="CommandNotification" VaryByParam="none" %>
To Enable for a DataSource,
<asp:SqlDataSource EnableCaching="true" SqlCacheDependency="CommandNotification" CacheDuration="Infinite" ... />
Whenever a command is issued to Sql Server 2005, ASP.NET and ADO.NET will automatically create a cache dependency that listens to change notifications sent from the Sql Server. As data is changed in Sql Server, these notifications will cause the cached queries to be invalidated on the web server. The next time a page or datasource control associated with the dependency is requested, the page or datasource control will be executed again as opposed to serving cached information.
There are other techniques such as the New Fragment Caching API, Disk Output Caching which we will explore in the future articles.
Cheers and Happy Caching !!!