Section 4.5. Cache the Data Source Control


4.5. Cache the Data Source Control


Note: To improve the performance of your application, you should cache the data source control if you are using one.

Performing database access is a time-consuming operation; therefore, it is important that you reduce the number of times you connect to the database. The SqlDataSource control (as well as the AccessDataSource control) supports caching of data. Caching of the data source controls is useful in cases when your data does not change oftenfor example, when you have pages displaying product listings and information.

4.5.1. How do I do that?

To see how to enable caching of data source controls in your application, you will build an application in this lab that uses a SqlDataSource control to retrieve rows from the authors table in the pubs database and then uses the GridView control to display them. The SqlDataSource control will be cached for 60 seconds before the cache is invalidated.


Note: In general, the more static your data, the longer the time you should cache your data.

  1. Launch Visual Studio 2005 and create a new web site project. Name the project C:\ASPNET20\chap04-DataSourceCache.

  2. Drag and drop a GridView control onto the default Web Form and configure it to use a SqlDataSource control to connect to the pubs database in SQL Server 2005 Express (see the lab Section 4.1 for detailed instructions on how to do this). In particular, the au_id, au_fname, and au_lname fields are retrieved. The Source View of the GridView and SqlDataSource control looks like Example 4-4.

    Example 4-4. Source View of the GridView and SqlDataSource controls
    <asp:GridView  runat="server"       DataSource       AutoGenerateColumns="False"      DataKeyNames="au_id">    <Columns>       <asp:BoundField ReadOnly="True" HeaderText="au_id"             DataField="au_id" SortExpression="au_id">       </asp:BoundField>       <asp:BoundField HeaderText="au_lname"             DataField="au_lname" SortExpression="au_lname">       </asp:BoundField>       <asp:BoundField HeaderText="au_fname"             DataField="au_fname" SortExpression="au_fname">       </asp:BoundField>    </Columns> </asp:GridView> <asp:SqlDataSource  runat="server"       SelectCommand="SELECT [au_id], [au_lname],                      [au_fname] FROM [authors]"      ConnectionString="<%$ ConnectionStrings:                            pubsConnectionString %>"> </asp:SqlDataSource>

  3. To enable caching of the SqlDataSource control, set the CacheDuration (in seconds) and EnableCaching attributes, as shown in the following code snippet:

    <asp:SqlDataSource  runat="server"       SelectCommand="SELECT [au_id], [au_lname],                      [au_fname] FROM [authors]"      ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"      CacheDuration="60"       EnableCaching="True"      CacheExpirationPolicy="Absolute" > </asp:SqlDataSource>

  4. Press F5 to test the application. To verify that the data in the SqlDataSource control is cached, modify one of the rows in the authors table and refresh the page. You will notice that the data in the GridView control is not updated until approximately one minute (60 seconds) later.

The default cache expiration policy is that of absolute, which means that the data cached by the data source control will expire x seconds (as specified in the CacheDuration attribute) after the data source has been loaded.

4.5.2. What about...

...sliding cache policy?

In the sliding cache policy, the cache will expire if a request is not made within a specified duration. For example, the following code specifies that the cache will have a sliding duration of one minute. If a request is made 59 seconds after the cache is accessed, the validity of the cache would be reset to another minute. Sliding expiration policy is useful whenever you have a large number of items to cache, since this policy enables you to keep only the most frequently accessed items in memory.

<asp:SqlDataSource  runat="server"       SelectCommand="SELECT [au_id], [au_lname],                      [au_fname] FROM [authors]"      ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"      CacheDuration="60"       EnableCaching="True"      CacheExpirationPolicy="Sliding" > </asp:SqlDataSource>

4.5.3. Where can I learn more?

For a good discussion on implementing page caching in ASP.NET using absolute expiration, check out the article at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpPageCacheInASP.asp.

You will also learn how to cache fragments of a page in Chapter 6.



ASP. NET 2.0(c) A Developer's Notebook 2005
ASP. NET 2.0(c) A Developer's Notebook 2005
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 104

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