GWT 缓存概念

发布于 2024-10-19 12:31:31 字数 163 浏览 3 评论 0原文

有人可以简单地向我解释 GWT 中缓存的概念吗?我在很多地方都读过这篇文章,但可能由于我的知识有限,我无法理解它。

例如 nocache.js、cache.js

或其他东西,例如使客户端永久缓存文件或如何使客户端缓存文件,然后如果文件在服务器上发生更改,客户端才会再次下载这些文件

Can someone explain to me in simple term the concept of caching in GWT. I have read this in many places but may be due to my limited knowledge, i'm not being able to understand it.

Such as nocache.js, cache.js

or other things such as making the client cache files forever or how to make files cached by the client and then if file get changed on the server only then the client download these files again

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

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

发布评论

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

评论(2

箹锭⒈辈孓 2024-10-26 12:31:31

一般来说,有 3 种类型的文件 -

  1. 永久缓存 缓存
  2. 某个时间
  3. 从不缓存

某些文件永远不会被缓存,并且将始终落入“从不缓存”存储桶中。但最大的性能优势来自于系统地将第二个存储桶中的文件转换为可以永久缓存的文件。 GWT 可以通过多种方式轻松实现这一点。

.cache.js 文件可以安全地永久缓存。如果它们发生变化,GWT 将重命名该文件,因此浏览器将被迫再次下载它。

.nocache.js 文件永远不应该被缓存。即使您更改一行代码并重新编译,该文件也会被修改。 nocache.js 包含 .cache.js 的链接,因此浏览器始终拥有该文件的最新版本非常重要。

第三个存储桶包含图像、CSS 和属于应用程序一部分的任何其他静态资源。 CSS 文件总是在变化,所以你不能告诉浏览器“永远缓存”。但如果您使用 ClientBundle / CssResource,GWT 将为您管理该文件。每次更改 CSS 时,GWT 都会重命名该文件,因此浏览器将被迫再次下载它。这使您可以设置强大的缓存标头以获得最佳性能。

总之 -

  1. 对于任何匹配 .cache. 的内容,设置一个远期过期标头,有效地告诉浏览器永久缓存它。
  2. 对于任何与 .nocache. 匹配的内容,请设置缓存标头以强制浏览器与服务器重新验证资源。
  3. 对于其他所有内容,您应该根据更改资源的频率设置一个短的过期标头。
  4. 尝试使用ClientBundle / CssResource;这会自动将您的资源重命名为 *.cache 存储桶

Generally, there are 3 type of files -

  1. Cache Forever
  2. Cache for some time
  3. Never Cache

Some files can never be cached, and will always fall in the "never cache" bucket. But the biggest performance wins comes from systematically converting files in the second bucket to files that can be cached forever. GWT makes it easy to do this in various ways.

The <md5>.cache.js files are safe to cache forever. If they ever change, GWT will rename the file, and so the browser will be forced to download it again.

The .nocache.js file should never be cached. This file is modified even if you change a single line of code and recompile. The nocache.js contains the links of the <md5>.cache.js, and therefore it is important that the browser always has the latest version of this file.

The third bucket contains images, css and any other static resources that are part of your application. CSS files are always changing, so you cannot tell the browser 'cache forever'. But if you use ClientBundle / CssResource, GWT will manage the file for you. Every time you change the CSS, GWT will rename the file, and therefore the browser will be forced to download it again. This lets you set strong cache headers to get the best performance.

In summary -

  1. For anything that matches .cache., set a far-in-the-future expires header, effectively telling the browser to cache it forever.
  2. For anything that matches .nocache., set cache headers that force the browser to re-validate the resource with the server.
  3. For everything else, you should set a short expires header depending on how often you change resources.
  4. Try to use ClientBundle / CssResource; this automatically renames your resources to *.cache bucket
恋竹姑娘 2024-10-26 12:31:31

这篇博文很好地概述了 GWT 引导过程(以及 GWT 的许多其他部分)顺便说一下,系统),这与缓存的内容和原因有很大关系。

基本上,生成的 nocache.js 文件是一段相对较小的 JS,其唯一目的是决定应该下载哪个生成的排列。

每个单独的排列都包含特定于用户的浏览器、语言等的应用程序的实现。这比简单的引导代码要多得多,因此需要缓存以便您的应用程序快速响应。这些是 GWT 编译器生成的 cache.html 文件。

当您重新编译和部署应用程序时,您的用户将照常下载 nocache.js 文件,但这将告诉他们的浏览器下载包含应用程序新功能的新 cache.html 文件。现在,这也将被缓存,以供下次加载您的应用程序时使用。

This blog post has a good overview of the GWT bootstrapping process (and many other parts of the GWT system, incidentally), which has a lot to do with what gets cached and why.

Basically, the generated nocache.js file is a relatively small bit of JS whose sole purpose is to decide which generated permutation should be downloded.

Each individual permutation consists of the implementation of your app specific to the browser, language, etc., of the user. This is a lot more code than the simple bootstrapping code, and thus needs to be cached for your app to respond quickly. These are the cache.html files that get generated by the GWT compiler.

When you recompile and deploy your app, your users will download the nocache.js file as normal, but this will tell their browsers to download a new cache.html file with the app's new features. This will now be cached as well for the next time they load your app.

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