smarty页面局部缓存
2008-10-25 22:34
204 查看
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:
<?php
$smarty->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.
缓存是减少数据库访问的最佳方案.
有两种最基本缓存的方式
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:
<?php
$smarty->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.
相关文章推荐
- smarty局部页面缓存(局部不缓存)
- Varnish+ESI实现静态页面的局部缓存
- 使用Varnish+ESI实现静态页面的局部缓存(思路篇)
- 页面局部缓存
- smarty 局部不缓存
- ehcache 页面整体缓存和局部缓存
- smarty的局部缓存
- smarty局部缓存
- smarty总结及局部缓存
- 系统缓存全解析3:页面局部缓存
- Substitution 使用方法 页面缓存时的局部刷新
- smarty处理页面缓存
- 系统缓存全解析3:页面局部缓存
- 六、smarty-缓存控制前的页面静态化原理
- Smarty局部缓存的几种方法简介
- ehcache-03 : ehcache实现页面整体缓存和页面局部缓存
- 【转】系统缓存全解析二:动态缓存(2)-页面局部缓存的两种方式
- SMARTY局部缓存
- smarty局部缓存
- 三.页面局部缓存