ASP.NET

Conclusion: Caching and Page Performance

Caching is one of the easiest and most well-understood ways of wringing better performance out of an application. ASP.NET implements an easy-to-use application cache. The application cache stores any CLR object and is available at any time while processing a request. You can dig it out of the current context (the HttpContext), and it's also available as a member variable of System.Web.UI.Page.

Probably the most common way to use the cache is to store database query results to avoid round-trips to a database. Accessing memory is often orders of magnitude faster than hitting the database. In addition, you sidestep issues such as limited connection resources and database contention.

While you can effectively improve the performance of your application by simply putting items in the cache, ASP.NET's caching mechanism provides facilities for putting limits on the amount of time items remain cached. You may also set up dependencies between cached items and their physical data sources so that you may be alerted when items need to be reloaded into the cache.

Tutorial 14 Quick Reference

How to access the data cache

The data cache is available as

  1. the Cache property in the page

  2. the Cache property in the current HttpContext


Insert an item in the cache

Use the indexer notation to add an object and a value to the cache


How to insert an item in the cache with a dependency

Create a CacheDependency object and add the object to the cache using the overloaded insert method


How to insert an item in the cache with an expiration time

Create a DateTime object and add the object to the cache using the overloaded insert method


How to delete an item from the cache

Call the cache's Remove method


How to be notified that an item is being removed from the cache

Include a callback delegate when inserting an item in the cache