cache ParseResult for not calculating twice the same loop
This commit is contained in:
@@ -67,6 +67,8 @@ abstract class BaseLoop
|
|||||||
protected $timestampable = false;
|
protected $timestampable = false;
|
||||||
protected $versionable = false;
|
protected $versionable = false;
|
||||||
|
|
||||||
|
private static $cache = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Loop
|
* Create a new Loop
|
||||||
*
|
*
|
||||||
@@ -361,45 +363,52 @@ abstract class BaseLoop
|
|||||||
*/
|
*/
|
||||||
public function exec(&$pagination, $count = false)
|
public function exec(&$pagination, $count = false)
|
||||||
{
|
{
|
||||||
if ($this instanceof PropelSearchLoopInterface) {
|
$hash = $this->args->getHash();
|
||||||
$searchModelCriteria = $this->buildModelCriteria();
|
if(false === array_key_exists($hash, self::$cache))
|
||||||
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();
|
if ($count) {
|
||||||
if (null === $searchArray) {
|
return $results ? count($results) : 0;
|
||||||
$results = array();
|
|
||||||
} else {
|
|
||||||
$results = $this->searchArray(
|
|
||||||
$searchArray,
|
|
||||||
$pagination
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$loopResult = new LoopResult($results);
|
||||||
|
|
||||||
|
if (true === $this->countable) {
|
||||||
|
$loopResult->setCountable();
|
||||||
|
}
|
||||||
|
if (true === $this->timestampable) {
|
||||||
|
$loopResult->setTimestamped();
|
||||||
|
}
|
||||||
|
if (true === $this->versionable) {
|
||||||
|
$loopResult->setVersioned();
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$cache[$hash] = $this->parseResults($loopResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($count) {
|
return self::$cache[$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()
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user