smarty局部页面缓存

web2.0网站速度优化的最好方案是减少数据库访问.
缓存是减少数据库访问的最佳方案.
有两种最基本缓存的方式
1.页面缓存
1.1 如果你用的是smarty做为ui模板的话.页面缓存十分的方便

require(‘Smarty.class.php’);
$smarty = new Smarty;
$smarty->caching = 1;
if(!$smarty->is_cached(‘index.tpl’)) {
// No cache available, do variable assignments here.
$contents = get_database_contents();
$smarty->assign($contents);
}
$smarty->display(‘index.tpl’);

以上为最基本的缓存方案,对于同一个tpl,根据不同的参数可以缓存多个.

$smarty->is_cached(‘index.tpl’,params); //根据参数判断
$smarty->display(‘index.tpl’ ,params);//根据参数显示

以上实现了整个页面的缓存
很多时候为了优化用户的体验,需要对局部即时显示.最方便的方案是对即时显示的部分做一下块标记.每次刷新时重新显示些块就可

index.php:

caching = 1;

function smarty_block_dynamic($param, $content, &$smarty) {
return $content;
}
$smarty->register_block(‘dynamic’, ‘smarty_block_dynamic’, false);

$smarty->display(‘index.tpl’);
?>

index.tpl is:

Page created: {‘0′|date_format:’%D %H:%M:%S’}

{dynamic}

Now is: {‘0′|date_format:’%D %H:%M:%S’}

… do other stuff …

{/dynamic}

官方介绍

1.2: 当然上面的smarty缓存只是把文件生成好的模板文件缓存在文件系统.更加高级的缓存方案是把文件静态化缓存到内存中.现在有很多此类工具.这里先不介绍.

2.数据缓存.
最流行的是memcache.



发表评论

您的电子邮箱地址不会被公开。

83 + = 93