Skip to content

Commit

Permalink
Merge pull request #490 from dadi/patch/cache-key-full-url
Browse files Browse the repository at this point in the history
Use full request url as cache key
  • Loading branch information
adamkdean committed Apr 18, 2019
2 parents d83e150 + a5101cf commit d41642a
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions dadi/lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,6 @@ Cache.prototype.init = function () {
// only cache GET requests
if (req.method && req.method.toLowerCase() !== 'get') return next()

// we build the filename with a hashed hex string so we can be unique
// and avoid using file system reserved characters in the name
const requestUrl = url.parse(req.url, true).path

// get the host key that matches the request's host header
const virtualHosts = config.get('virtualHosts')

Expand All @@ -199,9 +195,11 @@ Cache.prototype.init = function () {
return virtualHosts.hostnames.includes((req.headers.host || req.headers[':authority']))
}) || ''

// we build the filename with a hashed hex string so we can be unique
// and avoid using file system reserved characters in the name
const filename = crypto
.createHash('sha1')
.update(`${host}${requestUrl}`)
.update(`${host}${req.url}`)
.digest('hex')

// allow query string param to bypass cache
Expand Down Expand Up @@ -299,7 +297,7 @@ Cache.prototype.init = function () {
try {
self.cache.set(filename, Buffer.concat(data), opts).then(() => {})
} catch (e) {
console.log('Could not cache content: ' + requestUrl)
console.log('Could not cache content: ' + req.url)
}
}

Expand Down

0 comments on commit d41642a

Please sign in to comment.