smarty页面局部缓存

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

  1. require('Smarty.class.php');
  2. $smarty = new Smarty;
  3. $smarty->caching = 1;
  4. if(!$smarty->is_cached('index.tpl')) {
  5.     // No cache available, do variable assignments here.
  6.     $contents = get_database_contents();
  7.     $smarty->assign($contents);
  8. }
  9. $smarty->display('index.tpl');

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

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

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

  1. index.php:
  2.  
  3. <?php
  4. $smarty->caching = 1;
  5.  
  6. function smarty_block_dynamic($param, $content, &$smarty) {
  7.     return $content;
  8. }
  9. $smarty->register_block('dynamic', 'smarty_block_dynamic', false);
  10.  
  11. $smarty->display('index.tpl');
  12. ?>
  13.  
  14.  
  15. index.tpl is:
  16.  
  17. Page created: {'0'|date_format:'%D %H:%M:%S'}
  18.  
  19. {dynamic}
  20.  
  21. Now is: {'0'|date_format:'%D %H:%M:%S'}
  22.  
  23. ... do other stuff ...
  24.  
  25. {/dynamic}

官方介绍

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值