gpt4 book ai didi

caching - 如何在 Vaadin 中禁用浏览器缓存

转载 作者:行者123 更新时间:2023-12-02 10:37:52 25 4
gpt4 key购买 nike

我的问题很短(希望很容易解决!):如何在我的使用 vaadin 实现的网络服务中完全禁用浏览器缓存?

我想完全禁用缓存,因为当我尝试进行一些 PDF 流式传输并在浏览器中显示它们时遇到问题。

我已经阅读了有关我的问题的解决方案,例如:

Using <meta> tags to turn off caching in all browsers?

他们讨论了向 Web 应用程序添加一些 header 以禁用浏览器缓存。但如何将它们添加到我的 Vaadin 应用程序中?

非常欢迎简短的代码片段(并且很有帮助!)

再次感谢您与我分享的每一个答案和想法。

最佳答案

在我看来,您想在下载 PDF 文件时禁用缓存。假设您使用的是 DownloadStream流式传输内容,然后设置 Content-DispositionCache-Control如下的标题应该可以工作。

 DownloadStream stream = new DownloadStream(getStreamSource().getStream(), contentType, filename);
stream.setParameter("Content-Disposition", "attachment;filename=" + filename);
// This magic incantation should prevent anyone from caching the data
stream.setParameter("Cache-Control", "private,no-cache,no-store");
// In theory <=0 disables caching. In practice Chrome, Safari (and, apparently, IE) all ignore <=0. Set to 1s
stream.setCacheTime(1000);

如果您想禁用所有 Vaadin 请求的缓存,您必须查看 AbstractApplicationServlet 的源代码,并扩展诸如 #serveStaticResourcesInVAADIN 之类的方法。和其他 - 这很快很棘手,因为其中很多都是私有(private)方法。

更简单的方法可能是使用 Http Servlet Filter 将适当的参数添加到响应中,而无需修改您的应用程序。你可以自己写这个 - 应该很快很容易 - 尽管快速搜索发现 Apache2 许可的缓存过滤器: http://code.google.com/p/cache-filter/wiki/NoCacheFilter

我没有使用过缓存过滤器,但快速浏览一下就会发现它非常适合您。

关于caching - 如何在 Vaadin 中禁用浏览器缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12331993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com