之前在看到可以关闭compile_check来减少模板编译文件的判断,这几天看了下 郭欣
的《构建高性能Web站点》,再次学到好东西。就是放弃smarty自带的缓存文件的判断,直接生成html文件,然后用stat来检查html文件的时间,用来判断是否缓存是否过期。代码类似:
????$cache_filename =
'cache/enjoy_'.$par.'.htm';
????$stat_info =
@stat($cache_filename);
????if($stat_info && $stat_info[9] >
time()-10800){
????????echo
file_get_contents($cache_filename);
????????exit();
????}
????......
????$html
=
$smarty->fetch($template_name,$par);
????file_put_contents($cache_filename,$html);
????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函数写了一个生成缓存文件名的一个函数。
function
get_smarty_cachefile($cache_dir,$template_name,$cache_id=null){
????$_compile_dir_sep
= '^';
????$_return =
$cache_dir.DIRECTORY_SEPARATOR;
????if(isset($cache_id))
{
????????$auto_id =
str_replace('%7C',$_compile_dir_sep,(urlencode($cache_id)));
????????$_return
.= $auto_id . $_compile_dir_sep;
????}??
????
????$_filename =
urlencode(basename($template_name));
????$_crc32 = sprintf('%08X',
crc32($template_name));
????$_crc32 = substr($_crc32, 0, 2) .
$_compile_dir_sep .
??????????????substr($_crc32, 0, 3) . $_compile_dir_sep .
$_crc32;
????$_return .= '%%' . $_crc32 . '%%' . $_filename;
????return
$_return;
}
这个函数,未考虑缓存目录分级,如果有使用缓存目录分级$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性能的提升了。