ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
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:
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:
See Also
Recipe 16.5 |