Caching the Product Data


The tabs user control discussed in the previous section does not retrieve a list of categories from the Categories database table every time it is displayed. Instead, a cached copy of the data is used.

The same thing is true in the case of the Products database table. For performance reasons, when product information is displayed in the Products.aspx page, cached information from the Products database table is displayed.

All the caching in the ASP.NET store happens in a custom component located within the StoreComponents assembly. The source code for this component is contained in Listing 31.3.

Listing 31.3 CachedData Class
[View full width]
 '''''''''''''''''''''''''''''''''''''''''' ' Contains shared methods for retrieving ' cached database data '''''''''''''''''''''''''''''''''''''''''' Public Class CachedData   Public Shared Function GetCategories() As DataView     If HttpContext.Current.Cache( "Categories" ) Is Nothing Then       HttpContext.Current.Cache( "Categories" ) = GetCategoriesFromDB     End If     Return HttpContext.Current.Cache( "Categories" )   End Function   Public Shared Function GetCategoryDescription( intCategory As Integer ) As String     If HttpContext.Current.Cache( "Categories" ) Is Nothing Then       HttpContext.Current.Cache( "Categories" ) = GetCategoriesFromDB     End If     Return HttpContext.Current.Cache( "Categories" ).Item( intCategory ) ( "Description" )   End Function   Private Shared Function GetCategoriesFromDB() As DataView     Dim strConString As String     Dim conMyData As SqlConnection     Dim strSelect As String     Dim dadCategories As SqlDataAdapter     Dim dstCategories As DataSet     strConString = ConfigurationSettings.AppSettings( "connectionString" )     conMyData = New SqlConnection( strConString )     strSelect = "Select CategoryID,CategoryName,Description From Categories"     dadCategories = New SqlDataAdapter( strSelect, conMyData )     dstCategories = New DataSet     dadCategories.Fill( dstCategories, "Categories" )     Return  dstCategories.Tables( "Categories" ).DefaultView   End Function   Public Shared Function GetProducts( intCategoryIndex As Integer ) As DataView     Dim intCategoryID As Integer     Dim dvwProducts As DataView     dvwProducts = HttpContext.Current.Cache( "Products" )     If dvwProducts Is Nothing Then       dvwProducts = GetProductsFromDB       HttpContext.Current.Cache( "Products" ) = dvwProducts     End If     If HttpContext.Current.Cache( "Categories" ) Is Nothing Then       HttpContext.Current.Cache( "Categories" ) = GetCategoriesFromDB     End If     intCategoryID = HttpContext.Current.Cache( "Categories" ).Item( intCategoryIndex )( graphics/ccc.gif "CategoryID" )     dvwProducts.RowFilter = "CategoryID=" & intCategoryID     Return dvwProducts   End Function   Public Shared Function GetProductTemplate( intProductID As Integer ) As String     Dim intProductIndex As Integer     If HttpContext.Current.Cache( "Products" ) Is Nothing Then       HttpContext.Current.Cache( "Products" ) = GetProductsFromDB     End If     HttpContext.Current.Cache( "Products" ).RowFilter = ""     intProductIndex = HttpContext.Current.Cache( "Products" ).Find( intProductID )     Return HttpContext.Current.Cache( "Products" ).Item( intProductIndex )( "Template" )   End Function   Public Shared Function GetProductRow( intProductID As Integer ) As DataRowView     Dim intProductIndex As Integer     If HttpContext.Current.Cache( "Products" ) Is Nothing Then       HttpContext.Current.Cache( "Products" ) = GetProductsFromDB     End If     HttpContext.Current.Cache( "Products" ).RowFilter = ""     intProductIndex = HttpContext.Current.Cache( "Products" ).Find( intProductID )     Return HttpContext.Current.Cache( "Products" ).Item( intProductIndex )   End Function   Private Shared Function GetProductsFromDB() As DataView     Dim strConString As String     Dim conMyData As SqlConnection     Dim strSelect As String     Dim dadProducts As SqlDataAdapter     Dim dstProducts As DataSet     strConString = ConfigurationSettings.AppSettings( "connectionString" )     conMyData = New SqlConnection( strConString )     strSelect = "Select * From Products"     dadProducts = New SqlDataAdapter( strSelect, conMyData )     dstProducts = New DataSet     dadProducts.Fill( dstProducts, "Products" )     dstProducts.Tables( "Products" ).DefaultView.Sort = "ProductID"     Return dstProducts.Tables( "Products" ).DefaultView   End Function End Class 

The C# version of this code can be found on the CD-ROM.

Most of the methods in the CachedData class work by attempting to retrieve data from the cache first. If that doesn't work, the information is retrieved from the original database and cached for the next request.

For example, if the GetCategories() method can't retrieve the categories from the Cache , the method immediately calls the GetCategoriesFromDb function.



ASP.NET Unleashed
ASP.NET 4 Unleashed
ISBN: 0672331128
EAN: 2147483647
Year: 2003
Pages: 263

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