数据库中的Smarty缓存站点属性

时间:2011-10-10 15:16:47

标签: php caching smarty

我打算在数据库中构建一个具有动态内容(属性为title,url等)的站点。我想每次查询所有内容并分配变量都是非常不必要的,所以我读过缓存。

我使用Smarty模板,系统。

   include('libs/Smarty/Smarty.class.php');

    $smarty = new Smarty;
    $smarty->setCaching(true);

    if (!$smarty->isCached('index.tpl')) {

    //query here and assign..
    $smarty->assign('title', 'Test');
    }

    $smarty->display('themes/simple/index.tpl');
  1. 上面的代码什么时候重新检查缓存?如果我对我的数据库网站属性进行更改,我希望立即更改我的网站,可能是非常重要的东西,如将网站放到维护等等。
  2. 我是否真的需要在每次页面加载时反复查询所有数据以获取站点信息,或者是否有任何解决方案,例如使用缓存检查数据库行结构,或类似的东西?
  3. 解决方案,我需要!


    更新: 我尝试了clearCache的方法,但似乎没有正常工作。我更新了数据库,但是“clearCache”方法似乎没有被触发,或者什么。

    $smarty = new Smarty;
    $smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
    
    //just to test
    $smarty->clearCache('index.tpl');
    
    
    if (!$smarty->isCached('index.tpl')) {
    
    
    
    //do stuff here
    

1 个答案:

答案 0 :(得分:1)

修改数据库中的内容时,请通知Smarty清除相关的缓存clearCache()。

您还可以根据存储在数据库中的某个时间戳检查缓存的修改时间。

<?php
$smarty = new Smarty();
$tpl = $smarty->createTemplate($template_file, $cache_id);
$databaseLastModified = strtotime("yesterday"); // determine when the database was modified last
if (!$tpl->isCached() || $tpl->cached->timestamp < $databaseLastModified) {
  $tpl->force_cache = true; // in case of timestamp < modified
  // load data for smarty to process
  $tpl->assign('your', 'stuff');
}
$tpl->display();
相关问题