Chapter 18. Caching and Performance
There are several ways to achieve higher performance and better scalability in ASP.NET. One way is through the use of caching. Caching is a technique whereby frequently requested data is stored in a quickly accessible location, so that the next time the same data is requested, it can be quickly fetched from the cache location rather than regenerated by the application.
This can result in a significant performance boost, especially for dynamically generated content (such as ASP.NET web pages and components), and in cases where the data underlying the response is expensive to gather (such as database queries).
Most web browsers cache pages received so that if the same page is requested again, it does not have to be sent over the Internet, but rather is retrieved directly from the local hard drive. Most operating systems also employ caching of some sort to store frequently requested data in memory, rather than require additional hard drive reads.
The only caching this chapter will be concerned with is server-side caching performed by the .NET Framework.
In some respects, caching is similar to the storage of state objects. (See Chapter 6 for a complete discussion of state in ASP.NET.) In both cases, data is saved for use across multiple requests, and, in the case of application state, across multiple sessions. However, there the similarity ends. With state objects, the developer explicitly saves a particular piece of data in a particular place, intending to be able to retrieve that data at any time later in the session or in other sessions. The data stored in state objects will last as long as the session or application, and will not be lost until the developer specifies it is to be removed or replaced. In short, the developer can count on the data in a state object being available.
In contrast, cached data is non-deterministic. You cannot assume that any piece of data you are looking for will be in the cache. As will be shown later in this chapter, whenever your program attempts to retrieve data from the cache, it must test to see if the data is there, and make provisions to retrieve the data elsewhere if it is not present in the cache. The data may be missing because its lifetime expired, because the application needed to free memory for other purposes, or simply because the cache was never populated.