Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Advantage of ASP.NET 2.0's Data Caching Techniques, Part 2 : Page 3

The second half of this two-part article looks at ASP.NET 2.0's new SQL cache invalidation support, and shows you how to use the Caching API to speed up your ASP.NET applications.


advertisement
Using a SqlDependency with a Data Source Control
After configuring a SQL cache dependency, you can use it to enable change notification for a data source control (or in an OutputCache directive, as you'll see shortly). The following declaration of a SqlDataSource control uses the nwind-cache entry:

<asp:SqlDataSource id="ds1" runat="server" ConnectionString="<%$ConnectionStrings:nwind%>" SelectCommand="SELECT OrderID, OrderDate FROM Orders" DataSourceMode="DataSet" EnableCaching="True" SqlCacheDependency="nwind-cache:Orders"> </asp:SqlDataSource>

If you want to base the cached data invalidation on more than one SQL cache dependency, you separate them with semicolons, for example:

SqlDependency="nwind-cache:Orders;nwind-cache:Products"

 
Figure 5. Using a SQL Server 2000Cache Dependency with a Data Source Control: The main page caches the data indefinitely, but using the update page to update the Order Date causes the cache to refresh. Subsequent queries to the main page show the updated values.
The sample page data-source-sql2000.aspx demonstrates how to use a data source control with a SQL cache dependency, as described above. Figure 5 shows the example page, along with the new window that you can open from it to perform updates in the source database. The EnableCaching="True" attribute in the data source control within the main page causes it to cache the rowset indefinitely (in other words, it doesn't specify a CacheDuration attribute), so it shows the same rowset with each page refresh, without fetching it from the database again. However, the cached rowset data is invalidated automatically when you update the database using the button in the other browser window; after updating, the next refresh of the main page will shown the new value.

Using a SqlDependency in an OutputCache Directive
You can use the same SQL cache dependency described in the previous section for a data source control in an OutputCache directive in an ASP.NET page. The same syntax applies for using multiple dependencies:



<%@OutputCache Duration="#seconds" VaryByParam="None" SqlDependency="dependency-name:table-name" />

Or:

<%@OutputCache Duration="#seconds" VaryByParam="None" SqlDependency="dependency-name:table-name; dependency-name:table-name" />

 
Figure 6. Using a SQL Server 2000 Cache Dependency in an OutputCache Directive: This example is equivalent to the one shown in Figure 5, but this time the main page contains a Duration attribute.
Figure 6 shows an example equivalent to the one in Figure 5, but this time using the SqlDependency in an OutputCache directive within the page, rather than in the data source control. The only visible difference is that this example displays the time at which the page was executed, so that you can confirm that the output is cached until the values in the source database table are changed by the other window.

You can also create database cache dependencies based on SQL Server 7.0 or SQL Server 2000 directly in code, using the SqlCacheDependency class added to the .NET Framework in version 2.0. Use an OutputCache directive in the page that specifies only the caching duration:

<%@OutputCache Duration="#seconds" VaryByParam="None" />

Then, in the Page_Load event (or wherever required), create a new SqlCacheDependency instance and set this dependency on the output cache:

' database defined in Web.config as "nwind-cache", ' dependency on Orders table Dim dep As New SqlCacheDependency("nwind-cache", "Orders") Response.AddCacheDependency(dep)

Using SQL Server 2005 Change Notifications
In SQL Server 2005, the Broker Service provides change notification automatically. You can create a dependency on a Command instance that generates the data for your page and attach it to the output cache in a similar way:

' for a SqlCommand instance named myCommand, ' previously created in code Dim dep As New SqlCacheDependency(myCommand) Response.AddCacheDependency(dep)

You can also take advantage of dependencies in SQL Server 2005 using the special cache identifier value "CommandNotification" in your data source controls:

<asp:SqlDataSource id="identifier" runat="server" ... EnableCaching="True" SqlCacheDependency="CommandNotification" />

Or, in an OutputCache directive:

<%@OutputCache Duration="#seconds" VaryByParam="None" SqlDependency="CommandNotification" />

Author's Note: The current Beta 2 release of SQL Server 2005, with ASP.NET 2.0 Beta 1, requires Windows XP SP2 or Windows Server 2003 SP1. Also, with common combinations of the Beta Framework and SQL Server 2005, the "CommandNotification" identifier is unreliable at the moment. However, the sample files include a page you can experiment with that's equivalent to the example shown in Figure 5, except that it uses a SQL cache dependency with SQL Server 2005.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap