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

chore: Added caching for classes name / id mapping in attempt to reduce db queries. #7

Open
wants to merge 12 commits into
base: 10.6
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject;

use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController;
use Pimcore\Cache;
use Pimcore\Controller\KernelControllerEventInterface;
use Pimcore\Db;
use Pimcore\Event\AdminEvents;
Expand Down Expand Up @@ -301,6 +302,7 @@ public function addAction(Request $request)
$class->setId($classId);

$class->save(true);
Cache::clearTags(['ClassDefinitionDao']);

return $this->adminJson(['success' => true, 'id' => $class->getId()]);
}
Expand Down Expand Up @@ -351,6 +353,7 @@ public function deleteAction(Request $request)
$class = DataObject\ClassDefinition::getById($request->get('id'));
if ($class) {
$class->delete();
Cache::clearTags(['ClassDefinitionDao']);
}

return new Response();
Expand Down
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