我要在数据库中建立一个具有动态内容的网站(属性为标题,url等)。我想这将是非常不必要的查询所有的东西,并分配变量,每次,所以我读过缓存。
我使用Smarty模板,system.
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');
- 上面的代码什么时候重新检查缓存?如果我要改变我的数据库网站属性,我想立即改变我的网站,可以是非常重要的东西,比如把网站维护,等等。
- 我真的需要查询所有的数据,一次又一次地在每个页面加载抓取网站信息,或者有任何解决方案,如检查数据库行结构与缓存,或类似的东西?
解决方案,我需要!
更新:我尝试了cleararchache的方法,但它似乎不能正常工作。我更新了数据库,但是"cleararchache"方法,似乎没有被触发,或者什么。
$smarty = new Smarty;
$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);
//just to test
$smarty->clearCache('index.tpl');
if (!$smarty->isCached('index.tpl')) {
//do stuff here
当修改数据库中的内容时,通知Smarty清除相关缓存cleararchache ()
您还可以根据数据库中存储的时间戳检查缓存的修改时间。
<?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();