From bf5c36fdb2be5ca13de075b0e7f624d18a87d691 Mon Sep 17 00:00:00 2001 From: Tim Higgs Date: Mon, 7 Mar 2016 16:02:33 +1300 Subject: [PATCH 1/4] Minor update to Redirect class constructor to format the 'from' and 'to' URLs before setting the local properties. --- src/Redirect.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Redirect.php b/src/Redirect.php index 3659d1a..a4dcfb6 100644 --- a/src/Redirect.php +++ b/src/Redirect.php @@ -20,8 +20,8 @@ class Redirect */ public function __construct($from, $to, $statusCode = 301) { - $this->from = $from; - $this->to = $to; + $this->from = self::formatUrl($from); + $this->to = self::formatUrl($to); $this->statusCode = $statusCode; } From cc2837ffe5b2e150c816fd0ddc4e96965e140f0a Mon Sep 17 00:00:00 2001 From: Tim Higgs Date: Mon, 7 Mar 2016 16:04:33 +1300 Subject: [PATCH 2/4] Minor update to Redirect class to improve the readability of return check of the matchI() method and also change the formatUrl() method to be static so it can be called more easily from elsewhere. --- src/Redirect.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Redirect.php b/src/Redirect.php index a4dcfb6..a483cd9 100644 --- a/src/Redirect.php +++ b/src/Redirect.php @@ -55,16 +55,16 @@ public function getStatusCode() */ public function match($url) { - $from = $this->formatUrl($this->from); - return $from === $this->formatUrl($url) - && $from !== $this->formatUrl($this->to); + $from = self::formatUrl($this->from); + + return (($from === self::formatUrl($url)) && ($from !== self::formatUrl($this->to))); } /** * @param string $url * @return string */ - public function formatUrl($url) + public static function formatUrl($url) { return trim( strtolower($url), From cdebf9757457288dea3743de1f359be280567e6a Mon Sep 17 00:00:00 2001 From: Tim Higgs Date: Wed, 9 Mar 2016 15:54:21 +1300 Subject: [PATCH 3/4] Revert "Minor update to Redirect class constructor to format the 'from' and 'to' URLs before setting the local properties." This reverts commit bf5c36fdb2be5ca13de075b0e7f624d18a87d691. --- src/Redirect.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Redirect.php b/src/Redirect.php index a483cd9..db0a5c3 100644 --- a/src/Redirect.php +++ b/src/Redirect.php @@ -20,8 +20,8 @@ class Redirect */ public function __construct($from, $to, $statusCode = 301) { - $this->from = self::formatUrl($from); - $this->to = self::formatUrl($to); + $this->from = $from; + $this->to = $to; $this->statusCode = $statusCode; } From 62aff525b5406c7db0205f0655095e0225c25b54 Mon Sep 17 00:00:00 2001 From: Tim Higgs Date: Mon, 14 Mar 2016 11:43:05 +1300 Subject: [PATCH 4/4] Update to Redirector class and DataListDataSource class to change redirect cache to be an associative array to improve performance. In order to improve performance for redirect lookup, instead of a standard array being generated an associative array (keyed on the 'from' URL) is generated which allows a direct check rather than needing to iterate through all values which can be inefficient, especially when there is a large number (and also considering this would be done on each page request). --- src/DataSource/DataListDataSource.php | 15 ++++++++++++++- src/Redirector.php | 9 ++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/DataSource/DataListDataSource.php b/src/DataSource/DataListDataSource.php index c8e5e50..8e23f37 100644 --- a/src/DataSource/DataListDataSource.php +++ b/src/DataSource/DataListDataSource.php @@ -3,6 +3,7 @@ namespace Heyday\Redirects\DataSource; use Heyday\Redirects\CacheableDataSourceInterface; +use Heyday\Redirects\Redirect; use Heyday\Redirects\TransformerInterface; class DataListDataSource implements CacheableDataSourceInterface @@ -24,11 +25,23 @@ public function __construct(\DataList $list, TransformerInterface $transformer) } /** + * In order to improve performance for a larger number of redirects, instead of a standard array being generated + * an associative array (keyed on the 'from' URL) is generated which allows a direct check rather than needing + * to iterate through a large list (especially considering this would be done on each page request). + * * @return \Heyday\Redirects\Redirect[] */ public function get() { - return array_map([$this->transformer, 'transform'], $this->list->toArray()); + $data = array(); + + /* @var \Heyday\Redirects\Redirect $redirect */ + foreach (array_map([$this->transformer, 'transform'], $this->list->toArray()) as $redirect) { + // Format the URL so it will match the SS_HTTPRequest request URL + $data[Redirect::formatUrl($redirect->getFrom())] = $redirect; + } + + return $data; } /** diff --git a/src/Redirector.php b/src/Redirector.php index d84feef..6128dd5 100644 --- a/src/Redirector.php +++ b/src/Redirector.php @@ -21,14 +21,17 @@ public function __construct(DataSourceInterface $dataSource) */ public function getRedirectForRequest(\SS_HTTPRequest $request) { - $url = $request->getURL(); + // Format the URL as the key will have been formatted + $url = Redirect::formatUrl($request->getURL()); + $dataSource = $this->dataSource->get(); - foreach ($this->dataSource->get() as $redirect) { + // Check if there's a key for the URL + if (isset($dataSource[$url])) { + $redirect = $dataSource[$url]; if ($redirect->match($url)) { return $redirect; } } - return false; }