-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache-lru.js
41 lines (40 loc) · 962 Bytes
/
cache-lru.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const QuickLRU = require('quick-lru')
function createCacheLRU (opts) {
opts = {
...createCacheLRU.DEFAULTS,
...opts
}
const cache = new QuickLRU({ maxSize: opts.maxSize })
return Object.freeze({
async clear () {
cache.clear()
},
async clearName (name) {
for (const key of cache.keys()) {
const keyName = /^[^:]+:(.*)$/.exec(key)[1]
if (keyName === name) {
cache.delete(key)
}
}
},
async close () {},
async flush () {
const now = Date.now()
for (const [key, { expires }] of cache) {
if (expires < now) {
cache.delete(key)
}
}
},
async get (protocol, name) {
return cache.get(`${protocol}:${name}`)
},
async set (protocol, name, entry) {
cache.set(`${protocol}:${name}`, entry)
}
})
}
createCacheLRU.DEFAULTS = Object.freeze({
maxSize: 1000
})
module.exports = Object.freeze(createCacheLRU)