Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputcache: VaryByHeader="User-Agent" or VaryByCustom="Browser"?

I'm not sure about the differences between VaryByHeader="User-Agent" and VaryByCustom="Browser". Is it right that the latter will create less cached pages? Or in other words, if I wish to cache only for different browser-types (and versions), the latter is the better choice?

Thx for any advice! sl3dg3

like image 487
sl3dg3 Avatar asked May 15 '11 08:05

sl3dg3


2 Answers

From the MSDN article - ASP.NET Caching: Techniques and Best Practices:

In order to enable separate cache entries for each browser, VaryByCustom can be set to a value of "browser". This functionality is built into the caching module, and will insert separate cached versions of the page for each browser name and major version.

I'd take this to mean that the following useragents would all be considered IE9.0, and create one cache entry:

  • Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))
  • Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)
  • Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)

But would create 3 entries if you used VaryByHeader="User-Agent"

like image 185
russau Avatar answered Oct 25 '22 16:10

russau


The user agent can have more stuff than just the browser, like OS and even some plugins including .NET, so, browser by guessing should have less cached pages.

Also, this is what the best practices page recommend to use:

http://msdn.microsoft.com/en-us/library/aa478965.aspx

Also from this best practices article I can see that browser DOES vary by browser versions. That was my only fear and it's good they already cover it!

In order to enable separate cache entries for each browser, VaryByCustom can be set to a value of "browser". This functionality is built into the caching module, and will insert separate cached versions of the page for each browser name and major version.

<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="browser"  %>

Note that even with user-agent you wouldn't have so many more copies of the pages, so, I'd argue it's not a big deal which one you puck.

like image 3
Meligy Avatar answered Oct 25 '22 15:10

Meligy