Skip to content

Commit d8ac5aa

Browse files
authored
fix(memo): use lru-cache for better mem management (#75)
Fixes: #74
1 parent 00f773e commit d8ac5aa

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/memoization.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
'use strict'
22

3-
let MEMOIZED = {}
3+
const LRU = require('lru-cache')
4+
5+
const MAX_SIZE = 50 * 1024 * 1024 // 50MB
6+
const MAX_AGE = 3 * 60 * 1000
7+
8+
let MEMOIZED
9+
clearMemoized()
410

511
module.exports.clearMemoized = clearMemoized
612
function clearMemoized () {
713
var old = MEMOIZED
8-
MEMOIZED = {}
14+
MEMOIZED = new LRU({
15+
max: MAX_SIZE,
16+
maxAge: MAX_AGE,
17+
length: (entry, key) => {
18+
if (key.startsWith('key:')) {
19+
return entry.data.length
20+
} else if (key.startsWith('digest:')) {
21+
return entry.length
22+
}
23+
}
24+
})
925
return old
1026
}
1127

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"chownr": "^1.0.1",
5353
"glob": "^7.1.1",
5454
"graceful-fs": "^4.1.10",
55+
"lru-cache": "^4.0.2",
5556
"mississippi": "^1.2.0",
5657
"mkdirp": "^0.5.1",
5758
"promise-inflight": "^1.0.1",

0 commit comments

Comments
 (0)