如何防止 Play 浏览器缓存?
我的应用程序的一部分提供了一个要使用 redirect()
方法下载的文件。我发现 Chrome(奇怪的是,不是 Firefox 或 IE)正在缓存这个文件,这样即使服务器端发生了更改,也会下载相同的版本。我认为有一种方法可以告诉浏览器不要缓存文件,例如 像这样在 HTML 中,或者通过在 HTTP 标头中添加某些内容。我可能可以在较低级别的 Web 框架中解决这些问题,但我不知道如何获取 Play! 中的标题,并且 HTML 选项不起作用,因为它不是 HTML 文件。
似乎总有一种聪明而简单的方法来完成 Play! 中的常见任务,那么有没有一种聪明而简单的方法来防止控制器中的缓存呢?
谢谢!
编辑:
理想情况下,我想做类似的事情:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Play 框架
response
对象有一个setHeader
方法。您可以像这样添加您想要的标题,例如:Play framework
response
object has asetHeader
method. You can add the headers you want like this, for example:我还没有测试过它,但它看起来像
http.cacheControl
配置设置 可能会起作用。I haven't tested it, but it looks like the
http.cacheControl
configuration setting might work.其实是这样的:
It is actually this:
Tommi 的答案是好的,但为了确保它在每个浏览器中都有效,请使用:
Tommi's answer is ok, but to make sure it works in every browser, use:
当前正在播放 2.5.x 至 2.8.x
您可以在配置中设置资产文件夹或资产文件的缓存寿命。
对于文件夹
-对于特定文件
----文档
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-
for specific file -
--- documentation
https://www.playframework.com/documentation/2.8.x/AssetsOverview