This post originated from an RSS feed registered with .NET Buzz
by Darrell Norton.
Original Post: ASP.NET "VaryByCustom" page output caching
Feed Title: Darrell Norton's Blog
Feed URL: /error.htm?aspxerrorpath=/blogs/darrell.norton/Rss.aspx
Feed Description: Agile Software Development: Scrum, XP, et al with .NET
Steve Eichert was interested in how to create a custom caching setup, so here are some notes I took from the Devscovery conference.
We all know that the OutputCache directive allows ASP.NET to cache fully-rendered pages. We can instruct ASP.NET to cache different copies based on form variables like this: <%@ OutputCache Duration=”60” VaryByParam=”ZipCode” %>
Or we can cache based on HTTP Headers like this: <%@ OutputCache Duration=”60” VaryByParam=”None” VaryByHeader=”User-Agent” %>
The key to creating a custom cache variance is understanding that ASP.NET uses a simple string comparison to determine if a cached result should be returned instead of processing the page. For example, say we want to cache a certain page by SessionID. We add the OutputCache directive like this: <%@ OutputCache Duration=”60” VaryByParam=”None” VaryByCustom=”SessionID” %>
Now, in global.asax, we must override the GetVaryByCustomString method, like this: