As I mentioned before, there have been numerous improvements to caching in ASP.NET 2.0. Along with the use on DataSource controls, there is a new type of cache invalidation that allows you for the cache to be updated any time a change is made to a particular Sql Table.
The way this work varies by which version of SQL Server you are running in SQL Server 2000 / 7, the process works by polling the SQL Server at a set interval to see if anything has changed. The way this works is by setting up a new table using aspnet_regsql. This program has a number of wizards and command line parameters to set up all the various new features in ASP.NET 2.0 (i.e.: Profile Database, Page Counting, Cache Invalidation, etc). Basically, how this works is that it sets up a trigger on the table you are monitoring and copies data to another table to indicate that the cache has been invalidated.
Here is what your web.config would look like in this scenario:
<configuration>
<connectionStrings>
<add name="LocationConnectionString"
connectionString="Server=tweb24\thrifty;Database=Teamowner" />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled="true">
<databases>
<add
name="Location"
connectionStringName="LocationConnectionString"
pollTime="60000" />
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
In this case, the database specified in the connection string will be polled every 60000 milliseconds. To use this on a datasource control, you just need to add the SqlCacheDependency parameter and specify which database and table to invalidate on.
<asp:SqlDataSource
ID="SqlDataSource1"
EnableCaching="true"
SqlCacheDependency="teamowner:tblsearchalias"
SelectCommand="spGetLocations"
ConnectionString="<%$ ConnectionStrings:LocationConnectionString %>"
Runat="server" />
What's cool about this is that you don't necessarily have to use with a datasouce. You can also use this at the page or control level by specifying a compiler directive.
<%@ OutputCache SqlDependency="Teamowner:tblsearchalias"
Duration="6000" VaryByParam="none" %>
In this case the page's output cache will invalidate itself any time the tblsearchalias table is updated in the teamowner database.
In SQL Server 2005, polling is not necessary. Instead, whenever a table is modified it can notify IIS directly that the cache has been invalidated. This has been a lot. I'll cover even more on caching tomorrow.
Read the complete post at http://www.dotnettipoftheday.com/blog.aspx?id=158