ASP.NET Caching Overview

An application can often increase performance by storing data in memory that is accessed frequently and that requires significant processing time to create. For example, if your application processes large amounts of data using complex logic and then returns the data as a report accessed frequently by users, it is efficient to avoid re-creating the report every time that a user requests it. Similarly, if your application includes a page that processes complex data but that is updated only infrequently, it is inefficient for the server to re-create that page on every request.


Application Cache

The application cache provides a programmatic way for you to store arbitrary data in memory using key/value pairs. Using the application cache is like using application state. However, unlike application state, the data in the application cache is volatile. This means it is not stored in memory for the life of the application. The advantage of using the application cache is that ASP.NET manages the cache and removes items when they expire or become invalidated, or when memory runs low. You can also configure application caching to notify your application when an item is removed. For more information, see Caching Application Data.

The pattern when using the application cache is to determine whether an item exists in the cache any time you access an item, and if it does, to use it. If the item does not exist, you can re-create the item and then place it back in the cache. This pattern ensures that you always have the latest data in the cache.

For more information, see Caching in .NET Framework Applications and How to: Retrieve Values of Cached Items.

Page Output Cache

A typical kind of caching for server applications is output caching. Output caching enables you to store rendered HTML. The stored HTML is served in response to subsequent requests for the same page. You can use output caching to cache a whole Web page or just the output of an ASP.NET control. Output caching enables you to do the following:

  • Configure ASP.NET to cache a particular output cache entry for a specific period.

  • Cache a different version of the content based on the browser type or user-language preferences of the clients visiting your application.

  • Cache a mobile version of a page that differs from a version that is optimized for a desktop browser.

  • Configure ASP.NET to evict a cache entries based on an external event.

Output caching is extensible. You can use a custom output cache provider that can store data on any data storage device.

The page output cache stores the contents of a processed ASP.NET page in memory. This lets ASP.NET send a page response to a client without going through the page processing life cycle again. Page output caching is especially useful for pages that do not change often but that require significant processing to create. For example, if you are creating a high-traffic Web page to display data that is not frequently updated, page output caching can dramatically increase the performance of that page. Page caching can be configured individually for each page, or you can create cache profiles in the Web.config file, which allow you to define caching settings once and then use those settings with multiple pages.

Page output caching provides two models for page caching: full-page caching and partial-page caching. Full-page caching persists the complete contents of a page and uses the cached page content to fulfill client requests. Partial-page caching persists specified portions of a page and lets other portions of the page be created dynamically. For more information, see Caching Portions of an ASP.NET Page.

Partial-page caching can work in two ways: control caching and post-cache substitution. Control caching, also sometimes referred to as fragment caching, lets you cache parts of the page output by including the information in a user control and then marking the user control as cacheable. This enables specific content throughout a page to be cached, while the overall page is not cached and is therefore re-created on each request. For example, if you create a page that displays largely dynamic content, such as stock information, but has sections that are static, such as weekly summaries, you can place the static sections in user controls and specify that they are cached.


Caching Pages Based on Request Parameters


Extensible Output Caching

ASP.NET adds extensibility to output caching that enables you to configure one or more custom output-cache providers. Output-cache providers can use any storage mechanism to persist HTML content. These storage options can include local or remote disks, cloud storage, and distributed cache engines.

Output-cache provider extensibility in ASP.NET lets you design more aggressive and more intelligent output-caching strategies for Web sites. For example, you can create an output-cache provider that caches the "Top 10" pages of a site in memory, while caching pages that get lower traffic on disk. Alternatively, you can cache every vary-by combination for a rendered page, but use a distributed cache so that the memory consumption is offloaded from front-end Web servers.

You create a custom output-cache provider as a class that derives from the OutputCacheProvider type. You can then configure the provider in the Web.config file by using the new providers subsection of the outputCache element, as shown in the following example:

  <outputCache defaultProvider="AspNetInternalProvider">
      <add name="DiskCache" 
          type="Test.OutputCacheEx.DiskOutputCacheProvider, DiskCacheProvider"/>

For more information and for examples that show how to configure the output cache, see outputCache Element for caching (ASP.NET Settings Schema). For more information about the classes that support caching, see the documentation for the OutputCache and OutputCacheProvider classes.

By default, all HTTP responses, rendered pages, and controls use the in-memory output cache that is illustrated in the previous example (where the defaultProvider attribute is set to AspNetInternalProvider). You can change the default output-cache provider that is used for a Web application by specifying a different provider name for defaultProvider.

You can also select different output-cache providers for individual control and for individual requests. The easiest way to choose a different output-cache provider for different Web user controls is to do so by declaratively using the providerName attribute in an @ Page or @ Control directive, as shown in the following example:

    providerName="DiskCache" %>

To specify a different output cache provider for an HTTP request, you override the new GetOutputCacheProviderName method in the Global.asax file to programmatically specify which provider to use for a specific request. For more information, see GetOutputCacheProviderName.

Automatic Data Removal



  • Because the item in the cache has expired.

  • Because the item's dependency changes.



Scavenging is the process of deleting items from the cache when memory is scarce. Items are removed when they have not been accessed in some time or when items are marked as low priority when they are added to the cache. ASP.NET uses the CacheItemPriority object to determine which items to scavenge first. For more information, see How to: Add Items to the Cache.



Expiration Type


Sliding expiration


Absolute expiration




ASP.NET caching supports the dependencies described in the following table.



Key dependency


File dependency


SQL dependency


Aggregate dependency


Custom dependency


Application Cache Item Removal Notification


See Also


How to: Cache Page Output with File Dependencies


Caching ASP.NET Pages

Caching Application Data

Caching in ASP.NET with the SqlCacheDependency Class