Caching Database Data with the SqlDataSource Control


Caching Database Data with the SqlDataSource Control

The easiest way to dramatically improve the performance of a database-driven website is through caching. Retrieving data from a database is one of the slowest operations that you can perform in a web page. Retrieving data from memory, on the other hand, is lightning fast. The SqlDataSource control makes it easy to cache data in your server's memory.

Caching is discussed in detail in Chapter 23, "Caching Application Pages and Data." In that chapter, you learn about all the different caching options supported by the SqlDataSource control. However, because it is so easy to cache data with the SqlDataSource control and caching has such a dramatic impact on performance, I wanted to provide you with a quick sample of how you can use the SqlDataSource control to cache data in this chapter.

The page in Listing 9.30 displays a list of movies that are cached in memory.

Listing 9.30. CacheSqlDataSource.aspx

[View full width]

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Protected Sub srcMovies_Selecting(ByVal sender As Object, ByVal e As System.Web.UI .WebControls.SqlDataSourceSelectingEventArgs)         lblMessage.Text = "Selecting data from database"     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <title>Cache SqlDataSource</title> </head> <body>     <form  runat="server">     <div>     <asp:Label                  EnableViewState="false"         Runat="server" />     <br /><br />     <asp:GridView                  DataSource         Runat="server" />     <asp:SqlDataSource                  EnableCaching="True"         CacheDuration="3600"         SelectCommand="SELECT * FROM Movies"         ConnectionString="<%$ ConnectionStrings:Movies %>"         OnSelecting="srcMovies_Selecting"         Runat="server" />     </div>     </form> </body> </html>

In Listing 9.30, two properties of the SqlDataSource control related to caching are set. First, the EnableCaching property is set to the value TRue. Next, the CacheDuration property is set to a value that represents 3,600 seconds (one hour). The movies are cached in memory for a maximum of one hour. If you don't supply a value for the CacheDuration property, the default value is Infinite.

Warning

It is important to understand that there is no guarantee that the SqlDataSource control will cache data for the amount of time specified by its CacheDuration property. Behind the scenes, the SqlDataSource control uses the Cache object for caching. This object supports scavenging. When memory resources become low, the Cache object automatically removes items from the cache.


Notice that the page in Listing 9.30 includes a srcMovies_Selecting() event handler. This handler is called only when the movies are retrieved from the database rather than from memory. In other words, you can use this event handler to detect when the movies are dropped from the cache (see Figure 9.15).

Figure 9.15. Caching the data represented by a SqlDataSource control.


The page in Listing 9.30 illustrates only one type of caching that you can use with the SqlDataSource control. In Chapter 23, you learn about all the advanced caching options supported by the SqlDataSource control. For example, by taking advantage of SQL cache dependencies, you can reload the cached data represented by a SqlDataSource control automatically when data in a database is changed. For more information, see the final section of Chapter 23, "Caching Application Pages and Data."




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net