Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

Commit

Permalink
fix(memoization): missed spots + allow passthrough of memo objs
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 28, 2017
1 parent dc0563d commit ac0cd12
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ module.exports = class Cache {
// Returns a Promise that resolves to the response associated with the first
// matching request in the Cache object.
match (req, opts) {
opts = opts || {}
const key = cacheKey(req)
return cacache.get.info(this._path, key).then(info => {
if (info && info.metadata && matchDetails(req, {
Expand Down Expand Up @@ -68,14 +69,16 @@ module.exports = class Cache {
disturbed = true
if (opts.memoize !== false && info.size > MAX_MEM_SIZE) {
pipe(
cacache.get.stream.byDigest(cachePath, info.integrity),
cacache.get.stream.byDigest(cachePath, info.integrity, {
memoize: opts.memoize
}),
body,
() => {}
)
} else {
// cacache is much faster at bulk reads
cacache.get.byDigest(cachePath, info.integrity, {
memoize: opts.memoize !== false
memoize: opts.memoize
}).then(data => {
body.write(data, () => {
body.end()
Expand Down Expand Up @@ -105,6 +108,7 @@ module.exports = class Cache {

// Takes both a request and its response and adds it to the given cache.
put (req, response, opts) {
opts = opts || {}
const size = response.headers.get('content-length')
const fitInMemory = !!size && opts.memoize !== false && size < MAX_MEM_SIZE
const ckey = cacheKey(req)
Expand All @@ -118,7 +122,7 @@ module.exports = class Cache {
uid: this._uid,
gid: this._gid,
size,
memoize: fitInMemory
memoize: fitInMemory && opts.memoize
}
if (req.method === 'HEAD' || response.status === 304) {
// Update metadata without writing
Expand Down Expand Up @@ -192,7 +196,19 @@ module.exports = class Cache {
// Finds the Cache entry whose key is the request, and if found, deletes the
// Cache entry and returns a Promise that resolves to true. If no Cache entry
// is found, it returns false.
'delete' (req) {
'delete' (req, opts) {
opts = opts || {}
if (typeof opts.memoize === 'object') {
if (opts.memoize.reset) {
opts.memoize.reset()
} else if (opts.memoize.clear) {
opts.memoize.clear()
} else {
Object.keys(opts.memoize).forEach(k => {
opts.memoize[k] = null
})
}
}
return cacache.rm.entry(
this._path,
cacheKey(req)
Expand Down

0 comments on commit ac0cd12

Please sign in to comment.