Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

10.5 classes mapping cache reduce db queries fixed T2-1912 #16

Draft
wants to merge 2 commits into
base: 10.5
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions models/DataObject/ClassDefinition/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Pimcore\Model\DataObject\ClassDefinition;

use Pimcore\Cache;
use Pimcore\Db\Helper;
use Pimcore\Logger;
use Pimcore\Model;
use Pimcore\Model\DataObject;
Expand All @@ -39,6 +41,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 = Helper::fetchPairs($this->db, 'SELECT id, name FROM classes');
Cache::save($mapping, md5(__METHOD__), ['ClassDefinitionDao']);
}
return $mapping;
}

/**
* @param string $id
*
Expand All @@ -48,9 +69,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) {
}
Expand All @@ -71,7 +95,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) {
}
Expand All @@ -97,6 +126,9 @@ public function save($isUpdate = true)
}

$this->update();

// Update class name / id mapping in cache.
$this->getClassNameIdMap(true);
}

/**
Expand Down