在 ASP.Net MVC 和 IIS 7.5 中设置最佳 http 缓存标头和服务器参数

发布于 2024-09-18 19:49:06 字数 1047 浏览 2 评论 0原文

我有一个 ASP.Net 站点(恰好是 MVC,但这与这里无关),其中有几个页面我希望缓存得很好。

具体来说,我想实现:

  1. 输出在服务器上缓存 2 小时。
  2. 应该刷新
  3. 在浏览器中缓存 10 分钟的该页面的输出缓存(即甚至不询问服务器是否新鲜)
  4. 如果服务器上的文件内容发生变化,则当浏览器确实发出实际的后续请求时, ,我希望它使用 etags,以便服务器在不修改的情况下可以返回 304。

(注意 - 上面的时间值仅是指示性示例)

  • 1)和2)我可以通过Response.Cache.SetCacheability(HttpCacheability.Server)实现
  • 我知道3)可以通过使用max-age和cache-control:private来
  • 实现我可以使用 Response.Cache.SetETagFromFileDependency() 发出 etag;

但我似乎无法让所有这些东西一起工作。这就是我所拥有的:

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

我想要的场景可能吗?特别是:

  • 浏览器可以同时执行 3) 和 4) 吗?当 Firefox 在本地缓存过期后发出新请求时,它确实会发送服务器之前响应的 etag,但我收到 200 响应。
  • 像上面一样设置变量,我在哪里设置输出缓存的持续时间?

感谢您的任何提示!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2024-09-25 19:49:07

我不确定您是否已经解决了这个问题(几个月后......),但这应该是可能的。

SetMaxAge 应设置“保证”的新鲜时间。如果您另外发送 ETag,您将满足 3 & 4. 要求1& 2 可以与您使用的任何服务器端缓存机制正交地解决:我从未使用过这样的 ASP.NET 服务器端缓存,但几乎可以肯定这是可能的。

我会从您的响应中删除无关的标头,例如 SetRevalidation - 为什么这是必要的?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文