Setting optimum http caching headers and server params in ASP.Net MVC and IIS 7.5

我怕爱的太早我们不能终老 提交于 2019-12-03 08:43:41

问题


I have an ASP.Net site (happens to be MVC, but that's not relevant here) with a few pages I'd like cached really well.

Specifically I'd like to achieve:

  1. output cached on the server for 2 hours.
  2. if the file content on the server changes, that output cache should be flushed for that page
  3. cached in the browser for 10 minutes (i.e. don't even ask the server if it's that fresh)
  4. when the browser does make an actual subsequent request, I'd like it to use etags, so that the server can return a 304 if not modified.

(note - time values above are indicative examples only)

  • 1) and 2) I can achieve by Response.Cache.SetCacheability(HttpCacheability.Server)
  • I know 3) can be achieved by using max-age and cache-control:private
  • I can emit etags with Response.Cache.SetETagFromFileDependencies();

but I can't seem to get all of these things to work together. Here's what I have:

    Response.Cache.SetCacheability(HttpCacheability.ServerAndPrivate);
        Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        Response.Cache.SetETagFromFileDependencies();
        Response.Cache.SetValidUntilExpires(true);
        Response.Cache.SetMaxAge(TimeSpan.FromSeconds(60 * 10));

Is the scenario I want possible? In particular:

  • can browsers do both 3) and 4) like that? When Firefox issues a new request after it expires in the local cache, it does indeed send the etag the server responded with before, but I get a 200 response.
  • setting the variables like above, where would I set the duration of the output caching?

Thanks for any tips!


回答1:


I'm not sure if you've solved this problem yet (several months later...), but this should be possible.

SetMaxAge should set the amount of "guarranteed" fresh time. If you additionally send an ETag, you'll have satisfied 3 & 4. Requirements 1 & 2 can be solved orthogonally with whatever server-side caching mechanism you use: I've never used ASP.NET server-side cache like this, but it's almost certainly possible.

I'd remove extraneous headers from your responses such as SetRevalidation - why would this be necessary?



来源:https://stackoverflow.com/questions/3704433/setting-optimum-http-caching-headers-and-server-params-in-asp-net-mvc-and-iis-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!