From 8d0daaae2223510d5ee8c661306e8968648f68a0 Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Wed, 15 Apr 2015 12:41:59 -0700 Subject: [PATCH 1/2] Improve performance of `findMatchingRedirect()`. --- src/RedirectRepository.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/RedirectRepository.php b/src/RedirectRepository.php index 41f1dde..2428be7 100644 --- a/src/RedirectRepository.php +++ b/src/RedirectRepository.php @@ -41,17 +41,22 @@ public function __construct(EntityManagerInterface $manager) { * @return \Drupal\redirect\Entity\Redirect * The matched redirect entity. */ - public function findMatchingRedirect($source_path, array $query = array(), $language = Language::LANGCODE_NOT_SPECIFIED) { + public function findMatchingRedirect($source_path, array $query = [], $language = Language::LANGCODE_NOT_SPECIFIED) { - $hashes = array(Redirect::generateHash($source_path, $query, $language)); + $hashes = [Redirect::generateHash($source_path, $query, $language)]; if ($language != Language::LANGCODE_NOT_SPECIFIED) { $hashes[] = Redirect::generateHash($source_path, $query, Language::LANGCODE_NOT_SPECIFIED); } - // Load redirects by hash. - $redirects = $this->manager->getStorage('redirect')->loadByProperties(array('hash' => $hashes)); - if (!empty($redirects)) { - return array_shift($redirects); + // Load redirects by hash. A direct query is used to improve performance. + if (count($hashes) > 1) { + $rid = db_query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[])', [':hashes[]' => $hashes])->fetchField(); + } + else { + $rid = db_query('SELECT rid FROM {redirect} WHERE hash = :hash', [':hash' => reset($hashes)])->fetchField(); + } + if (!empty($rid)) { + return Redirect::load($rid); } return NULL; From 07d399e865b94f42813002ced761950301d09f58 Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Thu, 16 Apr 2015 09:17:57 -0700 Subject: [PATCH 2/2] Inject the database connection to the RedirectRepository. --- redirect.services.yml | 4 +++- src/RedirectRepository.php | 21 +++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/redirect.services.yml b/redirect.services.yml index 5def92d..f7dbb57 100644 --- a/redirect.services.yml +++ b/redirect.services.yml @@ -1,7 +1,9 @@ services: redirect.repository: class: Drupal\redirect\RedirectRepository - arguments: ['@entity.manager'] + arguments: ['@entity.manager', '@database'] + tags: + - { name: backend_overridable } redirect.checker: class: Drupal\redirect\RedirectChecker arguments: ['@config.factory', '@state', '@flood'] diff --git a/src/RedirectRepository.php b/src/RedirectRepository.php index 2428be7..2d44b5a 100644 --- a/src/RedirectRepository.php +++ b/src/RedirectRepository.php @@ -7,6 +7,7 @@ namespace Drupal\redirect; +use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Language\Language; use Drupal\redirect\Entity\Redirect; @@ -18,14 +19,22 @@ class RedirectRepository { */ protected $manager; + /** + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + /** * Constructs a \Drupal\redirect\EventSubscriber\RedirectRequestSubscriber object. * * @param \Drupal\Core\Entity\EntityManagerInterface $manager * The entity manager service. + * @param \Drupal\Core\Database\Connection $connection + * The database connection. */ - public function __construct(EntityManagerInterface $manager) { + public function __construct(EntityManagerInterface $manager, Connection $connection) { $this->manager = $manager; + $this->connection = $connection; } /** @@ -49,14 +58,10 @@ public function findMatchingRedirect($source_path, array $query = [], $language } // Load redirects by hash. A direct query is used to improve performance. - if (count($hashes) > 1) { - $rid = db_query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[])', [':hashes[]' => $hashes])->fetchField(); - } - else { - $rid = db_query('SELECT rid FROM {redirect} WHERE hash = :hash', [':hash' => reset($hashes)])->fetchField(); - } + $rid = $this->connection->query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[])', [':hashes[]' => $hashes])->fetchField(); + if (!empty($rid)) { - return Redirect::load($rid); + return $this->load($rid); } return NULL;