2

对于(企业)Web 项目,我想保留以前版本的静态文件,以便项目在准备好实施设计更改时可以自行决定。我最初的计划是为静态内容提供文件夹,如下所示:

company.com/static/1.0.0/
company.com/static/1.0.0/css/
company.com/static/1.0.0/js/
company.com/static/1.0.0/images/
company.com/static/2.0.0/
company.com/static/2.0.0/css/
company.com/static/2.0.0/js/
company.com/static/2.0.0/images/

这些文件夹中的每个文件都应该有一个缓存策略来“永远”缓存 - 至少一年。我还计划将 css 文件和 js 文件合并为一个,以尽量减少请求数量。

然后我还会提供一个current文件夹(符号链接到最新发布的版本)

company.com/static/current/
company.com/static/current/css/
company.com/static/current/js/
company.com/static/current/images/

这将解决我的第一个问题(项目和子网站可以将其代码锁定到某个版本,并且可以随时升级)。

但后来我可以看到一些缓存问题。现在我不能“只”缓存current文件夹,因为每个版本都会改​​变。我的缓存策略应该在那个文件夹上。

此外,对于每个版本,大多数静态文件无论如何都不会改变。永久缓存它们是否相关,如果有更改则重命名?

我在这里寻求建议,因为我想知道您在缓存和更改文件之间的最佳权衡。

4

3 回答 3

1

There are three approaches you can select from:

  1. Use resource's path as a cache key, i.e. when it changed - the browser will have to download new version of your resources. In this case you don't need /current folder at all, you just need to avoid .html page caching and put appropriate path to your resources in it.
  2. You can point browser to /current folders only and add ETag to your resources, in this case another server request will be made from the client, but it will be conditional request (i.e. with If-None-Match header), so you can return 304 response (with no resource body) until your customer decide to migrate to another version. Another drawback of such solution (if you have several customers who use different versions) is that /current folder will contain only some single version of the design.
  3. As you're going to concatenate resources into single files, you can specify resource version as part of url: /current/js/combined.js?version=1.0.0.0 But this is not much different from first approach.

Hope this helps.

于 2012-03-08T21:36:54.247 回答
1

看看 Google、Microsoft 等如何为他们的 jQuery CDN 实现缓存策略可能是值得的

对于版本化的 URL,您的永久缓存策略是可以的。

对于当前的 URL,您显然需要更短的到期时间。

需要考虑的几件事:

  • 应用程序如何能够针对 /current/ 进行测试,即如果他们使用它,您如何知道更改不会破坏现有应用程序?

  • 永久缓存实际上只是减少“当前会话”期间的请求,因为大多数浏览器缓存不足以长时间保存文件(当人们浏览其他网站时它们会被删除)

于 2012-03-14T10:49:54.267 回答
1

小心 HTTP 缓存。我前段时间研究过这个。 我关于 HTTP 缓存的博客文章

于 2012-03-07T10:14:49.380 回答