浏览代码

[Templating] optimized the cache directory of CacheLoader (especially if there is a large number of templates)

Fabien Potencier 15 年之前
父节点
当前提交
94f6e78ce4
共有 1 个文件被更改,包括 9 次插入6 次删除
  1. 9 6
      src/Symfony/Components/Templating/Loader/CacheLoader.php

+ 9 - 6
src/Symfony/Components/Templating/Loader/CacheLoader.php

@@ -41,11 +41,6 @@ class CacheLoader extends Loader
     $this->loader = $loader;
     $this->dir = $dir;
 
-    if (!file_exists($dir))
-    {
-      mkdir($dir, 0777, true);
-    }
-
     parent::__construct();
   }
 
@@ -61,7 +56,10 @@ class CacheLoader extends Loader
   {
     $options = $this->mergeDefaultOptions($options);
 
-    $path = $this->dir.DIRECTORY_SEPARATOR.md5($template.serialize($options)).'.tpl';
+    $tmp = md5($template.serialize($options)).'.tpl';
+    $dir = $this->dir.DIRECTORY_SEPARATOR.substr($tmp, 0, 2);
+    $file = substr($tmp, 2);
+    $path = $dir.DIRECTORY_SEPARATOR.$file;
 
     if ($this->loader instanceof CompilableLoaderInterface)
     {
@@ -90,6 +88,11 @@ class CacheLoader extends Loader
       $content = $this->loader->compile($content);
     }
 
+    if (!file_exists($dir))
+    {
+      mkdir($dir, 0777, true);
+    }
+
     file_put_contents($path, $content);
 
     if (null !== $this->debugger)