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

Commit

Permalink
feat(cache): allow configuring algorithms to be calculated on insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 4, 2017
1 parent 8a7dbd1 commit bf4a0f2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
15 changes: 8 additions & 7 deletions cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,11 @@ module.exports = class Cache {
}

// Takes both a request and its response and adds it to the given cache.
put (req, response) {
put (req, response, opts) {
const size = response.headers.get('content-length')
const fitInMemory = !!size && size < MAX_MEM_SIZE
const opts = {
const cacheOpts = {
algorithms: opts.algorithms,
metadata: {
url: req.url,
reqHeaders: req.headers.raw(),
Expand All @@ -127,11 +128,11 @@ module.exports = class Cache {
// Update metadata without writing
return cacache.get.info(this._path, cacheKey(req)).then(info => {
// Providing these will bypass content write
opts.integrity = info.integrity
cacheOpts.integrity = info.integrity
return new this.Promise((resolve, reject) => {
pipe(
cacache.get.stream.byDigest(this._path, info.integrity, opts),
cacache.put.stream(this._path, cacheKey(req), opts),
cacache.get.stream.byDigest(this._path, info.integrity, cacheOpts),
cacache.put.stream(this._path, cacheKey(req), cacheOpts),
err => err ? reject(err) : resolve(response)
)
})
Expand All @@ -154,15 +155,15 @@ module.exports = class Cache {
cachePath,
cacheKey(req),
Buffer.concat(buf, bufSize),
opts
cacheOpts
).then(
() => done(),
done
)
})
} else {
cacheTargetStream =
cacache.put.stream(cachePath, cacheKey(req), opts)
cacache.put.stream(cachePath, cacheKey(req), cacheOpts)
}
}
cacheTargetStream.write(chunk, enc, cb)
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function remoteFetch (uri, opts) {
// No other statuses should be stored!
(res.status === 200 || res.status === 304)
) {
return opts.cacheManager.put(req, res, opts.cacheOpts)
return opts.cacheManager.put(req, res, opts)
} else if (opts.cacheManager && (
(req.method !== 'GET' && req.method !== 'HEAD')
)) {
Expand Down

0 comments on commit bf4a0f2

Please sign in to comment.