免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1994 | 回复: 0
打印 上一主题 下一主题

smarty缓存终极优化测试 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-09-26 21:57 |只看该作者 |倒序浏览
smarty缓存终极优化测试



之前在看到可以关闭compile_check来减少模板编译文件的判断,这几天看了下 郭欣 的《构建高性能Web站点》,再次学到好东西。就是放弃smarty自带的缓存文件的判断,直接生成html文件,然后用stat来检查html文件的时间,用来判断是否缓存是否过期。代码类似:

  1.     $cache_filename = 'cache/enjoy_'.$par.'.htm';
  2.     $stat_info = @stat($cache_filename);
  3.     if($stat_info && $stat_info[9] > time()-10800){
  4.         echo file_get_contents($cache_filename);
  5.         exit();
  6.     }
  7.     ......
  8.     $html = $smarty->fetch($template_name,$par);
  9.     file_put_contents($cache_filename,$html);
  10.     echo $html;
复制代码
用本机的程序测试了下,本机因为是测试环境,没有装apc。

ab.exe -n 100 -c 10  http://www.itokit.com/

没用使用smarty缓存,就1句速度很快的SQL查询语句
Document Path:          /
Document Length:        16787 bytes

Time taken for tests:   1.984 seconds
Total transferred:      1697100 bytes
HTML transferred:       1678700 bytes
Requests per second:    50.39 [#/sec] (mean)
Time per request:       198.438 [ms] (mean)
Time per request:       19.844 [ms] (mean, across all concurrent requests)
Transfer rate:          835.19 [Kbytes/sec] received

使用smarty缓存后:
Time taken for tests:   1.891 seconds
Total transferred:      1697100 bytes
HTML transferred:       1678700 bytes
Requests per second:    52.89 [#/sec] (mean)
Time per request:       189.063 [ms] (mean)
Time per request:       18.906 [ms] (mean, across all
Transfer rate:          876.60 [Kbytes/sec] received
没多大提高。

改造之后,但仍然先构造了smarty对象
Time taken for tests:   0.688 seconds
Total transferred:      1697100 bytes
HTML transferred:       1678700 bytes
Requests per second:    145.45 [#/sec] (mean)
Time per request:       68.750 [ms] (mean)
Time per request:       6.875 [ms] (mean, across all concurrent requests)
Transfer rate:          2410.65 [Kbytes/sec] received

显著提高啊!
把加载smarty的语句放在判断后:

Time taken for tests:   0.391 seconds
Total transferred:      1697100 bytes
HTML transferred:       1678700 bytes
Requests per second:    256.00 [#/sec] (mean)
Time per request:       39.063 [ms] (mean)
Time per request:       3.906 [ms] (mean, across all concurrent requests)
Transfer rate:          4242.75 [Kbytes/sec] received
再次大幅提高!


如果自定义了缓存文件名称,那么删除缓存文件就得另外写一个,不能使用smarty自带的。如果要使用smarty自带的删除功能,那么缓存文件就要使用smarty的方式来命名。如模板文件为index.html,那么生成的缓存文件为:%%77^774^774BE9C9%%index.html,如果加了hx这个$cache_id,那么缓存文件为:hx^%%77^774^774BE9C9%%index.html。这是怎么生成的呢?我根据smarty核心程序中的_get_auto_id和_get_auto_filename函数写了一个生成缓存文件名的一个函数。

  1. function get_smarty_cachefile($cache_dir,$template_name,$cache_id=null){
  2.     $_compile_dir_sep = '^';
  3.     $_return = $cache_dir.DIRECTORY_SEPARATOR;
  4.     if(isset($cache_id)) {
  5.         $auto_id = str_replace('%7C',$_compile_dir_sep,(urlencode($cache_id)));
  6.         $_return .= $auto_id . $_compile_dir_sep;
  7.     }  
  8.    
  9.     $_filename = urlencode(basename($template_name));
  10.     $_crc32 = sprintf('%08X', crc32($template_name));
  11.     $_crc32 = substr($_crc32, 0, 2) . $_compile_dir_sep .
  12.               substr($_crc32, 0, 3) . $_compile_dir_sep . $_crc32;
  13.     $_return .= '%%' . $_crc32 . '%%' . $_filename;
  14.     return $_return;
  15. }
复制代码
这个函数,未考虑缓存目录分级,如果有使用缓存目录分级$smarty->use_sub_dirs=true,那么只需要把$_compile_dir_sep = '^';改为$_compile_dir_sep =DIRECTORY_SEPARATOR即可。
如上例:            
$cache_filename = get_smarty_cachefile('cache',$template_name,'hx');
这样就可以使用$smarty->clear_cache($template_name, 'hx')来删除这个缓存文件。

这样,只需要更改判断是否有缓存的代码,尽量把加载smarty的代码和初始化smarty放到判断之后,其他的不用考虑,就可以享受smarty性能的提升了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP