Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
fix(memoization): actually use the LRU
Browse files Browse the repository at this point in the history
This is a bug from when the LRU was introduced. I totally forgot to change
the actual calls to `.get/.set`. So we were just treating it as a plain
object. lololol

Fixes: #89
  • Loading branch information
zkat committed Apr 27, 2017
1 parent 358443a commit 0e55dc9
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/memoization.js
Expand Up @@ -5,43 +5,45 @@ const LRU = require('lru-cache')
const MAX_SIZE = 50 * 1024 * 1024 // 50MB
const MAX_AGE = 3 * 60 * 1000

let MEMOIZED
clearMemoized()
let MEMOIZED = new LRU({
max: MAX_SIZE,
maxAge: MAX_AGE,
length: (entry, key) => {
if (key.startsWith('key:')) {
return entry.data.length
} else if (key.startsWith('digest:')) {
return entry.length
}
}
})

module.exports.clearMemoized = clearMemoized
function clearMemoized () {
var old = MEMOIZED
MEMOIZED = new LRU({
max: MAX_SIZE,
maxAge: MAX_AGE,
length: (entry, key) => {
if (key.startsWith('key:')) {
return entry.data.length
} else if (key.startsWith('digest:')) {
return entry.length
}
}
const old = {}
MEMOIZED.forEach((v, k) => {
old[k] = v
})
MEMOIZED.reset()
return old
}

module.exports.put = put
function put (cache, entry, data) {
MEMOIZED[`key:${cache}:${entry.key}`] = { entry, data }
MEMOIZED.set(`key:${cache}:${entry.key}`, { entry, data })
putDigest(cache, entry.integrity, data)
}

module.exports.put.byDigest = putDigest
function putDigest (cache, integrity, data) {
MEMOIZED[`digest:${cache}:${integrity}`] = data
MEMOIZED.set(`digest:${cache}:${integrity}`, data)
}

module.exports.get = get
function get (cache, key) {
return MEMOIZED[`key:${cache}:${key}`]
return MEMOIZED.get(`key:${cache}:${key}`)
}

module.exports.get.byDigest = getDigest
function getDigest (cache, integrity) {
return MEMOIZED[`digest:${cache}:${integrity}`]
return MEMOIZED.get(`digest:${cache}:${integrity}`)
}

0 comments on commit 0e55dc9

Please sign in to comment.