Caching For Performance
What's New in 2.0
This section discusses these and other caching features in ASP.NET 2.0.
Caching is a technique widely used in computing to increase performance by keeping
frequently accessed or expensive data in memory. In the context of a Web application,
caching is used to retain pages or data across HTTP requests and reuse them without
the expense of recreating them.
ASP.NET has several kinds of caching that can be used by Web applications:
Output caching is useful when the contents of an entire page can be cached. On a
heavily accessed site, caching frequently accessed pages for even a minute at a
time can result in substantial throughput gains. While a page is cached by the output
cache, subsequent requests for that page are served from the output page without
executing the code that created it.
Sometimes it is not practical to cache an entire page - perhaps portions of the
page must be created or customized for each request. In this case, it is often worthwhile
to identify objects or data that are expensive to construct and are eligible for
caching. Once these items are identified, they can be created once and then cached
for some period of time. Additionally, fragment caching can be used to cache regions
of a page's output.
Choosing the time to cache an item can be an interesting decision. For some items,
the data might be refreshed at regular intervals or the data is valid for a certain
amount of time. In that case, the cache items can be given an expiration policy
that causes them to be removed from the cache when they have expired. Code that
accesses the cache item simply checks for the absence of the item and recreates
it, if necessary.
The ASP.NET cache supports file and cache key dependencies, allowing developers
to make a cache item dependent on an external file or another cache item. This technique
can be used to invalidate items when their underlying data source changes.
ASP.Net 2.0 includes some new features to help with cache configuration. Cache profiles
enable you to configure cache profiles in the configuration system, and then use
those profiles on pages. This enables changes to caching for sets of pages to be
made on a global basis. More options for customizing the cache performance have
also been added.