From 64e12fff323cfd6ec3768b6d267c6771b1fa459d Mon Sep 17 00:00:00 2001 From: Henning Leutz <leutz@pcsg.de> Date: Thu, 27 Aug 2020 13:27:03 +0200 Subject: [PATCH] refactor: pcsg/kunden/bow-modellhandel-template#24 - brick cache --- src/QUI/Bricks/Brick.php | 52 ++++++++++++++++++++++++++++++++++++-- src/QUI/Bricks/Manager.php | 14 ++++++++++ 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/QUI/Bricks/Brick.php b/src/QUI/Bricks/Brick.php index b8e82e2..6772b15 100644 --- a/src/QUI/Bricks/Brick.php +++ b/src/QUI/Bricks/Brick.php @@ -60,6 +60,11 @@ class Brick extends QUI\QDOM */ protected $cssClasses = []; + /** + * @var string + */ + protected $hash; + /** * Constructor * @@ -172,6 +177,8 @@ public function __construct($params = []) $this->customfields = $customfields; } } + + $this->hash = $this->createBrickHash(); } /** @@ -230,6 +237,31 @@ public function check() return $this; } + /** + * Create a unique brick hash + * which is created from the brick data + * + * @return string + */ + protected function createBrickHash() + { + $attributes = $this->getAttributes(); + $hashParams = []; + + foreach ($attributes as $name => $value) { + if (\is_object($value)) { + continue; + } + + $hashParams[$name] = \serialize($value); + } + + $hash = \serialize($hashParams); + $hash = \md5($hash); + + return $hash; + } + /** * Return the HTML of the Brick * @@ -239,6 +271,16 @@ public function check() */ public function create() { + $cacheName = Manager::getBrickCacheNamespace() + .\md5($this->getType()) + .'/' + .$this->hash; + + try { + return QUI\Cache\Manager::get($cacheName); + } catch (QUI\Exception $Exception) { + } + if ($this->getAttribute('type') == 'content') { $_classes = [ 'brick-'.$this->id @@ -292,7 +334,10 @@ public function create() 'classesStr' => $classesStr ]); - return $Engine->fetch(\dirname(__FILE__).'/Brick.html'); + $result = $Engine->fetch(\dirname(__FILE__).'/Brick.html'); + QUI\Cache\Manager::set($cacheName, $result); + + return $result; } $Control = $this->getControl(); @@ -324,7 +369,10 @@ public function create() $Control->addCSSClass($cssClass); } - return $Control->create(); + $result = $Control->create(); + QUI\Cache\Manager::set($cacheName, $result); + + return $result; } /** diff --git a/src/QUI/Bricks/Manager.php b/src/QUI/Bricks/Manager.php index cc19dbb..9a9fe6e 100644 --- a/src/QUI/Bricks/Manager.php +++ b/src/QUI/Bricks/Manager.php @@ -99,6 +99,16 @@ public static function getUIDTable() return QUI::getDBTableName(self::TABLE_UID); } + /** + * Return the long time cache namespace + * + * @return string + */ + public static function getBrickCacheNamespace() + { + return 'quiqqer/package/quiqqer/bricks/'; + } + /** * Creates a new brick for the project * @@ -1025,6 +1035,10 @@ public function saveBrick($brickId, array $brickData) ); QUI\Cache\Manager::clear($cache); + + QUI\Cache\Manager::clear( + self::getBrickCacheNamespace().\md5($type) + ); } /** -- GitLab