From c25f4283e7cb5d41048aa8e6e963051bd4ce3c54 Mon Sep 17 00:00:00 2001 From: David Jackson Date: Thu, 30 Mar 2017 16:18:11 -0500 Subject: [PATCH 1/3] Remove Cache-Control header (allowing the caller to determine its preferred caching policy) --- index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.js b/index.js index 2effa4f..626ad60 100644 --- a/index.js +++ b/index.js @@ -22,8 +22,6 @@ module.exports=function(invalidateTimeInMilliseconds,parameters){ cache.get(request.originalUrl,function(err,value){ if(value){ console.log('[CACHE] HIT: GET '+request.originalUrl); - // TODO: Add max-age here - response.header('Cache-Control', 'private, no-cache'); response.send(value); return true; } else { @@ -44,8 +42,6 @@ module.exports=function(invalidateTimeInMilliseconds,parameters){ console.log("[CACHE] RESPONSE CODE WAS "+this.statusCode+", NOT CACHING"); }; }); - // TODO: Add max-age here - response.header('Cache-Control', 'private, no-cache') response.end(chunk, encoding); }; return next(); From 437ecca9f28e10ac744368b9555969499acac58a Mon Sep 17 00:00:00 2001 From: Luke Savage Date: Tue, 23 May 2017 12:56:24 -0500 Subject: [PATCH 2/3] updated cacher to accept a cache key --- index.js | 12 ++++++------ lib/adapterRedis.js | 5 +++++ lib/redis-mock.js | 14 ++++++++++++++ package.json | 2 +- tests/adapter-test.js | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 lib/redis-mock.js diff --git a/index.js b/index.js index 2effa4f..15cece1 100644 --- a/index.js +++ b/index.js @@ -13,15 +13,15 @@ module.exports=function(invalidateTimeInMilliseconds,parameters){ invalidateTimeInMilliseconds=60*1000; //1 minute } cache = adapterRedis; - + cacheKey = parameters.cacheKey || 'originalUrl'; return function(request,response,next){ if(parameters && parameters.type){ response.type(parameters.type); } if (request.method == 'GET') { - cache.get(request.originalUrl,function(err,value){ + cache.get(request[cacheKey] + request['originalUrl'],function(err,value){ if(value){ - console.log('[CACHE] HIT: GET '+request.originalUrl); + console.log('[CACHE] HIT: GET '+request[cacheKey]+request['originalUrl']); // TODO: Add max-age here response.header('Cache-Control', 'private, no-cache'); response.send(value); @@ -32,12 +32,12 @@ module.exports=function(invalidateTimeInMilliseconds,parameters){ response.end = end; response.on('finish',function(){ if (this.statusCode === 200) { - cache.set(request.originalUrl,chunk,function(err,result){ + cache.set(request[cacheKey]+request['originalUrl'],chunk,function(err,result){ if(err) throw err; if(result){ - console.log('[CACHE] SAVED: GET '+request.originalUrl); + console.log('[CACHE] SAVED: GET '+request[cacheKey]+request['originalUrl']); } else { - console.log('[CACHE] ERROR SAVING: GET '+request.originalUrl) + console.log('[CACHE] ERROR SAVING: GET '+request[cacheKey]+request['originalUrl']) } },invalidateTimeInMilliseconds); } else { diff --git a/lib/adapterRedis.js b/lib/adapterRedis.js index 930d5e8..8c9c19e 100644 --- a/lib/adapterRedis.js +++ b/lib/adapterRedis.js @@ -1,4 +1,5 @@ var redis = require('redis'), + RedisMock = require('./redis-mock.js') url = require('url'); var config = { @@ -12,6 +13,10 @@ if (config.port !== null && config.host !== null && config.db !== null){ client.select(config.db,function(err){ if(err) throw err; }); +} else if (!!process.env.TEST_CLIENT) { + var client = null; + var mock = new RedisMock(); + console.log('my mock', mock); } else { var client = null; } diff --git a/lib/redis-mock.js b/lib/redis-mock.js new file mode 100644 index 0000000..30358b4 --- /dev/null +++ b/lib/redis-mock.js @@ -0,0 +1,14 @@ +function RedisMock() { + this.store = {}; + this.set = function() { + console.log('set'); + }; + this.get = function() { + console.log('get') + }; + this.expire = function() { + console.log('expire') + } +}; + +module.exports = RedisMock; \ No newline at end of file diff --git a/package.json b/package.json index 3925600..87c3426 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Fork of https://github.com/vodolaz095/express-view-cache", "main": "index.js", "scripts": { - "test": "./node_modules/.bin/vows --spec tests/*" + "test": "TEST_CLIENT=true ./node_modules/.bin/vows --spec tests/*" }, "repository": { "type": "git", diff --git a/tests/adapter-test.js b/tests/adapter-test.js index beb1109..ada6361 100644 --- a/tests/adapter-test.js +++ b/tests/adapter-test.js @@ -90,7 +90,7 @@ vows.describe('Cache Adapters tests') assert.isNull(result); } }}) -// Redis adaprer +// Redis adapter .addBatch({"General test for adapterRedis": { "topic": adapterRedis, "It should have get and set methods": function (topic) { From 96356ce9d83d084da72be029f0c7c5c39ce2fd9a Mon Sep 17 00:00:00 2001 From: David Jackson Date: Tue, 13 Jun 2017 14:56:32 -0500 Subject: [PATCH 3/3] Create README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index ca8ee62..b88ffd5 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,4 @@ Hacked-up version of https://www.npmjs.org/package/express-view-cache Expects environment variables REDIS_HOST, REDIS_PORT, REDIS_CACHE_DB -Sets no-cache headers. - See https://github.com/vodolaz095/express-view-cache for more info.