Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
The ASP.NET cache offers a means for achieving great improvements in performance. The ability to cache output and data for use in subsequent requests that would otherwise have to be created again can result in those later requests running much faster. Caching is covered in detail in Chapter 6, so we’ll just review here which cache options can be leveraged to improve performance.
Caching Page Output
When a page is executed, the entire output can be stored in the cache. Subsequent requests for the same page can be handled by the ASP.NET run time by retrieving the output from the cache and returning it. The page itself is not executed again if a match is found in the cache. To get the output from a page to be cached, use the OutputCache directive at the top of the page. You can also utilize the cache by writing code directly as detailed in Chapter 6.
The OutputCache directive requires a duration attribute. An item is removed from the cache when the number of seconds specified in the duration attribute passes. Use the OutputCache aggressively to increase performance by eliminating page executions. Even caching page output for just a few minutes can have a dramatic impact on performance.
Another attribute required by the OutputCache directive is VaryByParam. This attribute gives us more control over how ASP.NET looks for items in the cache. If VaryByParam is set to none, requests with post data or query string data will not be retrieved from the cache. More often, the output is a function of some specific variable in the post data or query string. The VaryByParam attribute should then be set to a semicolon-separated list of the variables to use in storing and retrieving the page output.
In some situations, parts of a particular page should be executed on each request but other parts of that page should be cached. The cacheable part of the page can be placed in a separate user control with the OutputCache directive specified. This part of the page is then cached even though the rest of the page must be executed for each request. This is called fragment caching and can also be a significant factor in improving page execution time.
Caching Objects
The ASP.NET cache provides a rich set of functionality for storing data and objects. It supports setting dependencies for purging an item from the cache as well as for setting priorities to free memory and setting the time when cached output expires. Objects that retain handles and resources, like an open connection to a database, should not be stored in the cache to avoid having other requests deprived of the resources necessary to complete.
Tip | Store application-wide data in the cache object instead of the application object. The cache object does not have the same locking semantics and provides more flexibility. |
Consider the cacheability of pages, user controls, objects, and data as the application is designed and developed. Using the cache effectively can have more benefit to application performance than many other performance-coding techniques. After all, code that doesn’t have to execute again is much faster than the most optimized running code.
Категории