缓存 PHP 脚本以供当天剩余时间使用
如何使 PHP 脚本对我们的私有反向代理 (Squid) 缓存友好?
我希望它在当天剩下的时间里保持缓存。 换句话说,该页面的最后修改时间是今天 00:00,并将于明天 00:00 过期。
要可靠地完成此操作,最少需要哪些 HTTP 标头?
编辑:我不希望客户端的浏览器以任何不同的方式缓存。 也就是说,我希望在任何给定时间保留对清除 Squid 服务器上陈旧页面的控制权。
How can I make a PHP script cache-friendly to our private reverse proxy (Squid)?
I would like it to stay cached for the rest of the day. In other words, the page's last modification is 00:00 today and it will expire at 00:00 tomorrow.
What are the minimum required HTTP headers to do this reliably?
EDIT: I do not want the client's browser to cache any differently. That is, I wish to retain control over purging stale pages on our Squid server at any given time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您尝试过使用 APC 吗?
http://us.php.net/apc
Have you tried using APC ?
http://us.php.net/apc
@OP:
下面是一些注释代码来实现您的要求。
@克莱图斯:
你说memcached是OP想要的,而这不是Squid的设计目的。
我不知道 Squid 的设计目的是什么,但我知道它的用途,并且肯定有人使用它作为反向代理来减轻动态页面生成的负担。 除了标准 HTTP 标头之外,不需要任何“搭配”。
我不知道为什么您在不了解应用程序和环境的性质的情况下这么快就推荐 memcached。
@OP:
Below is some commented code to achieve what you asked.
@cletus:
You said memcached is what the OP wants, and that this is not what Squid is designed for.
I don't know what Squid was designed for but I know what it is used for and there are definitely people using it as a reverse proxy to take load off dynamic page generation. No "tie-in" is needed except standard HTTP headers.
I'm not sure why you're so quick to recommend memcached without knowing more about the nature of the application and environment.
我假设您的意思是您想要缓存脚本的输出,而不是脚本本身。 而且您显然需要服务器端脚本,因此 HTTP 标头不是您想要的(除非与 Squid 有某种关联,这很可能是)。 但这不是 Squid 的设计目的。
对于这种事情,你确实需要 memcached (或类似的)。
检查结果是否在缓存中。 如果是,请将其退回。 如果没有,则生成结果(例如使用 ob_start() 等),将其放入缓存中并返回。
I assume you mean you want to cache the output of the script, not the script itself. And you clearly want serverside scripting so HTTP headers is not what you want (unless there is some tie-in to Squid, which there very well might be). But this isn't what Squid is designed for.
For this kind of thing, you really want memcached (or similar).
Check if the result is in the cache. If it is, return it. If not, generate the result (eg using ob_start(), etc), put it in the cache and return it.
您可以运行脚本并生成要提供的静态 HTML 文件。 这样,您就可以决定何时替换内容。
您不必在 Web 服务器内运行脚本。 cron 作业完美运行。
you could run your script and generate static HTML files to be served. that way, it's up to you when to replace the content.
you don't have to run your scripts inside the web server. a cron job works perfectly.