Permalink
Browse files

Merge pull request #127 from real-chocopanda/feat-sfPropelData-load-fast

Add sfFileCache for databaseMap with sfPropelData to load faster each tests
  • Loading branch information...
2 parents 4d5a98c + e0b1cff commit 342343c8a329534fd102f8506f55220968c63ec9 @willdurand willdurand committed Mar 28, 2012
Showing with 33 additions and 8 deletions.
  1. +33 −8 lib/addon/sfPropelData.class.php
@@ -26,6 +26,20 @@ class sfPropelData extends sfData
$formatter = null;
/**
+ * @var int The cache duration
+ */
+ protected $cacheKeepDuration;
+
+ public function __construct($cacheKeepDuration = 7200)
+ {
+ $this->cache = new sfFileCache(array(
+ 'cache_dir' => sfConfig::get('sf_cache_dir')
+ ));
+
+ $this->cacheKeepDuration = $cacheKeepDuration;
+ }
+
+ /**
* Initializes the sfPropelData instance.
*
* @param sfEventDispatcher $dispatcher A sfEventDispatcher instance
@@ -313,23 +327,34 @@ protected function doDeleteCurrentData($files)
}
/**
- * Loads all map builders.
+ * Loads all map builders for a given connection name
+ *
+ * Use sfFileCache to cache propel databaseMap
*
* @throws sfException If the class cannot be found
*/
protected function loadMapBuilders($connectionName)
{
- $dbMap = Propel::getDatabaseMap();
- $files = sfFinder::type('file')->name('*TableMap.php')->in(sfProjectConfiguration::getActive()->getModelDirs());
- foreach ($files as $file)
+ $cacheKey = 'dbMap-' . $connectionName;
+
+ if(!$dbMap = $this->cache->get($cacheKey))
{
- $omClass = basename($file, 'TableMap.php');
- if (class_exists($omClass) && is_subclass_of($omClass, 'BaseObject') && constant($omClass.'Peer::DATABASE_NAME') == $connectionName)
+ $dbMap = Propel::getDatabaseMap();
+ $files = sfFinder::type('file')->name('*TableMap.php')->in(sfProjectConfiguration::getActive()->getModelDirs());
+ foreach ($files as $file)
{
- $tableMapClass = basename($file, '.php');
- $dbMap->addTableFromMapClass($tableMapClass);
+ $omClass = basename($file, 'TableMap.php');
+ if (class_exists($omClass) && is_subclass_of($omClass, 'BaseObject') && constant($omClass.'Peer::DATABASE_NAME') == $connectionName)
+ {
+ $tableMapClass = basename($file, '.php');
+ $dbMap->addTableFromMapClass($tableMapClass);
+ }
}
+
+ $dbMap = serialize($dbMap);
+ $this->cache->set($cacheKey, $dbMap, $this->cacheKeepDuration);
}
+ Propel::setDatabaseMap($connectionName, unserialize($dbMap));
}
/**

0 comments on commit 342343c

Please sign in to comment.