2011-10-10 12 views
0

我将构建一个网站,其中包含数据库中的动态内容(属性如标题,URL等)。我想每次查询所有内容并分配变量都是非常不现实的,所以我已经阅读了关于缓存的内容。数据库中的Smarty缓存站点属性

我使用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. 我是否真的需要查询所有数据页面加载来获取网站信息,还是有任何解决方案,比如用缓存检查数据库行结构,或者类似的东西?

解决方案,我需要!


更新: 我尝试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 
+0

好吧,如果你想改变被立即提供给用户,你不应该使用缓存可言,这是它:-) –

+0

有没有办法像检查数据库表已被更改,如果是的话,重新查询一切,这应该节省一些时间?还是我误会了? – John

+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(); 
+0

我更新了我的帖子=)看看 – John

+0

好吧,如果你使用PhpMyAdmin或其他类似的工具对数据库进行操作,那么显然你无法清除Smarty的缓存。你需要在你的管理工具中使用 - > clearCache()。 – rodneyrehm

+0

如果您更喜欢使用其他工具来处理数据库,那么您应该添加一列来添加上次更新时间(使用TRIGGER相当简单)并尝试上述代码。 – rodneyrehm