ASP.NET MVC OutputCache vary by * and vary by user cookie
Asked Answered
T

3

20

I have an asp.net mvc 3 project and I have a home controller. I have tagged my Index action with this attribute:

[OutputCache(Location = System.Web.UI.OutputCacheLocation.Any, Duration = 120, VaryByParam = "*", VaryByCustom = "user")]
public ActionResult Index()
{
    return View();
}

The vary by custom with user is handled in Global.asax.cs to check the user cookie value so that caching changes based on whether the user is logged in or not and what user they are.

When I go to this page on my web server, I get these headers in the response:

Cache-Control   public, max-age=120
Content-Type    text/html; charset=utf-8
Content-Encoding    gzip
Expires Sun, 20 Mar 2011 21:50:09 GMT
Last-Modified   Sun, 20 Mar 2011 21:48:09 GMT
Vary    Accept-Encoding
Date    Sun, 20 Mar 2011 21:48:09 GMT
Content-Length  3105

Right off the bat, the Vary - Accept-Encoding value looks wrong, shouldn't it have sent a Vary - * instead?

I am rendering the User.Identity.Name property to this view as well and I am noticing that even when I log out it will still render the user name, until the 120 seconds expires.

public override string GetVaryByCustomString(HttpContext context, string custom)
{
    if (custom.Equals("user", StringComparison.OrdinalIgnoreCase))
    {
        HttpCookie cookie = context.Request.Cookies["user"];
        if (cookie != null)
        {
            return cookie.Value;
        }
    }
    return base.GetVaryByCustomString(context, custom);
}

Been playing with this for several hours, and am completely stuck, hopefully someone has an idea...

Thermosiphon answered 20/3, 2011 at 21:52 Comment(0)
L
18

You can have IIS compress the response before it gets cached by setting dynamicCompressionBeforeCache="true" on the urlCompression element in your web.config. This will result in the expected Vary:* header being returned.

Excerpt, from the IIS Configuration Reference, about the dynamicCompressionBeforeCache attribute:

The dynamicCompressionBeforeCache attribute specifies whether IIS will dynamically compress content that has not been cached. When the dynamicCompressionBeforeCache attribute is true, IIS dynamically compresses the response the first time a request is made and queues the content for compression. Subsequent requests are served dynamically until the compressed response has been added to the cache directory. Once the compressed response is added to the cache directory, the cached response is sent to clients for subsequent requests. When dynamicCompressionBeforeCache is false, IIS returns the uncompressed response until the compressed response has been added to the cache directory.

Lysol answered 23/7, 2011 at 0:23 Comment(1)
Very good tip. I dont know why it is false by default in IIS7.0, which leads us to a bugged outputcacheIlex
T
4

The problem was IIS dynamic compression. It fouls up the http headers. I solved it by rolling my own dynamic compression using an actionfilter.

Thermosiphon answered 11/5, 2011 at 16:12 Comment(0)
S
0

There is a Microsoft HotFix for this but they won't let you just download the hotfix.

You have to fill in your email for them to send it to you and it says :

  • "Do not deploy a hotfix in a production environment without first testing the hotfix."
  • "Back up the system or the computer that will receive the hotfix before you install the hotfix."

From what I can see it creates the following issues

  • There is no uninstall
  • Requires a restart
  • The last modified date is updated for every request even if it returns the cached version

More info:

http://support2.microsoft.com/kb/2877816/en-gb

http://blogs.iis.net/erez/archive/2013/08/02/iis-compression-overrides-the-vary-header.aspx

https://51degrees.com/support/documentation/net/iis-modification

So think carefully before you install this!

Sexcentenary answered 13/1, 2015 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.