When Output Caching Makes Sense

As with other caching techniques, one of the most important strategies is to turn on output caching for those pages that are accessed frequently and expensive to generate. Also, be sure to cache only those pages that don't change frequently (otherwise, you may be better off simply not using output caching).

For example, pages full of controls that render a great deal of HTML are probably expensive. Imagine a page including a DataGrid displaying an employee directory. This is a perfect candidate for caching due to several reasons. First, a database hit (or even an in-memory cache hit) Is required. Second, a DataGrid is pretty expensive to render-especially if it needs to figure out the schema of the employee directory table on the fly. Finally, an employee directory probably doesn't change very often. By caching it once, you can avoid spending a great deal of unnecessary cycles.

A related issue here is to be careful when typing asterisks into the output caching parameters such as VaryByParam. Using VaryByParam=* tells ASP.NET to generate a new page for every single request in which any query string parameter has changed. That's almost the same as not caching altogether-with the added cost of the memory consumed by the output cache. However, this may make sense for Web sites with limited audiences where the parameter variance between requests remains limited.

In addition, be wary of how caching might affect the appearance of your page on different browsers. Much of the time, content will appear the same regardless of the browser. However, if you cache some content that depends upon a specific browser feature (such as Dynamic HTML), clients whose browsers don't understand the feature may see some very weird behavior in the browser.

Tuning the behavior of the output cache is also important. Effective caching is always a matter of balance. While you can potentially speed up your site by employing output caching, the cost is memory consumption. Using instrumentation tools can help you balance performance against cost.

Finally, User Controls often represent a prime output caching opportunity-especially if they don't change frequently. Wrapping the portion of a page that doesn't change in an output cached User Control will usually enhance the perceived performance of your application at a minimal cost because only the User Control content is cached.