【发布时间】:2011-10-14 16:39:15
【问题描述】:
我有一个 cms,每个页面都将上次更新的时间存储在数据库中。我已经在 smarty (3.1) 中设置了缓存,但我希望能够清除缓存并强制它创建一个新的缓存文件,如果页面自上次保存的缓存文件以来已更新,但要做到这一点我需要知道缓存文件的创建时间。
有没有办法获取缓存文件的时间戳?
谢谢
【问题讨论】:
我有一个 cms,每个页面都将上次更新的时间存储在数据库中。我已经在 smarty (3.1) 中设置了缓存,但我希望能够清除缓存并强制它创建一个新的缓存文件,如果页面自上次保存的缓存文件以来已更新,但要做到这一点我需要知道缓存文件的创建时间。
有没有办法获取缓存文件的时间戳?
谢谢
【问题讨论】:
我最近回答了一个类似的问题:Smarty cache site properties in database
<?php
// fill these if you do cache grouping and or have different compiles of the same template
$template = 'foobar.tpl';
$cache_id = null;
$compile_id = null;
$smarty = new Smarty();
$tpl = $smarty->createTemplate($template, $cache_id, $compile_id);
if ($tpl->isCached() && $tpl->cached->timestamp < $yourTimestampFromDB) {
$smarty->clearCache($template, $cache_id, $compile_id);
}
【讨论】:
->getTimestamp()。 :s
:P
Smarty_CacheResource 可能由于某种竞争条件而报告过时的时间戳吗? (注意,我并不怀疑你是对的,只是想理解你在说什么。)
我不确定 Smarty 是否有任何内部功能。但是请查看filemtime() 和filectime,分别确定文件上次修改和更改的时间。
来自 php.net:
$filename = 'somefile.txt';
if (file_exists($filename)) {
echo "$filename was last changed: " . date("F d Y H:i:s.", filectime($filename));
}
修改时间和更改时间的区别:
注意:在大多数 Unix 文件系统中,当文件的 inode 数据发生更改时,文件被认为已更改;也就是说,当来自 inode 的权限、所有者、组或其他元数据被更新时。另请参见 filemtime()(当您想在网页上创建“上次修改”页脚时要使用的内容)和 fileatime()。
【讨论】:
Smarty_CacheResource_Custom 类,其中包含从 Smarty_CacheResource 继承的 fetchTimestamp() 方法。