Compartilhar via


What's New in ASP.NET Caching

ASP.NET version 2.0 retains all of the caching functionality of ASP.NET version 1.1, but adds new features and enhances existing features. New features include cache profiles, custom cache dependencies, a SQL cache dependency, and creating dynamic content in cached pages (post-cache substitution). Feature enhancements include a more powerful partial-page (control) caching model, enhanced cache configuration, and improvements to the output cache directive.

New Cache Features

Cache Profiles

Cache profiles allow you to create cache settings in an application's Web.config file and then reference those settings on individual pages. This allows you to apply cache settings to many pages at once. For example, you can define a cache profile named DailyUpdate that sets a page's cache duration to one day. You can then configure individual pages to use the DailyUpdate cache profile, and the cache duration of those pages will be one day. If you change the DailyUpdate cache profile to not use caching, the pages will stop being cached. For more information see Cache Configuration in ASP.NET.

Custom Cache Dependencies

In ASP.NET 2.0 you can create your own custom cache dependency based on application-specific conditions. To create a custom cache dependency, you create a class that inherits from CacheDependency and in your custom class implement your own dependency methods. For example, you can create a dependency that polls a Web service for data; when the data changes, you can invalidate your cache data. To learn about adding items to the cache by specifying a dependency, see How to: Add Items to the Cache.

SqlCacheDependency

ASP.NET 2.0 introduces the SqlCacheDependency class, which allows you to configure an item in the cache to have a dependency on a table or row in a Microsoft SQL Server database. When a change occurs in the table or in a specific row, the cached item that has a dependency is invalidated and therefore removed from the cache. ASP.NET 2.0 allows you to set a dependency on a table in SQL Server 7.0, SQL Server 2000, and SQL Server 2005. When using SQL Server 2005, you can also set a dependency on a specific record. For more information, see Caching in ASP.NET with the SqlCacheDependency Class.

Post-Cache Substitution

ASP.NET 2.0 now supports post-cache substitution, which allows you to configure a section in a page as non-cacheable. Therefore, although the page is cached, parts of it will be re-processed when the page is requested again. For example, you can create a cached page with mostly static content but that displays the user's name in a Label control. Without post-cache substitution, the user name would remain the same for all requests. With post-cache substitution, you can mark the page as cacheable and put the Label control inside another control marked as not cacheable. The user name will then refresh each time the page is requested. For more information see Caching Portions of an ASP.NET Page.

Caching Enhancements

Control Caching

In ASP.NET 1.1, you configure user control caching declaratively by setting parameters in the @ Control directive. In ASP.NET 2.0, you can configure user control cache settings at run time with the CachePolicy object. The CachePolicy object allows you to work with user control caching the same way that you can work programmatically with page output caching. For more information, see Caching Portions of an ASP.NET Page.

Cache Configuration Enhancements

In addition to cache profiles, ASP.NET 2.0 introduces new cache configuration settings that you can specify in an application's Web.config file. These settings allow more control over the cache, such as how much memory is used and cache scavenging behavior. For more information see Cache Configuration in ASP.NET.

Output Cache Directive Improvements

ASP.NET 2.0 includes new @ OutputCache directive options and includes enhancements to existing ones. The new features and enhancements enable declarative control over output cache features that were previously available only programmatically using the HttpCachePolicy class. For example, you can now declaratively set the Duration attribute and the NoStore attribute in the page @ OutputCache directive. For more information, see Setting the Cacheability of a Page.

See Also

Tasks

How to: Add Items to the Cache

Concepts

ASP.NET Caching Overview
Caching ASP.NET Pages
Caching Application Data
Caching in ASP.NET with the SqlCacheDependency Class