One of the most frequently requested features for the ASP.NET Cache is the ability to invalidate cached items from the database. This capability, along with some changes to the underlying plumbing of the ASP.NET Cache, has been added in ASP.NET 2.0. However, the Technology Preview release does not contain all the planned additions to the caching features of ASP.NET. Microsoft still intends to add some additional capabilities, but they won't be available until the beta release. The caching features in ASP.NET are specifically designed to increase throughput and decrease load on the server by keeping frequently accessed content in memory versus constantly running code. There are two primary uses of the Cache .
Behind the scenes, pages marked for output caching are stored and retrieved through the Cache API. The Cache itself is simply a hashtable with enhanced capabilities.
While the Cache and its dependency features allowed complex applications to be built, the ASP.NET team soon realized that most cached data came from a database and there was not a dependency that allowed for invalidation when data in the database changed. Database change dependency is now possible in the ASP.NET 2.0 Cache . Listing 11.1 demonstrates how this is used in a pageusing the new sqldependency attribute of the <%@ OutputCache %> directive. Listing 11.1 SQL ServerBased Cache Dependency <%@ Page Language="VB" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <%@ OutputCache duration="5555" varybyparam="none" sqldependency="Northwind:Products" %> <h1>Last update: <%=DateTime.Now.ToString("r")%></h1> <hr> <form runat="server"> <asp:gridview id="GridView1" datasourceid="SqlDataSource1" runat="server" /> <asp:sqldatasource id="SqlDataSource1" runat="server" providername="System.Data.OleDb" selectcommand="SELECT * FROM dbo.[Products]" connectionstring="provider=sqloledb; database=northwind; uid=sa;pwd=00password" /> </form> Database cache invalidation allows for the removal of an item from the Cache when data stored in the database changes. ASP.NET 2.0 adds support for database cache invalidation for Microsoft SQL Server 7, Microsoft SQL Server 2000, and the next version of Microsoft SQL Server (code-named "Yukon"). Although support for these three databases is built into ASP.NET, there are some significant differences.
The level of granularity between the databases is very relative. For example, if you output cache a page for each product in the Products table, you would output cache 77 pages. If you were using SQL Server 7 or SQL Server 2000, each of the 77 pages would be invalidated if the Products table were updated. However, with "Yukon" you can be more selective about how items are invalidated; you can invalidate only output-cached pages that changed (e.g., the page displaying information for the product with ID 35). We are not going to focus on the "Yukon" caching capabilities in this chapter but will instead examine how we enable database cache invalidation from Microsoft SQL Server 7 and 2000. In order to use database cache invalidation on these databases, we first need to enable it. |