|
||||||||||||
|
Query Notification and ASPNET's Cache
In addition to the ADO.NET-based query notification, there is an entirely alternate set of tools built into ASP.NET 2.0 that build database cache invalidation into your Web site. The effect is, in some ways, similar to what you achieve with the middle tier caching samples above. This functionality is something that was done previously through a great deal of trickery using database polling, as detailed in Jeff Prosise's April 2003 Wicked Code column. Figure 2 shows the new queue and service created to be used with the QueryNotificationRequest
In the preceding code, creating the object is similar to creating the SqlDependency object in ADO.NET. Then you tie the notification into the page's cache with Cache.AddDependency, setting the familiar caching properties. In this case, the cache is valid until it expires, which is either within 30 seconds or when a notification comes in, whichever happens first. When the page cache is invalidated, the next request for this page creates the page from scratch, forcing the data access to happen again.Remember that you don't see SqlDependency.Start here because it is in the Global.asax. Invalidating the cache with the <OutputCache> in the page's markup is even simpler. By using the new SqlDependency parameter of <OutputCache> and setting its value to CommandNotification, you are plugged right in to query notifications. Here is an example with only the minimal parameters being used.
Any valid queries called from this page have a notification request registered in the Service Broker. The data access does not necessarily have to be done in the page's client-side or server-side code code-behind). You can call into another class or even another assembly for the data access and it will still use query notification requested by the page.The SqlDependency parameter can also take other values, but only the CommandNotification value, used with SQL Server 2005, gives you the Service Broker Query Notification. ASP.NET 2.0 is also wired up to give cache invalidation from SQL 7 and SQL 2000. One way to do this is by using the value database-name:table-name (for example, pubs:authors) as the SqlDependency value. The effect is that a polling type of query is performed. There are more pieces necessary to make this work, but as it is outside of the scope of this article, I will point you to the "Implementing SQL Server 7/2000 Dependency Caching" explanation and example in the Visual Web Developer Guide on the ASP.NET Web site. Query Notification in Transactions Query Notifications do work inside of transactions. However, you will only get one notification per transaction regardless of the number of changes made to the database within that transaction. SQL Server Express Query Notifications can be used with SSE but the SSE database must be a named instance. Named instance is a setup option. Additionally, the database that you want to use must be an attached database. Although Query Notification is definitely one of the big new features in .NET 2.0, it still plays underdog to other exciting features such as generics or the UI magic in ASP.NET. Whether you use it to prevent the incessant re-querying for a drop down list of the 50 U.S. states or to manage the Web-based updates of millions of client computers, it can help you reduce overhead on your resources. In its simplest usethrough the ASP.NET OutputCache directiveor by building a complex caching mechanism in your Web application's middle tier or Web service, Query Notification can be a powerful ally in creating extensible, responsive applications.
|
||||||||||||
Julia Lerman is an independent consultant who has been designing and writing software applications for 20 years. She lives in Vermont where she runs the Vermont.NET User Group. Julia is well known in the .NET community as an INETA Board member, .NET MVP, ASPInsider, conference speaker, and prolific blogger. You can read Julia's blog at thedatafarm.com/blog.
| ||||||||||||
|