我需要.htaccess来缓存php页面,并在每小时开始时(xx:00:00)使其过期


I need .htaccess to cache php pages, and expire them at the beginning of every hour (xx:00:00)

我想缓存多个php页面,这些页面在每小时开始时(xx:0:01)显示不同的数据?

到目前为止,我已经找到了一种从访问(或修改文件)起+1小时缓存页面的方法,但如果用户在xx:59:00访问页面,那么在xx+1:00:01,他将看到缓存的页面数据,而不是新显示的数据。

我需要写些什么才能获得定期的"最重要的一小时"缓存到期,最好使用.htaccess?

最终代码(非htaccess):

$nexthour = mktime(date("H")+1, 00, 20) - mktime();
header("Cache-Control: public, must-revalidate, max-age=".$nexthour.", s-maxage=".$nexthour);

在每一页的顶部。

可以用htaccess完成,但有点痛苦。

RewriteCond %{TIME_WDAY} ^0$
RewriteCond yourfile.php - [E=daystring:SUN]
#etc (7x)
RewriteCond %{TIME_MON} ^0$
RewriteCond yourfile.php - [E=monthstring:JAN]
#etc (12x)
Header set "Expires" "%{daystring}, %{TIME_DAY} %{monthstring} %{TIME_YEAR} %{TIME_HOUR}:59:59 GMT "

最好只在PHP本身中执行此操作(在session_start()之后)。

<?php
$nexthour = mktime (date("H"), 59, 59);
header('Expires: '.gmdate('D, d M Y H:i:s 'G'M'T', $nexthour)); 
?>

一个选项是设置一个cron作业以按小时运行。您可以让它更新所有它关心的静态html文件,然后让Apache提供这些文件。

(这不是一个基于.htaccess的解决方案。)