Merge branch 'optim'

This commit is contained in:
Manuel Raynaud
2014-01-27 14:25:21 +01:00
2 changed files with 72 additions and 50 deletions

View File

@@ -67,6 +67,9 @@ abstract class BaseLoop
protected $timestampable = false; protected $timestampable = false;
protected $versionable = false; protected $versionable = false;
private static $cacheLoopResult = array();
private static $cacheCount = array();
/** /**
* Create a new Loop * Create a new Loop
* *
@@ -335,71 +338,79 @@ abstract class BaseLoop
public function count() public function count()
{ {
$count = 0; $hash = $this->args->getHash();
if ($this instanceof PropelSearchLoopInterface) { if(false === array_key_exists($hash, self::$cacheCount))
$searchModelCriteria = $this->buildModelCriteria(); {
if (null === $searchModelCriteria) { $count = 0;
$count = 0; if ($this instanceof PropelSearchLoopInterface) {
} else { $searchModelCriteria = $this->buildModelCriteria();
$count = $searchModelCriteria->count(); if (null === $searchModelCriteria) {
} $count = 0;
} elseif ($this instanceof ArraySearchLoopInterface) { } else {
$searchArray = $this->buildArray(); $count = $searchModelCriteria->count();
if (null === $searchArray) { }
$count = 0; } elseif ($this instanceof ArraySearchLoopInterface) {
} else { $searchArray = $this->buildArray();
$count = count($searchArray); if (null === $searchArray) {
$count = 0;
} else {
$count = count($searchArray);
}
} }
self::$cacheCount[$hash] = $count;
} }
return $count; return self::$cacheCount[$hash];
} }
/** /**
* @param $pagination * @param $pagination
* @return LoopResult * @return LoopResult
*/ */
public function exec(&$pagination, $count = false) public function exec(&$pagination)
{ {
if ($this instanceof PropelSearchLoopInterface) { $hash = $this->args->getHash();
$searchModelCriteria = $this->buildModelCriteria(); if(false === array_key_exists($hash, self::$cacheLoopResult))
if (null === $searchModelCriteria) { {
$results = array(); if ($this instanceof PropelSearchLoopInterface) {
} else { $searchModelCriteria = $this->buildModelCriteria();
$results = $this->search( if (null === $searchModelCriteria) {
$searchModelCriteria, $results = array();
$pagination } else {
); $results = $this->search(
$searchModelCriteria,
$pagination
);
}
} elseif ($this instanceof ArraySearchLoopInterface) {
$searchArray = $this->buildArray();
if (null === $searchArray) {
$results = array();
} else {
$results = $this->searchArray(
$searchArray,
$pagination
);
}
} }
} elseif ($this instanceof ArraySearchLoopInterface) {
$searchArray = $this->buildArray(); $loopResult = new LoopResult($results);
if (null === $searchArray) {
$results = array(); if (true === $this->countable) {
} else { $loopResult->setCountable();
$results = $this->searchArray(
$searchArray,
$pagination
);
} }
if (true === $this->timestampable) {
$loopResult->setTimestamped();
}
if (true === $this->versionable) {
$loopResult->setVersioned();
}
self::$cacheLoopResult[$hash] = $this->parseResults($loopResult);
} }
if ($count) { return self::$cacheLoopResult[$hash];
return $results ? count($results) : 0;
}
$loopResult = new LoopResult($results);
if (true === $this->countable) {
$loopResult->setCountable();
}
if (true === $this->timestampable) {
$loopResult->setTimestamped();
}
if (true === $this->versionable) {
$loopResult->setVersioned();
}
return $this->parseResults($loopResult);
} }
protected function checkInterface() protected function checkInterface()

View File

@@ -144,4 +144,15 @@ class ArgumentCollection implements \Iterator
{ {
reset($this->arguments); reset($this->arguments);
} }
public function getHash()
{
$arguments = $this->arguments;
if (array_key_exists('name', $arguments)) {
unset($arguments['name']);
}
return sha1(serialize($arguments));
}
} }