Smarty缓存与数据库内容错误

时间:2012-02-23 11:04:18

标签: caching smarty

我使用smarty缓存功能,今天我意识到我的内容没有正确刷新。

我使用这些模板在smarty中使用文件缓存:

  • index.html(使用此缓存主页(约8k)
  • list.html(使用此缓存大约10页 - 每个约7k)

我有一个自定义的cms,这允许我更改它不刷新的主页面,我使用:

cache_dir&正确缓存feacture,因为如果我删除缓存文件,我可以看到我的内容刷新。

是否有一种简单的方法来更新这些文件,而不是每次刷新页面内容时都删除它们?

2 个答案:

答案 0 :(得分:4)

你可以用这个:

$Smarty = new Smarty();
$Smarty->caching = 1;

$SmartyTemplate = $Smarty->createTemplate($yourfile, $your_cache_id);
// $row = mysql_query("select date_modified from table where ...

if ($SmartyTemplate->isCached() && $SmartyTemplate->cached->timestamp < $row['date_modified']) {
    $Smarty->clearCache($yourfile, $your_cache_id); 
}
$SmartyTemplate->assign('variables', 'data');
$SmartyTemplate->display();

答案 1 :(得分:1)

这里有两个选择。

1)更新有关index.html或list.html的任何内容时,请告诉Smarty使用$smarty->clearCache("index.html");清除相应的缓存。请参阅the docs

2)编写自己的CacheResource。然后,您可以重载fetch()和fetchTimestamp()方法,以额外查询某个数据库以获取外部修改时间。这样,您就不必clearCache()通知Smarty某些事情发生了变化。

第一个选项更简单,更快捷。第二个选项是全局的,可能会浪费额外数据库连接上的资源。