在一天剩下的时间里缓存PHP脚本

在一天剩下的时间里缓存PHP脚本,php,http,caching,reverse-proxy,Php,Http,Caching,Reverse Proxy,如何使PHP脚本缓存对我们的私有反向代理Squid友好 我希望它能在今天剩下的时间里一直被缓存。换句话说,页面的最后一次修改是今天00:00,明天00:00过期 要可靠地做到这一点,至少需要哪些HTTP头 编辑:我不希望客户端的浏览器缓存有任何不同。也就是说,我希望保留在任何给定时间清除Squid服务器上过时页面的控制权。我猜您的意思是希望缓存脚本的输出,而不是脚本本身。显然,您需要服务器端脚本,所以HTTP头不是您想要的,除非与Squid有某种关联,这很可能是有的。但这不是Squid的设计目的

如何使PHP脚本缓存对我们的私有反向代理Squid友好

我希望它能在今天剩下的时间里一直被缓存。换句话说,页面的最后一次修改是今天00:00,明天00:00过期

要可靠地做到这一点,至少需要哪些HTTP头


编辑:我不希望客户端的浏览器缓存有任何不同。也就是说,我希望保留在任何给定时间清除Squid服务器上过时页面的控制权。

我猜您的意思是希望缓存脚本的输出,而不是脚本本身。显然,您需要服务器端脚本,所以HTTP头不是您想要的,除非与Squid有某种关联,这很可能是有的。但这不是Squid的设计目的

对于这类事情,你真的想要或类似


检查结果是否在缓存中。如果是,请退回。如果没有,则使用ob_start等生成结果,将其放入缓存并返回。

我猜您的意思是希望缓存脚本的输出,而不是脚本本身。显然,您需要服务器端脚本,所以HTTP头不是您想要的,除非与Squid有某种关联,这很可能是有的。但这不是Squid的设计目的

对于这类事情,你真的想要或类似


检查结果是否在缓存中。如果是,请退回。如果没有,则使用ob_start等生成结果,将其放入缓存并返回。

您可以运行脚本并生成要提供的静态HTML文件。这样,何时替换内容取决于您


您不必在web服务器中运行脚本。cron作业非常有效。

您可以运行脚本并生成要提供的静态HTML文件。这样,何时替换内容取决于您

您不必在web服务器中运行脚本。cron作业工作正常。

@OP: 下面是一些注释代码,以实现您的要求

@克莱特斯: 你说memcached是OP想要的,而这不是Squid的设计目的

我不知道Squid是为什么而设计的,但我知道它的用途,而且肯定有人使用它作为反向代理来减轻动态页面生成的负担。除了标准HTTP头之外,不需要任何连接

我不知道你为什么这么快就推荐memcached,却不了解应用程序和环境的本质

<?php

// the time we got hit and generated content
$now = time();
$generatedAt = gmdate('D, d M Y H:i:s T', $now);

// the last modified date (midnight on the same day of generation, as
// per your business-rule)
$lastModified = gmdate('D, d M Y 00:00:00 T', $now);

// date of expiry (24 hours after the last modified date, as per your
// business-rule)
$expiresAt = gmdate('D, d M Y H:i:s T', strtotime($lastModified) + 86400);

// the minimum required http headers to make Squid do what you asked is
// Last-modified and Cache-control.  We need to give Cache-control the
// expiry time in terms of "age" (in seconds) so we calculate that below.
// Optionally you could also provide the "Expires: $expiresAt" header to
// tell the browser/client the same information, just in a different way.
// This is not required for Squid though.
$maxAge = strtotime($expiresAt) - strtotime($generatedAt);
header('Last-modified: ' . $lastModified);
header('Cache-control: max-age=' . $maxAge);

// The rest is simply informational
header('Content-type: text/plain');
echo "The content of this page was last modified at $lastModified\n";
echo "This page was generated at $generatedAt and will be cached by Squid for $maxAge seconds until $expiresAt\n";

// Sample output:
//
// The content of this page was last modified at Tue, 13 Jan 2009 00:00:00 GMT
// This page was generated at Tue, 13 Jan 2009 04:29:33 GMT and will be cached by Squid for 70227 seconds until Wed, 14 Jan 2009 00:00:00 GMT
@作品: 下面是一些注释代码,以实现您的要求

@克莱特斯: 你说memcached是OP想要的,而这不是Squid的设计目的

我不知道Squid是为什么而设计的,但我知道它的用途,而且肯定有人使用它作为反向代理来减轻动态页面生成的负担。除了标准HTTP头之外,不需要任何连接

我不知道你为什么这么快就推荐memcached,却不了解应用程序和环境的本质

<?php

// the time we got hit and generated content
$now = time();
$generatedAt = gmdate('D, d M Y H:i:s T', $now);

// the last modified date (midnight on the same day of generation, as
// per your business-rule)
$lastModified = gmdate('D, d M Y 00:00:00 T', $now);

// date of expiry (24 hours after the last modified date, as per your
// business-rule)
$expiresAt = gmdate('D, d M Y H:i:s T', strtotime($lastModified) + 86400);

// the minimum required http headers to make Squid do what you asked is
// Last-modified and Cache-control.  We need to give Cache-control the
// expiry time in terms of "age" (in seconds) so we calculate that below.
// Optionally you could also provide the "Expires: $expiresAt" header to
// tell the browser/client the same information, just in a different way.
// This is not required for Squid though.
$maxAge = strtotime($expiresAt) - strtotime($generatedAt);
header('Last-modified: ' . $lastModified);
header('Cache-control: max-age=' . $maxAge);

// The rest is simply informational
header('Content-type: text/plain');
echo "The content of this page was last modified at $lastModified\n";
echo "This page was generated at $generatedAt and will be cached by Squid for $maxAge seconds until $expiresAt\n";

// Sample output:
//
// The content of this page was last modified at Tue, 13 Jan 2009 00:00:00 GMT
// This page was generated at Tue, 13 Jan 2009 04:29:33 GMT and will be cached by Squid for 70227 seconds until Wed, 14 Jan 2009 00:00:00 GMT

你试过使用APC吗

另一种PHP缓存APC是一种免费开放的PHP操作码缓存。它的目的是为缓存和优化PHP中间代码提供一个自由、开放和健壮的框架


你试过使用APC吗

另一种PHP缓存APC是一种免费开放的PHP操作码缓存。它的目的是为缓存和优化PHP中间代码提供一个自由、开放和健壮的框架


老帖子,但我个人认为这是最好的方法,在这种情况下,如果我理解的问题是正确的。如果您的页面是由罕见的变化数据生成的。。即使在这种情况下,让脚本删除最后生成的HTML页面,然后再次生成它们。。这将是理想的。老帖子,但我个人认为这是最好的方法,在这种情况下,如果我理解的问题正确。如果您的页面是由罕见的变化数据生成的。。即使在这种情况下,让脚本删除最后生成的HTML页面,然后再次生成它们。。这将是理想的。