一些Smarty相关的Tips

@ 2010-12-25 23:59:11
标签:

    关于Smarty中的缓存(Cache)

    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);
    

    不同的$cache_id却得到同样的结果

    下面例子中的两个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);
    
    //解决办法
    
    
    标签:

      分享到:
      comments powered by Disqus

      24/26ms