2012-02-23 63 views
4

我使用smarty缓存功能,今天我意识到我的内容wasnt刷新正确。Smarty缓存与数据库内容错误

我使用的文件缓存在智者与这些模板:

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

我有一个自定义的CMS,这让我改变主网页不刷新,我使用:

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

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

回答

1

您有两种选择。

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

2)编写您自己的CacheResource。然后,您可以重载fetch()和fetchTimestamp()方法,以额外查询某个数据库的外部修改时间。通过这种方式,您无需clearCache()即可通知Smarty事情已发生变化。

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

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

你应该看看[$ smarty-> force_cache](http://www.smarty.net/docs/en/variable.force.cache.tpl )。如果你“手动”使缓存失效而不是调用'clearCache()' – rodneyrehm 2012-02-23 23:02:32

+3

@rodneyrehm为什么?明确会清除它,强制会覆盖它? – EscoMaji 2012-02-24 12:59:29