Skip to content

Commit

Permalink
Merge pull request #20 from jhedstrom/improve-loading-performance
Browse files Browse the repository at this point in the history
Improve performance of `findMatchingRedirect()`.
  • Loading branch information
Berdir committed Apr 17, 2015
2 parents ccfdb01 + 07d399e commit 0a78d52
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 3 additions & 1 deletion redirect.services.yml
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
24 changes: 17 additions & 7 deletions src/RedirectRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -41,17 +50,18 @@ 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.
$rid = $this->connection->query('SELECT rid FROM {redirect} WHERE hash IN (:hashes[])', [':hashes[]' => $hashes])->fetchField();

if (!empty($rid)) {
return $this->load($rid);
}

return NULL;
Expand Down

0 comments on commit 0a78d52

Please sign in to comment.