mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Optimized loading dynamic redirects
- the code kept an array of IDs, and would check new entries against the values of this array - this algorithm is O(n^2) and became quite slow when the site had a lot of redirects - we can do away with this entirely, and just compute the keys of the redirects to get the IDs - this speeds up loading redirects by 3x or so
This commit is contained in:
parent
6dd821bd41
commit
bfdf2dd8bf
2 changed files with 1 additions and 10 deletions
|
@ -16,8 +16,6 @@ class DynamicRedirectManager {
|
||||||
|
|
||||||
/** @private */
|
/** @private */
|
||||||
this.router = express.Router();
|
this.router = express.Router();
|
||||||
/** @private @type {string[]} */
|
|
||||||
this.redirectIds = [];
|
|
||||||
/** @private @type {Object.<string, {fromRegex: RegExp, to: string, options: {permanent: boolean}}>} */
|
/** @private @type {Object.<string, {fromRegex: RegExp, to: string, options: {permanent: boolean}}>} */
|
||||||
this.redirects = {};
|
this.redirects = {};
|
||||||
|
|
||||||
|
@ -103,10 +101,6 @@ class DynamicRedirectManager {
|
||||||
|
|
||||||
const fromRegex = this.buildRegex(from);
|
const fromRegex = this.buildRegex(from);
|
||||||
const redirectId = from;
|
const redirectId = from;
|
||||||
|
|
||||||
if (!this.redirectIds.includes(redirectId)) {
|
|
||||||
this.redirectIds.push(redirectId);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.redirects[redirectId] = {
|
this.redirects[redirectId] = {
|
||||||
fromRegex,
|
fromRegex,
|
||||||
|
@ -131,11 +125,10 @@ class DynamicRedirectManager {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
removeRedirect(redirectId) {
|
removeRedirect(redirectId) {
|
||||||
this.redirectIds.splice(this.redirectIds.indexOf(redirectId), 1);
|
|
||||||
delete this.redirects[redirectId];
|
delete this.redirects[redirectId];
|
||||||
|
|
||||||
this.router = express.Router();
|
this.router = express.Router();
|
||||||
this.redirectIds.forEach(id => this.setupRedirect(id));
|
Object.keys(this.redirects).forEach(id => this.setupRedirect(id));
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -144,7 +137,6 @@ class DynamicRedirectManager {
|
||||||
* @returns {void}
|
* @returns {void}
|
||||||
*/
|
*/
|
||||||
removeAllRedirects() {
|
removeAllRedirects() {
|
||||||
this.redirectIds = [];
|
|
||||||
this.redirects = {};
|
this.redirects = {};
|
||||||
this.router = express.Router();
|
this.router = express.Router();
|
||||||
}
|
}
|
||||||
|
|
|
@ -137,7 +137,6 @@ describe('DynamicRedirectManager', function () {
|
||||||
req.url = '/redirect-me';
|
req.url = '/redirect-me';
|
||||||
|
|
||||||
manager.removeAllRedirects();
|
manager.removeAllRedirects();
|
||||||
manager.redirectIds.should.be.empty();
|
|
||||||
manager.redirects.should.be.empty();
|
manager.redirects.should.be.empty();
|
||||||
|
|
||||||
manager.handleRequest(req, res, function next() {
|
manager.handleRequest(req, res, function next() {
|
||||||
|
|
Loading…
Reference in a new issue