-
Notifications
You must be signed in to change notification settings - Fork 0
Adds 304 support by hashing response body to create an ETag. #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| var adapterMemory = require('./lib/adapterMemory.js'), | ||
| var crypto = require('crypto') | ||
| adapterMemory = require('./lib/adapterMemory.js'), | ||
| adapterMemJS = require('./lib/adapterMemJS.js'), | ||
| adapterRedis = require('./lib/adapterRedis.js'); | ||
|
|
||
|
|
||
| //returns a unique hash based on the passed string | ||
| var getHash = function(stringToHash) { | ||
| return crypto.createHash('sha256').update(stringToHash).digest('hex'); | ||
| }; | ||
|
|
||
| // Caching middleware for Express framework | ||
| // details are here https://github.com/vodolaz095/express-view-cache | ||
|
|
||
|
|
||
| module.exports=function(invalidateTimeInMilliseconds,parameters){ | ||
| if(invalidateTimeInMilliseconds && /^\d+$/.test(invalidateTimeInMilliseconds)){ | ||
| //it is ok | ||
|
|
@@ -18,11 +24,19 @@ module.exports=function(invalidateTimeInMilliseconds,parameters){ | |
| if(parameters && parameters.type){ | ||
| response.type(parameters.type); | ||
| } | ||
| if (request.method == 'GET') { | ||
| if (request.method === 'GET') { | ||
| cache.get(request.originalUrl,function(err,value){ | ||
| if(value){ | ||
| console.log('[CACHE] HIT: GET '+request.originalUrl); | ||
| response.header('Cache-Control', 'private, no-cache, no-store, must-revalidate') | ||
|
|
||
| var contentHash = getHash(value); | ||
| if (request.get('ETag') === contentHash) { | ||
| response.status(304).end(); | ||
| return true; | ||
| } | ||
|
|
||
| response.header('Cache-Control', 'private, no-cache'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few spots in poseidon where we set private, no-cache, no-store, must-revalidate (doing a search for no-cache in dh2o-poseidon/src shows 4 files and a no_cache middleware that is applied to the entire app. Do any of these need to be changed? I guess this line will correct that header if poseidon has set it to something else so maybe it doesn't matter. |
||
| response.header('ETag', contentHash); | ||
| response.send(value); | ||
| return true; | ||
| } else { | ||
|
|
@@ -43,7 +57,8 @@ module.exports=function(invalidateTimeInMilliseconds,parameters){ | |
| console.log("[CACHE] RESPONSE CODE WAS "+this.statusCode+", NOT CACHING"); | ||
| }; | ||
| }); | ||
| response.header('Cache-Control', 'private, no-cache, no-store, must-revalidate') | ||
| response.header('Cache-Control', 'private, no-cache'); | ||
| response.header('ETag', getHash(chunk)); | ||
| response.end(chunk, encoding); | ||
| }; | ||
| return next(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a console.log here that looks like [CACHE] HIT: ETAG MATCH ON url