自定义网络服务器缓存

发布于 2024-08-23 03:12:19 字数 726 浏览 6 评论 0原文

我正在嵌入式系统上使用自定义 Web 服务器,并且在正确设置 HTTP 标头进行缓存时遇到一些问题。

我们的网络服务器将所有动态内容生成为 XML,并且我们使用半静态 XSL 文件来显示它,并添加一些动态 JSON 请求以及半静态图像。我说“半静态”是因为当我们需要进行固件更新时会出现问题,这可能会更改 XSL 和图像文件。

需要做的事情如下:缓存 XSL 和图像文件,但不缓存 XML 和 JSON 响应。我可以完全控制 HTTP 响应,目前:

  1. 使用 ETag 与 XSL 和图像文件,使用修改的时间和大小生成 ETag
  2. 设置缓存控制:XML 和 JSON 响应上无缓存

正如我所说,一切在固件更新之前,当 XSL 和图像文件有时被缓存时,它可以正常工作。我发现它在最新版本的 Firefox 和 Safari 上运行良好,但在 IE 上却遇到了一些问题。

我知道解决此问题的一种方法是简单地在每个版本之后重命名 XSL 和图像文件(例如 logo-v1.1.png、logo-v1.2.png)并将 Expires 标头设置为将来的日期,但是这对于 XSL 文件来说会很困难,我想避免这种情况。

注意:设备上有一个时钟,但需要用户设置它,并且可能不是 100% 可靠,这可能是导致我在使用 ETag 时出现缓存问题的原因。

我应该采用的最佳实践是什么?我想避免尽可能多的网络服务器请求,但在软件更新后使旧的 XSL 和图像文件失效是第一要务。

I'm working with a custom webserver on an embedded system and having some problems correctly setting my HTTP Headers for caching.

Our webserver is generating all dynamic content as XML and we're using semi-static XSL files to display it with some dynamic JSON requests thrown in for good measure along with semi-static images. I say "semi-static" because the problems occur when we need to do a firmware update which might change the XSL and image files.

Here's what needs to be done: cache the XSL and image files and do not cache the XML and JSON responses. I have full control over the HTTP response and am currently:

  1. Using ETags with the XSL and image files, using the modified time and size to generate the ETag
  2. Setting Cache-Control: no-cache on the XML and JSON responses

As I said, everything works dandy until a firmware update when the XSL and image files are sometimes cached. I've seen it work fine with the latest versions of Firefox and Safari but have had some problems with IE.

I know one solution to this problem would be simply rename the XSL and image files after each version (eg. logo-v1.1.png, logo-v1.2.png) and set the Expires header to a date in the future but this would be difficult with the XSL files and I'd like to avoid this.

Note: There is a clock on the unit but requires the user to set it and might not be 100% reliable which is what might be causing my caching issues when using ETags.

What's the best practice that I should employ? I'd like to avoid as many webserver requests as possible but invalidating old XSL and image files after a software update is the #1 priority.

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

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

发布评论

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

评论(2

审判长 2024-08-30 03:12:19

我们正在从事同一个项目吗?我走了很多死胡同,找出解决这个问题的最佳方法。

我将 .html 和 .shtml 文件(动态 JSON 数据)设置为立即过期。 (“缓存控制:无缓存\r\n过期:-1\r\n”)
其他一切都将在 10 年后到期。 ("Cache-Control: max-age=290304000\r\n")

我的 makefile 对所有 .html 文件运行一个 perl 脚本,并识别您所说的“半静态”内容(图像、javascript、css)。然后对这些文件运行 md5 校验和,并将校验和附加到文件中:

<script type="text/Javascript" src="js/all.js?7f26be24ed2d05e7d0b844351e3a49b1">

问号后面的所有内容都会被忽略,但除非引号之间的所有内容都匹配,否则浏览器不会缓存它。

我使用 all.js 和 all.css 因为所有内容都是使用相同的脚本组合和缩小的。

出于好奇,您使用什么嵌入式网络服务器?

Are we working on the same project? I went down a lot of dead ends figuring out the best way to handle this.

I set my .html and my .shtml files (dynamic JSON data) to expire immediately. ("Cache-Control: no-cache\r\nExpires: -1\r\n")
Everything else is set to expire in 10 years. ("Cache-Control: max-age=290304000\r\n")

My makefile runs a perl script over all the .html files and identifies what you call "semi-static" content (images, javascript, css.) The script then runs a md5 checksum on those files and appends the checksum to the file:

<script type="text/Javascript" src="js/all.js?7f26be24ed2d05e7d0b844351e3a49b1">

Everything after the question mark is ignored, but no browser will cache it unless everything between the quotes matches.

I use all.js and all.css because everything's combined and minified using the same script.

Out of curiosity, what embedded webserver are you using?

能怎样 2024-08-30 03:12:19

尝试缓存控制:无存储no-cache 告诉客户端响应可以被缓存;除非缓存无法联系原始服务器,否则它通常不会被重用。

顺便说一句,单独设置 ETag 不会使响应可缓存;您还应该设置Cache-Control: max-age=nnn

您可以使用 http://redbot.org/ 检查您的回复将被如何处理

Try Cache-Control: no-store. no-cache tells the client that the response can be cached; it just generally isn't reused unless the cache can't contact the origin server.

BTW, setting an ETag alone won't make the response cacheable; you should also set Cache-Control: max-age=nnn.

You can check how your responses will be treated with http://redbot.org/

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