2

我想缓存一些在每小时开始时(xx:00:01)显示不同数据的php页面?

到目前为止,我已经找到了一种在访问(或修改文件)时缓存页面 +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);

在每一页的顶部。

4

2 回答 2

2

可以用 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 "

Better to just do this in the PHP itself (after session_start()).

<?php
$nexthour = mktime (date("H"), 59, 59);
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', $nexthour)); 
?>
于 2012-01-22T13:05:21.603 回答
1

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

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

于 2012-01-22T03:00:37.527 回答