From 4e1d5fd1022e30545b0ab5bc9dc0ab00c11f075e Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Thu, 2 Feb 2023 13:16:14 +0100 Subject: [PATCH 1/2] chore: Added caching for classes name / id mapping in attempt to reduce db queries. --- models/DataObject/ClassDefinition/Dao.php | 39 ++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/models/DataObject/ClassDefinition/Dao.php b/models/DataObject/ClassDefinition/Dao.php index 853accab0be..ca80869adf7 100644 --- a/models/DataObject/ClassDefinition/Dao.php +++ b/models/DataObject/ClassDefinition/Dao.php @@ -15,6 +15,7 @@ namespace Pimcore\Model\DataObject\ClassDefinition; +use Pimcore\Cache; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -39,6 +40,25 @@ class Dao extends Model\Dao\AbstractDao */ protected $tableDefinitions = null; + /** + * Helper to minimize db queries used when looking up classes id / name. + * + * Mapping is actively updated as soon as a class is saved. + * + * @see self::save() + * + * @return array + */ + protected function getClassNameIdMap($skipCache = false): array + { + static $mapping; + if ($skipCache || (!isset($mapping) && !is_array(($mapping = Cache::load(md5(__METHOD__)))))) { + $mapping = $this->db->fetchPairs('SELECT id, name FROM classes'); + Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']); + } + return $mapping; + } + /** * @param string $id * @@ -48,9 +68,12 @@ public function getNameById($id) { try { if (!empty($id)) { - if ($name = $this->db->fetchOne('SELECT name FROM classes WHERE id = ?', [$id])) { - return $name; - } + $mapping = $this->getClassNameIdMap(); + return $mapping[$id] ?? null; + +// if ($name = $this->db->fetchOne('SELECT name FROM classes WHERE id = ?', [$id])) { +// return $name; +// } } } catch (\Exception $e) { } @@ -71,7 +94,12 @@ public function getIdByName($name) try { if (!empty($name)) { - $id = $this->db->fetchOne('SELECT id FROM classes WHERE name = ?', [$name]); + $mapping = $this->getClassNameIdMap(); + if (($v = array_search($name, $mapping, true)) !== false) { + $id = $v; + } +// $id = $this->db->fetchOne('SELECT id FROM classes WHERE name = ?', [$name]); + } } catch (\Exception $e) { } @@ -97,6 +125,9 @@ public function save($isUpdate = true) } $this->update(); + + // Update class name / id mapping in cache. + $this->getClassNameIdMap(true); } /** From 6a140aabe3ae841a37452ccde87276be93533a4d Mon Sep 17 00:00:00 2001 From: Tomasz Szustak Date: Thu, 2 Feb 2023 14:10:35 +0100 Subject: [PATCH 2/2] chore: Changed code to use Helper::fetchPairs --- models/DataObject/ClassDefinition/Dao.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/DataObject/ClassDefinition/Dao.php b/models/DataObject/ClassDefinition/Dao.php index ca80869adf7..081fe391b8e 100644 --- a/models/DataObject/ClassDefinition/Dao.php +++ b/models/DataObject/ClassDefinition/Dao.php @@ -16,6 +16,7 @@ namespace Pimcore\Model\DataObject\ClassDefinition; use Pimcore\Cache; +use Pimcore\Db\Helper; use Pimcore\Logger; use Pimcore\Model; use Pimcore\Model\DataObject; @@ -53,7 +54,7 @@ protected function getClassNameIdMap($skipCache = false): array { static $mapping; if ($skipCache || (!isset($mapping) && !is_array(($mapping = Cache::load(md5(__METHOD__)))))) { - $mapping = $this->db->fetchPairs('SELECT id, name FROM classes'); + $mapping = Helper::fetchPairs($this->db, 'SELECT id, name FROM classes'); Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']); } return $mapping;