Skip to content

Commit

Permalink
introducing router matching cache
Browse files Browse the repository at this point in the history
  • Loading branch information
jkyberneees committed Jan 26, 2025
1 parent 541e040 commit 9c669c8
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/router/sequential.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const STATUS_500 = {
}

module.exports = (config = {}) => {
const cache = new Map()

if (!config.defaultRoute) {
config.defaultRoute = () => {
return new Response(null, STATUS_404)
Expand Down Expand Up @@ -45,8 +47,16 @@ module.exports = (config = {}) => {
req.path = path || '/'
req.query = queryIndex > 0 ? qs.parse(url.substring(queryIndex + 1)) : {}

const match = router.find(req.method, req.path)
if (match.handlers.length > 0) {
const cacheKey = `${req.method}:${req.path}`
let match = null
if (cache.has(cacheKey)) {
match = cache.get(cacheKey)
} else {
match = router.find(req.method, req.path)
cache.set(cacheKey, match)
}

if (match?.handlers?.length > 0) {
if (!req.params) {
req.params = {}
}
Expand Down

0 comments on commit 9c669c8

Please sign in to comment.