From 2411f5ffa3144f0ff70559df173fc03d6f7fe7de Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 24 Jan 2014 12:09:24 +0100 Subject: [PATCH 1/3] cache ParseResult for not calculating twice the same loop --- .../Thelia/Core/Template/Element/BaseLoop.php | 77 +++++++++++-------- .../Loop/Argument/ArgumentCollection.php | 11 +++ 2 files changed, 54 insertions(+), 34 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 80220474b..32421430a 100644 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -67,6 +67,8 @@ abstract class BaseLoop protected $timestampable = false; protected $versionable = false; + private static $cache = array(); + /** * Create a new Loop * @@ -361,45 +363,52 @@ abstract class BaseLoop */ public function exec(&$pagination, $count = false) { - if ($this instanceof PropelSearchLoopInterface) { - $searchModelCriteria = $this->buildModelCriteria(); - if (null === $searchModelCriteria) { - $results = array(); - } else { - $results = $this->search( - $searchModelCriteria, - $pagination - ); + $hash = $this->args->getHash(); + if(false === array_key_exists($hash, self::$cache)) + { + if ($this instanceof PropelSearchLoopInterface) { + $searchModelCriteria = $this->buildModelCriteria(); + if (null === $searchModelCriteria) { + $results = array(); + } 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 (null === $searchArray) { - $results = array(); - } else { - $results = $this->searchArray( - $searchArray, - $pagination - ); + + if ($count) { + 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(); + } + + self::$cache[$hash] = $this->parseResults($loopResult); } - if ($count) { - return $results ? count($results) : 0; - } + return self::$cache[$hash]; - $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() diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php index dc851098b..7f5a604c5 100644 --- a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php +++ b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php @@ -144,4 +144,15 @@ class ArgumentCollection implements \Iterator { reset($this->arguments); } + + public function getHash() + { + $arguments = $this->arguments; + + if (array_key_exists('name', $arguments)) { + unset($arguments['name']); + } + + return sha1(serialize($arguments)); + } } From 5b9b03970e5f9a9317834d91aecfdecce529588a Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 24 Jan 2014 14:09:54 +0100 Subject: [PATCH 2/3] count parameter is not needed anymore in BaseLoop::exec --- core/lib/Thelia/Core/Template/Element/BaseLoop.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 32421430a..7b409a0c1 100644 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -361,7 +361,7 @@ abstract class BaseLoop * @param $pagination * @return LoopResult */ - public function exec(&$pagination, $count = false) + public function exec(&$pagination) { $hash = $this->args->getHash(); if(false === array_key_exists($hash, self::$cache)) @@ -388,10 +388,6 @@ abstract class BaseLoop } } - if ($count) { - return $results ? count($results) : 0; - } - $loopResult = new LoopResult($results); if (true === $this->countable) { From 54b95fccd5ee39122aa38aaa94e19f521f3d0a40 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 24 Jan 2014 14:22:06 +0100 Subject: [PATCH 3/3] create cache for BaseLoop::count method --- .../Thelia/Core/Template/Element/BaseLoop.php | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index 7b409a0c1..2aa5e874f 100644 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -67,7 +67,8 @@ abstract class BaseLoop protected $timestampable = false; protected $versionable = false; - private static $cache = array(); + private static $cacheLoopResult = array(); + private static $cacheCount = array(); /** * Create a new Loop @@ -337,24 +338,29 @@ abstract class BaseLoop public function count() { - $count = 0; - if ($this instanceof PropelSearchLoopInterface) { - $searchModelCriteria = $this->buildModelCriteria(); - if (null === $searchModelCriteria) { - $count = 0; - } else { - $count = $searchModelCriteria->count(); - } - } elseif ($this instanceof ArraySearchLoopInterface) { - $searchArray = $this->buildArray(); - if (null === $searchArray) { - $count = 0; - } else { - $count = count($searchArray); + $hash = $this->args->getHash(); + if(false === array_key_exists($hash, self::$cacheCount)) + { + $count = 0; + if ($this instanceof PropelSearchLoopInterface) { + $searchModelCriteria = $this->buildModelCriteria(); + if (null === $searchModelCriteria) { + $count = 0; + } else { + $count = $searchModelCriteria->count(); + } + } elseif ($this instanceof ArraySearchLoopInterface) { + $searchArray = $this->buildArray(); + if (null === $searchArray) { + $count = 0; + } else { + $count = count($searchArray); + } } + self::$cacheCount[$hash] = $count; } - return $count; + return self::$cacheCount[$hash]; } /** @@ -364,7 +370,7 @@ abstract class BaseLoop public function exec(&$pagination) { $hash = $this->args->getHash(); - if(false === array_key_exists($hash, self::$cache)) + if(false === array_key_exists($hash, self::$cacheLoopResult)) { if ($this instanceof PropelSearchLoopInterface) { $searchModelCriteria = $this->buildModelCriteria(); @@ -400,10 +406,10 @@ abstract class BaseLoop $loopResult->setVersioned(); } - self::$cache[$hash] = $this->parseResults($loopResult); + self::$cacheLoopResult[$hash] = $this->parseResults($loopResult); } - return self::$cache[$hash]; + return self::$cacheLoopResult[$hash]; }