Recipe 16.10. Caching Data Sources


Problem

You are using data sources in your application and you would like to cache the data source to improve the performance of your application.

Solution

Set the EnableCaching property of the data source to true and set the CacheDuration property to the desired expiration time:

 

dSource.EnableCaching = True dSource.CacheDuration = 5

dSource.EnableCaching = true; dSource.CacheDuration = 5;

Discussion

The data sources available in ASP.NET 2.0 (XmlDataSource, ObjectDataSource, and SqlDataSource) provide built-in support for caching. By using this built-in support, the data source will automatically cache the data it pulls from the data store. In other words, you do not need to handle the insertion and retrieval of the data source in the Cache explicitly, which simplifies the code in your application.

To use the built-in caching support in data sources, you need to set a couple of properties. First, you need to set the EnableCaching property to TRue to enable the caching of the data source.

Next, you need to define when the cached data expires by setting the CacheDuration property to the number of seconds after which the cached data is invalidated.

Optionally, you can set the CacheExpirationPolicy property to alter the way the CacheDuration time is used to cause the cache data to expire. Setting the CacheExpirationPolicy to DataSourceCacheExpiry.Absolute causes the cached data source to expire the number of seconds defined in the CacheDuration after the data source is first created. Setting the CacheExpirationPolicy to DataSourceCacheExpiry.Sliding causes the time to reset each time the data source is accessed, creating a sliding expiration window.

If you are using a SqlDataSource or an ObjectDataSource, you can set the SqlCacheDependency property to the database and table on which the data source is dependent to cause the cached data to expire when the data in the database change. If the data is dependent on more than one database and/or table, the dependencies can be specified in a semicolon-delimited list:

 

'Use the following if the data is dependent on a single database and table dSource.SqlCacheDependency = "ASPNetCookbook:Book" 'Use the following if the data is dependent on more than one database 'and/or table dSource.SqlCacheDependency = "ASPNetCookbook:Book;ASPNetCookbook:Problem"

// Use the following if the data is dependent on a single database and table dSource.SqlCacheDependency = "ASPNetCookbook:Book"; // Use the following if the data is dependent on more than one database // and/or table dSource.SqlCacheDependency = "ASPNetCookbook:Book;ASPNetCookbook:Problem";

Setting the SqlCacheDependency requires configuring your SQL Server database and web.config, as described in Recipe 16.5.


See Also

Recipe 16.5



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

Similar book on Amazon

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