ASP.NET OutputCache + OutputCache Profiles
In many projects i built, i used the OutputCache to improve WebSites performance and scalability. OutputCache is a powerful tool for increasing WebSites performance. When using OutputCache, the IIS saves a new version for the specific requested Page in memory. The next time we request the same Page, the IIS will serve us the rendered Page from memory, there for not process the page again. We can also set some conditions for the cache version like: when to cache it, how, and for how long (Duration).
First, how OutputCache directive looks like. in this case, we set the Duration to 300 (Seconds), witch means 5 minutes (300 / 60), and we set the VaryByParam to *, witch means that IIS will save a version for each requested Page and QueryString Params.
<%@ OutputCache VaryByParam="*" Duration="300" %>
For example, when we request URL like this: http://www.microsoft.co.il , the IIS now saves a version specific for this address, and when we request http://www.microsoft.co.il?catid=432 , the IIS now saves another version for this specific URL with the specific params added to QueryString params. The next time we request one of those URLs, the IIS will serve us the rendered version from memory for the specific requested page and for the requested QueryString params. Therefor the IIS will not process the Page again. Witch means we save a lots of IIS thinking time.
All most in every WebSite i built, i add an OutputCache directive in all most each of the Pages\UserControls, and set the Duration, and the varyBy conditions separately. This becomes a problem when i want to change some conditions or to change the cache Duration in all the WebSite Pages. In this case, i have to go threw the entire Pages/UserControls and change the OutputCache directive separately.
In VS2005, we have the ability to set a global cache Profile in web.config, and call it directly in the OutputCace directive. First, lets set the cache profiles in web.config under system.web section:
<caching>
<outputCacheSettings>
<outputCacheProfiles>
<add name="High" duration="36000" varyByParam="*"/>
<add name="Low" duration="300" varyByParam="catid"/>
</outputCacheProfiles>
</outputCacheSettings>
</caching>
Now lets see how we call the cache Profile in the OutputCache directive:
<%@ OutputCache CacheProfile="High" %>
Now, when i want to change the Duration or the conditions for my cached pages, i need to change them only in the web.config cache Profiles. Further more, in development environment, i will change the cache Duration to 0, for debugging.
Conclusion - OutputCache is powerful tool to increase performance, and now its even more easy to use.
Smart man once said..." I am cornholio - I need tipy for my bunghole"