关于Smarty中的缓存(Cache)
Smarty中cache的文件路径根据下面的方法计算得出:
$cache_id = str_replace('|','^'); $cache_file = "$_cache_dir/$cache_id^$compile_id^$tpl_uid.$tpl_name.php"; $cache_file = preg_replace('/\^+/', ($smarty->use_sub_dirs?'/':'^'), $cache_file);
下面例子中的两个display()虽然$cache_id不同,却会得到相同的结果
$cache_id = 'group|我'; $smarty->display('app.tpl', $cache_id); $cache_id = 'group|你'; $smarty->display('app.tpl', $cache_id);
原因是Smarty的内部对$cache_id进行了替换操作,两个不同的$cache_id替换后的结果却可能是相同的。Smarty 3中的替换方法和相应的解决办法如下:
// Smarty内部对 $cache_id的替换操作。 $smarty->cache_id = preg_replace('/[^\w\|]+/','_', $smarty->cache_id); //解决办法