0
Fork 0
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:
Daniel Lockyer 2024-10-14 17:01:51 +02:00 committed by Daniel Lockyer
parent 6dd821bd41
commit bfdf2dd8bf
2 changed files with 1 additions and 10 deletions

View file

@ -16,8 +16,6 @@ class DynamicRedirectManager {
/** @private */
this.router = express.Router();
/** @private @type {string[]} */
this.redirectIds = [];
/** @private @type {Object.<string, {fromRegex: RegExp, to: string, options: {permanent: boolean}}>} */
this.redirects = {};
@ -103,10 +101,6 @@ class DynamicRedirectManager {
const fromRegex = this.buildRegex(from);
const redirectId = from;
if (!this.redirectIds.includes(redirectId)) {
this.redirectIds.push(redirectId);
}
this.redirects[redirectId] = {
fromRegex,
@ -131,11 +125,10 @@ class DynamicRedirectManager {
* @returns {void}
*/
removeRedirect(redirectId) {
this.redirectIds.splice(this.redirectIds.indexOf(redirectId), 1);
delete this.redirects[redirectId];
this.router = express.Router();
this.redirectIds.forEach(id => this.setupRedirect(id));
Object.keys(this.redirects).forEach(id => this.setupRedirect(id));
return;
}
@ -144,7 +137,6 @@ class DynamicRedirectManager {
* @returns {void}
*/
removeAllRedirects() {
this.redirectIds = [];
this.redirects = {};
this.router = express.Router();
}

View file

@ -137,7 +137,6 @@ describe('DynamicRedirectManager', function () {
req.url = '/redirect-me';
manager.removeAllRedirects();
manager.redirectIds.should.be.empty();
manager.redirects.should.be.empty();
manager.handleRequest(req, res, function next() {