我的PHP脚本缓存(公共)是否友好? [英] Is my PHP script cache (public) friendly?

查看:103
本文介绍了我的PHP脚本缓存(公共)是否友好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个可以提供许多文档的小型PHP应用程序。这些文档必须可由客户代理缓存。

I have finalized a small PHP application that can serve many documents. These documents must be cacheable by clients and proxies.

由于代理可以缓存我的结果,我必须格外小心因为我服务的文件可以有不同的MIME类型(基于$ _SERVER ['HTTP_ACCEPT']的内容协商)和不同的语言(基于这个顺序:$ _POST值/ $ _GET值/ URL / PHP会话值/ $ _COOKIE值/ $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] /默认脚本值)。

Since proxies can cache my results I must be extra careful because the documents I serve can have different MIMEs types (content negotiation based on $_SERVER['HTTP_ACCEPT']) and different languages (based in this order: $_POST value / $_GET value / URL / PHP session value / $_COOKIE value / $_SERVER['HTTP_ACCEPT_LANGUAGE'] / default script value).

总而言之,一个页面可以提供多种MIME类型许多使用相同 URL的语言(问题已更改:请参阅下面的编辑)。

To shortly sum up, a page can be served with many MIME type and many languages with the same URL (question changed: see edit below).

为了帮助缓存代理,我使用Vary:Accept标题组合与ETag标头。 ETags是当前语言的MD5和最后修改的时间戳。

To help cache on proxies I use the "Vary: Accept" header in combination with the ETag header. The ETags is a MD5 of the current language and the last modified timestamp.

我总是:


  • 发送过期标题

  • 发送缓存控制标题

  • 发送最后修改标题

  • 发送内容类型标题

  • 发送ETag标题(基于当前语言和上次修改时间戳)

  • 发送内容-Language

  • 如果文件是XHTML,发送Vary:Accept标题

  • Send an Expires header
  • Send a Cache-Control header
  • Send a Last-Modified header
  • Send a Content-Type header
  • Send an ETag header (based on current language and Last-Modified timestamp)
  • Send a Content-Language
  • Send a "Vary: Accept" header if the document is XHTML

现在我的问题:这是否足以帮助缓存代理客户端?我错过了一个东西/标题吗?

Now with my question: is this enough to help cache on proxies and clients? Did I miss a thing/header?

为了帮助您,这里是测试页面的HTTP响应标题(在我的本地环境中):

To help you, here’s the HTTP response header for a test page (on my local environment):

"
Date             Wed, 30 Dec 2009 18:56:26 GMT
Server           Apache/2.0.63 (Win32) PHP/5.1.0
X-Powered-By     PHP/5.1.0
Set-Cookie       Tests=697daqbmple2e1daq2dg74ur96; path=/
Expires          Wed, 30 Dec 2009 21:56:26 GMT
Cache-Control    public, max-age=10800
Last-Modified    Mon, 28 Dec 2009 15:11:49 GMT
Etag             "44fa50be4638161a596e4b75d6ab7a94"
Vary             Accept
Content-Language en-us
Content-Length   3043
Keep-Alive       timeout=15, max=100
Connection       Keep-Alive
Content-Type     application/xhtml+xml; charset=UTF-8
"

编辑:好的,我知道在这种情况下,提供一个包含许多MIME的文档拥有不同的语言(可能来自如此多的来源 - 见上文)只是简单的糟糕设计。如果你想这样做,只需使用私人缓存(代理上没有缓存)......我是否正确?

OK I understand that in this case serving a document with many MIMEs and having different languages (that can come from so many sources - see above) is just plain bad design. If you want to do this just use "private" cache (no cache on proxies)... Am I correct?

如果每种语言都有自己的URL(但每个URL可以与许多MIME一起提供)我的当前实现是否可以用于公共缓存(客户端+代理上的缓存)?

If each language have it's own URL (but each URL can be served with many MIME still) is my current implementation is OK for a "public" cache (cache on clients + proxies)?

推荐答案

由于您的输出还取决于代理无法像会话数据那样的内容,因此将(不可缓存的)重定向发送到实际内容不会更容易,这对于给定的URL(具有参数)将是固定的)因此更容易缓存。我知道这涉及额外的往返,但它可能更不容易出错,并且对于不完全理解/支持所有标题组合的代理也会导致更少的问题。

Since your output also depends on things a proxy cannot know like session data, won't it be easier to send a (non-cachable) redirect to the actual content, which would be fixed for a given URL (with parameters) and therefore much easier to cache. I know this involves an extra round-trip, but it's probably much less error-prone and would also cause less problems with proxies that don't completely understand/support all your header combinations.

另外,我猜测,如果你有两个客户端通过相同的代理但使用不同的语言cookie,你的当前方法将为同一个URL返回两个不同的ETag,这将使代理更新每个它看到另一个客户的时间。

Also, I'm guessing that, if you have two clients going through the same proxy but with different language cookies, your current method would return two different ETags for the same URL, which would make the proxy update its copy each time it sees the other client.

这篇关于我的PHP脚本缓存(公共)是否友好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆