如何防止 Play 浏览器缓存?

发布于 2024-12-06 13:16:44 字数 894 浏览 1 评论 0原文

我的应用程序的一部分提供了一个要使用 redirect() 方法下载的文件。我发现 Chrome(奇怪的是,不是 Firefox 或 IE)正在缓存这个文件,这样即使服务器端发生了更改,也会下载相同的版本。我认为有一种方法可以告诉浏览器不要缓存文件,例如 像这样在 HTML 中,或者通过在 HTTP 标头中添加某些内容。我可能可以在较低级别的 Web 框架中解决这些问题,但我不知道如何获取 Play! 中的标题,并且 HTML 选项不起作用,因为它不是 HTML 文件。

似乎总有一种聪明而简单的方法来完成 Play! 中的常见任务,那么有没有一种聪明而简单的方法来防止控制器中的缓存呢?

谢谢!

编辑:

马特向我指出 http.cacheControl 设置< /a>,控制整个站点的缓存。虽然这可行,但我对大多数站点的缓存没有问题,尤其是 CSS 等。如果可能的话,我想一次控制一个 URL 的缓存(在本例中是指向下载文件的 URL)。它并不完全是一个高流量的网站,所以这只是学术兴趣的讨论。

理想情况下,我想做类似的事情:

public static void downloadFile(String url) {
  response.setCaching(false);  // This is the method I'm looking for
  redirect(url);  // Send the response
}

Part of my app provides a file to be downloaded using the redirect() method. I have found that Chrome (and not Firefox or IE, weirdly) is caching this file so that the same version gets downloaded even if it has changed server-side. I gather that there is a way to tell a browser not to cache a file, e.g. like this in the HTML, or by adding something to the HTTP header. I could probably figure those out in a lower-level web framework, but I don't know how to get at the header in Play!, and the HTML option won't work because it's not an HTML file.

It seems like there's always a clever and simple way to do common tasks in Play!, so is there a clever and simple way to prevent caching in a controller?

Thanks!

Edit:

Matt points me to the http.cacheControl setting, which controls caching for the entire site. While this would work, I have no problem with most of the site being cached, especially the CSS etc. If possible I'd like to control caching for one URL at a time (the one pointing to the downloading file in this case). It's not exactly going to be a high-traffic site, so this is just academic interest talking.

Ideally, I'd like to do something like:

public static void downloadFile(String url) {
  response.setCaching(false);  // This is the method I'm looking for
  redirect(url);  // Send the response
}

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

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

发布评论

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

评论(6

隔纱相望 2024-12-13 13:16:44

Play 框架 response 对象有一个 setHeader 方法。您可以像这样添加您想要的标题,例如:

response.setHeader("Cache-Control", "no-cache");

Play framework response object has a setHeader method. You can add the headers you want like this, for example:

response.setHeader("Cache-Control", "no-cache");
迷乱花海 2024-12-13 13:16:44

我还没有测试过它,但它看起来像 http.cacheControl 配置设置 可能会起作用。

http.cacheControl

静态文件的 HTTP 响应标头控制:设置默认的 max-age(以秒为单位),告诉用户的浏览器应该缓存页面多长时间。仅在 prod 模式下读取,在 dev 模式下缓存被禁用。例如,发送no-cache

http.cacheControl=0

默认:3600 – 将缓存过期设置为一小时。

I haven't tested it, but it looks like the http.cacheControl configuration setting might work.

http.cacheControl

HTTP Response headers control for static files: sets the default max-age in seconds, telling the user’s browser how long it should cache the page. This is only read in prod mode, in dev mode the cache is disabled. For example, to send no-cache:

http.cacheControl=0

Default: 3600 – set cache expiry to one hour.

美人迟暮 2024-12-13 13:16:44

其实是这样的:

response().setHeader("Cache-Control", "no-cache");

It is actually this:

response().setHeader("Cache-Control", "no-cache");
z祗昰~ 2024-12-13 13:16:44

Tommi 的答案是好的,但为了确保它在每个浏览器中都有效,请使用:

response().setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store");

Tommi's answer is ok, but to make sure it works in every browser, use:

response().setHeader("Cache-Control", "no-cache, max-age=0, must-revalidate, no-store");
话少情深 2024-12-13 13:16:44
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0"); // HTTP 1.1.
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, s-maxage=0"); // HTTP 1.1.
内心荒芜 2024-12-13 13:16:44

当前正在播放 2.5.x 至 2.8.x
您可以在配置中设置资产文件夹或资产文件的缓存寿命。

对于文件夹

play.assets.cache."/public/stylesheets/"="max-age=100"
play.assets.cache."/public/javascripts/"="max-age=200"

-对于特定文件

play.assets.cache."/public/stylesheets/bootstrap.min.css"="max-age=3600"

----文档
https://www.playframework.com/documentation/2.8.x/AssetsOverview

On play currently 2.5.x to 2.8.x
you can set cache life of both assets folder or assets file in configuration.

For folder-

play.assets.cache."/public/stylesheets/"="max-age=100"
play.assets.cache."/public/javascripts/"="max-age=200"

for specific file -

play.assets.cache."/public/stylesheets/bootstrap.min.css"="max-age=3600"

--- documentation
https://www.playframework.com/documentation/2.8.x/AssetsOverview

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