Smarty默认使用自带的文件系统储存Cache文件。其默认的Cache文件路径可以表示如下:
if($smarty->use_sub_dirs){ $tpl_uid = substr($tpl_uid,0,2).'/' .substr($tpl_uid,2,2).'/' .substr($tpl_uid,4,2).'/' .$tpl_uid; } $cache_id = str_replace('|','^',$cache_id); $cache_file = $_cache_dir.'/'.$cache_id.'^'.$compile_id.'^'.$tpl_uid.$tpl_name.php; $cache_file = preg_replace('/\^+/', ($smarty->use_sub_dirs?'/':'^'), $cache_file);
这里主要的问题是当$use_sub_dirs
为真时,会造成过深的目录嵌套,平白增加了系统的文件数目(目录也是文件)。
多数情况下,是一个模板文件对应多个不同的视图(View),即多个不同的$cache_id
。而不同的视图往往已经体现在了URL上。所以一种很自然的思路,就是用URL中的相应部分作为$cache_id
。这样一来,一个cache_id则对应很少的模板——多数时候可能就一个模板。
在这种思路下,比较合理的用法应该是通过$cache_id
体现目录层次就够了。
复制文件"smarty_internal_cacheresource_file.php"到Smarty的plugins目录中,并重命名为"cacheresource.sfile.php",作适当修改以体现上述思路。
$cache_id = str_replace('|', ($smarty->use_sub_dirs?'/':'^'), $cache_id); $cache_file = $_cache_dir.'/'.$cache_id.'^'.$compile_id.'^'.$tpl_uid.$tpl_name.php;
指定使用新的CacheResource可以用语句$smarty->caching_type='sfile';
来实现。