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. diff --git a/index.js b/index.js index 2effa4f..27de37a 100644 --- a/index.js +++ b/index.js @@ -13,17 +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); - // TODO: Add max-age here - response.header('Cache-Control', 'private, no-cache'); + console.log('[CACHE] HIT: GET '+request[cacheKey]+request['originalUrl']); response.send(value); return true; } else { @@ -32,20 +30,18 @@ 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 { 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(); 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) {