Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

Cache Configuration in ASP.NET

ASP.NET provides many options for configuring page output caching and the cache API. You use the page output cache to cache page responses after they have been processed. You use the cache API to programmatically cache application data. For more information, see ASP.NET Caching Overview.

You can configure page output caching in these places:

  • Configuration files   You can configure page output cache settings in any configuration file in the application configuration hierarchy, including the Machine.config file (to make settings for all Web applications on the computer) and your application-specific Web.config file (to make settings for a single application).

  • Individual pages   You can set caching options in individual pages either declaratively or programmatically. You can also apply cache profiles created in the configuration file to individual pages.

  • User controls   You can set caching in individual user controls either declaratively or programmatically. This is an easy way to cache content within a page that is otherwise not cached.

Web.config Cache Configuration Settings

There are two top-level configuration sections for the page output cache in the Web.config file: the OutputCacheSection and the OutputCacheSettingsSection.

The OutputCacheSection section is used to configure application-scope settings, such as whether page output caching is enabled or disabled. For example, you can disable page output caching for the entire application by adding enableOutputCache="false" to the OutputCacheSection in your Web.config file. Settings in the configuration file take precedence over cache settings in individual pages, so the example setting means that output cache will not be used.

The OutputCacheSettingsSection is used to configure profiles and dependencies that can be used by individual pages. For example, the following code creates an OutputCacheProfile named CacheProfile1 that will cache the implementing page for 60 seconds:

<outputCacheSettings>
  <outputCacheProfiles>
    <add name="CacheProfile1" duration="60" />
  </outputCacheProfiles>
</outputCacheSettings>

Machine.config Cache Configuration Settings

The configuration sections for the Machine.config file are the same as for the Web.config file, except that you can lock configuration settings in the Machine.config file so that they cannot be overridden by individual applications at any level. This might be necessary in a shared hosting scenario in which the hoster does not want individual applications modifying the cache configuration. For more information see How to: Lock ASP.NET Configuration Settings.

Page Cache Configuration Settings

You can configure caching in individual pages by applying cache profiles that have been defined in a configuration file. Alternatively, you can configure individual cache properties in the @ OutputCache directive or by setting attributes in the page's class definition. For more information see @ OutputCache and Setting the Cacheability of a Page.

User Control Cache Configuration Settings

You can configure user control caching by setting the @ OutputCache directive in the user control file or by setting the PartialCachingAttribute attribute in the control's class definition. For more information, see Caching Portions of an ASP.NET Page.

You can configure the application's cache API in your Web.config file. As with the page output cache, application hosters can set configuration properties in the Machine.config file and lock cache configuration settings for all applications. The application cache API is configured in the CacheSection. For example, you can disable item expiration with the following configuration element:

<cache disableExpiration="true" />

You can also specify other application cache API configuration settings by assigning values to attributes such as DisableExpiration and DisableMemoryCollection within the configuration file's CacheSection.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.