Datasheet
allows developers to aggressively output cache database-driven page and partial page content within a
site, and have ASP.NET automatically invalidate these cache entries and refresh the content whenever the
back-end database changes. ASP .NET 2.0 also introduces the
Substitution control, which allows you to
link dynamic and cached content in a web page.
Caching with the DataSource Controls
The DataSource controls enable you to cache database data while connecting a .NET application to a
database. The
DataSource control provides various properties, such as EnableCaching, which you can
use to automatically cache the data represented by a
DataSource control. The syntax to cache a
database table in a memory for 120 seconds is:
<asp:SqlDataSource ID=”SqlDataSource1” EnableCaching=”True” CacheDuration=”120”
ConnectionString=”Server=localhost;database=AdventureWorks;uid=user;pwd=word;”
SelectCommand=”SELECT * FROM Production.ProductCategory” Runat=”server”/>
The above syntax caches a database table, ProductCategory, by setting the EnableCaching property
of the
DataSource control to True. The CacheDuration property of the DataSource control specifies
the time, in seconds, for caching the data before it is updated in a database containing the
ProductCategory table. The value of the Time parameter is set to 120 to cache data for two minutes.
Using SQL Cache Invalidation
The Cache API introduced with ASP.NET 1.x was a powerful feature that can be immensely useful in
increasing the performance of a web application. The Cache API also allows you to invalidate items in
the cache based on some predefined conditions, such as change in an XML file, change in another cache
item, and so on. Using this feature, you can remove or invalidate an item from the cache when the data
or another cached item changes. However, the Cache API in ASP.NET 1.x versions did not provide a
mechanism to invalidate an item in the cache when data in a SQL Server database changed. This is a
very common capability that many web applications require. Now with ASP.NET 2.0, Microsoft has
introduced a new cache invalidation mechanism that works with SQL Server as well. Using this new
capability, you can invalidate an item in the
Cache object whenever the data in a SQL Server database
changes. This built-in cache invalidation mechanism works with SQL Server 7.0 and above. However,
with SQL Server 7.0 and 2000, only table-level cache invalidation mechanism is supported. The next
release of SQL Server (named SQL Server 2005) will also feature a row-level cache invalidation mecha-
nism, providing a finer level of accuracy over the cached data. To enable the SQL Server–based cache
invalidation mechanism, you need to do the following:
1. Add a <caching> element to the Web.config file, and specify the polling time and the connec-
tion string information.
2. Enable SQL cache invalidation at the database and table levels by using either the
aspnet_regsql utility or the SqlCacheDependencyAdmin class. This is not required
if you are using SQL Server 2005 as your database.
3. Specify the SqlCacheDependency attribute in the SqlDataSource control.
That’s all you need to do to leverage SQL Server cache invalidation from your ASP.NET pages.
21
Chapter 1: Introduction to ASP.NET 2.0
05_041796 ch01.qxp 12/29/06 9:09 PM Page 21