Skip to content

Commit 587672c

Browse files
authored
Fixing default init of sequential router and improving cache (#41)
* fix: passing init config to router * supporting unlimited cache size for router matching
1 parent 825d626 commit 587672c

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ server.listen(3000)
9797
res.end()
9898
}
9999
```
100-
- **cacheSize**: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: `1000`
100+
- **cacheSize**: The size of the LRU cache for router matching. If the value is `0`, the cache will be disabled. If the value is `<0`, the cache will have an unlimited size. If the value is `>0`, an LRU Cache will be used. Default value: `-1`, for extreme performance.
101101
- **errorHandler**: Global error handler function. Default value:
102102

103103
```js

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const httpServer = http.Server
33
const httpsServer = require('https').Server
44

55
module.exports = (config = { prioRequestsProcessing: true }) => {
6-
const router = config.router || require('./lib/router/sequential')()
6+
const router = config.router || require('./lib/router/sequential')(config)
77
const server = config.server || http.createServer()
88

99
server.prioRequestsProcessing =

lib/router/sequential.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { Protocol, IRouter, RequestHandler } from './../../common'
1+
import { Protocol, IRouter } from "./../../common";
22

3-
export default function createSequentialRouter<P extends Protocol>(config?: object): IRouter<P>
3+
export default function createSequentialRouter<P extends Protocol>(config?: object): IRouter<P>;

lib/router/sequential.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { Trouter } = require('trouter')
22
const next = require('./../next')
3-
const { LRUCache: LRU } = require('lru-cache')
43
const { parse } = require('regexparam')
4+
const { LRUCache: Cache } = require('lru-cache')
55
const queryparams = require('../utils/queryparams')
66

77
module.exports = (config = {}) => {
@@ -18,15 +18,19 @@ module.exports = (config = {}) => {
1818
}
1919
}
2020
if (config.cacheSize === undefined) {
21-
config.cacheSize = 1000
21+
config.cacheSize = -1
2222
}
2323
if (config.id === undefined) {
2424
config.id = (Date.now().toString(36) + Math.random().toString(36).substr(2, 5)).toUpperCase()
2525
}
2626

2727
const routers = {}
28-
const isCacheEnabled = config.cacheSize > 0
29-
const cache = isCacheEnabled ? new LRU({ max: config.cacheSize }) : null
28+
let cache = null
29+
if (config.cacheSize > 0) {
30+
cache = new Cache({ max: config.cacheSize })
31+
} else if (config.cacheSize < 0) {
32+
cache = new Map()
33+
}
3034
const router = new Trouter()
3135
router.id = config.id
3236

@@ -59,7 +63,7 @@ module.exports = (config = {}) => {
5963
queryparams(req, req.url)
6064

6165
let match
62-
if (isCacheEnabled) {
66+
if (cache) {
6367
const reqCacheKey = req.method + req.path
6468
match = cache.get(reqCacheKey)
6569
if (!match) {

0 commit comments

Comments
 (0)