From 9d10687f5b5c1fc8314cc7b6a2d7c176b16ca4a4 Mon Sep 17 00:00:00 2001 From: Eugenio Homes Date: Sun, 8 Nov 2015 09:52:44 -0300 Subject: [PATCH] avoid cache on the fly --- lib/Cacher.js | 16 +++++++++++----- test/fixtures/server.js | 5 +++++ test/testCacher.js | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/Cacher.js b/lib/Cacher.js index ab08798..6e24ef9 100644 --- a/lib/Cacher.js +++ b/lib/Cacher.js @@ -151,8 +151,8 @@ Cacher.prototype.cache = function(unit, value) { return self.sendCached(res, cacheObject) } - self.buildEnd(res, key, staleKey, realTtl, ttl) - self.buildWrite(res) + self.buildEnd(res, key, staleKey, realTtl, ttl, req) + self.buildWrite(res, req) res.header(self.cacheHeader, false) next() @@ -179,11 +179,15 @@ function appendCache(res, data) { } } -Cacher.prototype.buildEnd = function(res, key, staleKey, realTtl, ttl) { +Cacher.prototype.buildEnd = function(res, key, staleKey, realTtl, ttl, req) { var origEnd = res.end var self = this res.end = function (data) { + if (req && req.noCaching) { + return origEnd.apply(res, arguments) + } + appendCache(res, data) var cacheObject = {statusCode: res.statusCode, content: res._responseBody ? res._responseBody.toString("base64") : '', headers: res._headers} ttl = self.genCacheTtl(res, ttl) @@ -204,10 +208,12 @@ Cacher.prototype.buildEnd = function(res, key, staleKey, realTtl, ttl) { } } -Cacher.prototype.buildWrite = function(res) { +Cacher.prototype.buildWrite = function(res, req) { var origWrite = res.write res.write = function (data) { - appendCache(res, data) + if (!req || !req.noCaching) { + appendCache(res, data) + } return origWrite.apply(res, arguments) } } diff --git a/test/fixtures/server.js b/test/fixtures/server.js index 1703a3a..2758168 100644 --- a/test/fixtures/server.js +++ b/test/fixtures/server.js @@ -36,6 +36,11 @@ module.exports = function(cacher) { app.get('/head', cacher.cache('day'), function(req, res) { res.send('HEAD request') }) + app.get('/dont-cache-onthefly', cacher.cache('day'), function(req, res) { + req.noCaching = true; + res.send('this is not cached') + }) + var fooRouter = express.Router(); var barRouter = express.Router(); diff --git a/test/testCacher.js b/test/testCacher.js index eac1d6c..bbbd05d 100644 --- a/test/testCacher.js +++ b/test/testCacher.js @@ -525,5 +525,37 @@ describe('Cacher', function() { ]) }) + /** + * useful when you got some response error and dont want to cache it + * Example; + * app.get('/dont-cache-onthefly', cacher.cache('day'), function(req, res) { + * req.noCaching = true; + * res.send('this is not cached') + * }) + */ + it('should let you avoid cache on the fly', function(done) { + supertest(app) + .get('/dont-cache-onthefly') + .expect(cacher.cacheHeader, 'false') + .expect(200) + .expect('this is not cached') + .expect('Content-Type', /text/) + .end(function(err, res) { + assert.ifError(err) + + supertest(app) + .get('/dont-cache-onthefly') + .expect(cacher.cacheHeader, 'false') + .expect(200) + .expect('this is not cached') + .expect('Content-Type', /text/) + .end(function(err, res) { + assert.ifError(err) + done() + }) + }) + }) + + }) })