Programming Microsoft ASP.NET 4 - Dino Esposito [397]
IIS Kernel Caching
In IIS 6 and newer versions, you have the possibility of telling IIS to cache the page output for you without involving the ASP.NET runtime. This feature has tremendous potential and can dramatically improve the performance of a Web server, as long as enough of the content being served is cacheable.
The great news for ASP.NET developers is that no code changes are required to benefit from kernel caching, except for the @OutputCache directive. You enable kernel caching administratively from within the IIS Manager. When both output caching and IIS kernel caching are enabled, a kernel-level driver in IIS intercepts any incoming requests and, if the response was previously cached, it serves them by directly flushing the cached data from wherever the output provider in use had stored it. As this happens at the kernel level, there’s no need to make any context switch to user mode, which results in a remarkable performance improvement—about one tenth of the time it would take in classic user mode.
On a high-volume Web site, an output cache duration of only a few seconds can make a huge difference for the overall throughput of a Web server. There’s more to know about kernel caching, though. First and foremost, kernel caching is available only for pages requested through a GET verb. No kernel caching is possible on postbacks. Furthermore, pages with VaryByParam and VaryByHeader attributes set are also not stored in the kernel cache. Finally, note that ASP.NET Request/Cache performance counters will not be updated for pages served by the kernel cache.
Adding a Database Dependency to Page Output
The SqlDependency attribute is the @OutputCache directive’s interface to the SqlCacheDependency class that we discussed earlier. When the SqlDependency attribute is set to a Database:Table string, a SQL Server cache dependency object is created. When the dependency is broken, the page output is invalidated and the next request will be served by pushing the request through the pipeline as usual. The output generated will be cached again.
<% @OutputCache Duration="15" VaryByParam="none"
SqlDependency="Northwind:Employees" %>
A page that contains this code snippet has its output cached for 15 seconds or until a record changes in the Employees table in the Northwind database. Note that the Northwind string here is not the name of a database—it’s the name of an entry in the Important The more you move toward using layers in your ASP.NET solution (as discussed in Chapter 14), the less you need features like SQL dependency, which build their effectiveness on top of a tight form of coupling between ASP.NET pages and database details. The need of having a dependency between ASP.NET pages and stored content remains, but you can handle that using dependencies on cached items. This is one of the extra features that commercial distributed caches offer over most open-source solutions. The HttpCachePolicy Class Properties of the HttpCachePolicy Class Table 18-10. HttpCachePolicy Class Properties Property Description VaryByHeaders Gets an object of type HttpCacheVaryByHeaders, representing the list of all HTTP headers that will be used to vary cache output VaryByParams Gets
The HttpCachePolicy class is a programming interface alternative to using the @OutputCache directive. It provides direct methods to set cache-related HTTP headers, which you could also control to some extent by using the members of the HttpResponse object.
Table 18-10 shows the properties of the HttpCachePolicy class.