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

caching for classes name / id mapping in attempt to reduce db queries #15

Open
wants to merge 4 commits into
base: 10.6
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions models/DataObject/ClassDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ public static function cleanupForExport(&$data)
*/
private function exists()
{
// cache for class definitions needs to be cleared here to avoid wrong exists but still using the cache
Cache::clearTags(['ClassDefinitionDao']);
$name = $this->getDao()->getNameById($this->getId());

return is_string($name);
Expand Down
37 changes: 33 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,26 @@ 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.
*
* @param bool $skipCache
* @return array
* @see self::save()
*
*/
protected function getClassNameIdMap(bool $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'], null, 0, true);
}
return $mapping;
}

/**
* @param string $id
*
Expand All @@ -48,9 +70,8 @@ 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;
}
} catch (\Exception $e) {
}
Expand All @@ -71,7 +92,11 @@ 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;
}

}
} catch (\Exception $e) {
}
Expand All @@ -97,6 +122,8 @@ public function save($isUpdate = true)
}

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

/**
Expand Down Expand Up @@ -300,6 +327,8 @@ public function delete()

// clean slug table
DataObject\Data\UrlSlug::handleClassDeleted($this->model->getId());
// Update class name / id mapping in cache.
$this->getClassNameIdMap(true);
}

/**
Expand Down