Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions index.js
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
Expand All @@ -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) {

Copy link
Copy Markdown

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

response.status(304).end();
return true;
}

response.header('Cache-Control', 'private, no-cache');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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();
Expand Down