-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(perfs): split keys computation into smaller functions
- Loading branch information
1 parent
357ede2
commit 5aba888
Showing
11 changed files
with
109 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 15 additions & 40 deletions
55
packages/strapi-plugin-rest-cache/server/utils/middlewares/generateCacheKey.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,27 @@ | ||
'use strict'; | ||
|
||
/** | ||
* @typedef {import('koa').Context} Context | ||
*/ | ||
|
||
/** | ||
* Generates a cache key for the current request | ||
* | ||
* @param {CacheRouteConfig} cacheRouteConfig | ||
* @param {Context} ctx | ||
* @return {string} | ||
*/ | ||
function generateCacheKey(cacheRouteConfig, ctx) { | ||
const generateHeadersKey = require('./generateHeadersKey'); | ||
const generateQueryParamsKey = require('./generateQueryParamsKey'); | ||
|
||
function generateCacheKey( | ||
ctx, | ||
keys = { | ||
useQueryParams: false, // @todo: array or boolean => can be optimized | ||
useHeaders: [], | ||
} | ||
) { | ||
let querySuffix = ''; | ||
let headersSuffix = ''; | ||
|
||
if (cacheRouteConfig.keys.useQueryParams !== false) { | ||
let keys = []; | ||
|
||
if (cacheRouteConfig.keys.useQueryParams === true) { | ||
keys = Object.keys(ctx.query); | ||
} else if (cacheRouteConfig.keys.useQueryParams.length > 0) { | ||
keys = Object.keys(ctx.query).filter((key) => | ||
cacheRouteConfig.keys.useQueryParams.includes(key) | ||
); | ||
} | ||
|
||
querySuffix = keys | ||
.sort() | ||
.map( | ||
(k) => | ||
`${k}=${ | ||
typeof ctx.query[k] === 'object' | ||
? JSON.stringify(ctx.query[k]) | ||
: ctx.query[k] | ||
}` | ||
) // query strings are key sensitive | ||
.join(','); | ||
if (keys.useQueryParams !== false) { | ||
querySuffix = generateQueryParamsKey(ctx, keys.useQueryParams); | ||
} | ||
|
||
if (cacheRouteConfig.keys.useHeaders.length > 0) { | ||
headersSuffix = cacheRouteConfig.keys.useHeaders | ||
.filter((k) => ctx.request.header[k] !== undefined) | ||
.map((k) => `${k.toLowerCase()}=${ctx.request.header[k.toLowerCase()]}`) // headers are key insensitive | ||
.join(','); | ||
if (keys.useHeaders.length > 0) { | ||
headersSuffix = generateHeadersKey(ctx, keys.useHeaders); | ||
} | ||
|
||
return `${querySuffix}&${headersSuffix}`; | ||
return `${ctx.request.path}?${querySuffix}&${headersSuffix}`; | ||
} | ||
|
||
module.exports = generateCacheKey; |
10 changes: 10 additions & 0 deletions
10
packages/strapi-plugin-rest-cache/server/utils/middlewares/generateHeadersKey.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict'; | ||
|
||
function generateHeadersKey(ctx, useHeaders = []) { | ||
return useHeaders | ||
.filter((k) => ctx.request.header[k] !== undefined) | ||
.map((k) => `${k.toLowerCase()}=${ctx.request.header[k.toLowerCase()]}`) // headers are key insensitive | ||
.join(','); | ||
} | ||
|
||
module.exports = generateHeadersKey; |
33 changes: 33 additions & 0 deletions
33
packages/strapi-plugin-rest-cache/server/utils/middlewares/generateQueryParamsKey.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
function generateQueryParamsKey( | ||
ctx, | ||
useQueryParams = true // @todo: array or boolean => can be optimized | ||
) { | ||
let keys = []; | ||
|
||
if (useQueryParams === true) { | ||
keys = Object.keys(ctx.query); | ||
} else if (useQueryParams.length > 0) { | ||
keys = Object.keys(ctx.query).filter((key) => useQueryParams.includes(key)); | ||
} | ||
|
||
if (keys.length === 0) { | ||
return ''; | ||
} | ||
|
||
keys.sort(); | ||
|
||
return keys | ||
.map( | ||
(k) => | ||
`${k}=${ | ||
typeof ctx.query[k] === 'object' | ||
? JSON.stringify(ctx.query[k]) | ||
: ctx.query[k] | ||
}` | ||
) // query strings are key sensitive | ||
.join(','); | ||
} | ||
|
||
module.exports = generateQueryParamsKey; |
15 changes: 9 additions & 6 deletions
15
packages/strapi-plugin-rest-cache/server/utils/middlewares/shouldLookup.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters