route-cache
Advanced tools
Comparing version 0.3.0 to 0.3.1
10
index.js
@@ -11,11 +11,9 @@ 'use strict' | ||
module.exports.cacheSeconds = function (secondsTTL) { | ||
module.exports.cacheSeconds = function (secondsTTL, cacheKey) { | ||
var ttl = secondsTTL * 1000 | ||
return function (req, res, next) { | ||
var key = req.originalUrl | ||
var key = (cacheKey === undefined) ? req.originalUrl : cacheKey | ||
if (redirects[key]) { | ||
return res.redirect(redirects[key].status, redirects[key].url) | ||
} | ||
var value = cacheStore.get(key) | ||
@@ -44,3 +42,3 @@ | ||
function rawSend (data, isJson) { | ||
var key = req.originalUrl | ||
var key = (cacheKey === undefined) ? req.originalUrl : cacheKey | ||
@@ -116,3 +114,3 @@ // pass-through for Buffer - not supported | ||
next() | ||
// subsequent requests will batch while the first computes | ||
// subsequent requests will batch while the first computes | ||
} else { | ||
@@ -119,0 +117,0 @@ queues[key].push(function () { |
{ | ||
"name": "route-cache", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "express middleware for caching your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,51 +0,52 @@ | ||
'use strict'; | ||
'use strict' | ||
var request = require('supertest'), | ||
routeCache = require('../index'), | ||
express = require('express'); | ||
express = require('express') | ||
var testindex = 0; | ||
var testindexRemove = 0; | ||
var testindex = 0 | ||
var paramTestindex = 0 | ||
var testindexRemove = 0 | ||
describe('# RouteCache middleware test', function () { | ||
var app = express(); | ||
var app = express() | ||
app.get('/hello', routeCache.cacheSeconds(1), function (req, res) { | ||
testindex++; | ||
res.send('Hello ' + testindex); | ||
}); | ||
testindex++ | ||
res.send('Hello ' + testindex) | ||
}) | ||
app.get('/hello/1', routeCache.cacheSeconds(1), function (req, res) { | ||
res.send('Hello/1'); | ||
}); | ||
res.send('Hello/1') | ||
}) | ||
app.get('/hello/:num([0-9])', routeCache.cacheSeconds(1), function (req, res) { | ||
res.send('Hello param:' + req.params.num); | ||
}); | ||
res.send('Hello param:' + req.params.num) | ||
}) | ||
app.get('/500', routeCache.cacheSeconds(10), function (req, res) { | ||
res.status(500).send('Internal server error: ' + Math.random()); | ||
}); | ||
res.status(500).send('Internal server error: ' + Math.random()) | ||
}) | ||
app.get('/redirect-to-hello', routeCache.cacheSeconds(1), function (req, res) { | ||
res.redirect('/hello'); | ||
}); | ||
res.redirect('/hello') | ||
}) | ||
app.get('/301-redirect-to-hello', routeCache.cacheSeconds(1), function (req, res) { | ||
res.redirect(301, '/hello'); | ||
}); | ||
res.redirect(301, '/hello') | ||
}) | ||
app.get('/302-redirect-to-hello', routeCache.cacheSeconds(1), function (req, res) { | ||
res.redirect(302, '/hello'); | ||
}); | ||
res.redirect(302, '/hello') | ||
}) | ||
app.get('/hello-remove', routeCache.cacheSeconds(3600), function (req, res) { | ||
testindexRemove++; | ||
res.send('Hello remove ' + testindexRemove); | ||
}); | ||
testindexRemove++ | ||
res.send('Hello remove ' + testindexRemove) | ||
}) | ||
app.get('/hello-api', routeCache.cacheSeconds(3600), function (req, res) { | ||
res.json({msg: 'Hello'}); | ||
}); | ||
res.json({msg: 'Hello'}) | ||
}) | ||
var agent = request.agent(app); | ||
var agent = request.agent(app) | ||
@@ -55,4 +56,4 @@ it('1st Hello', function (done) { | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}); | ||
.expect('Hello 1', done) | ||
}) | ||
@@ -62,4 +63,4 @@ it('2nd Hello', function (done) { | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}); | ||
.expect('Hello 1', done) | ||
}) | ||
@@ -69,4 +70,4 @@ it('1st Hello w/ param', function (done) { | ||
.get('/hello/1') | ||
.expect('Hello/1', done); | ||
}); | ||
.expect('Hello/1', done) | ||
}) | ||
@@ -76,4 +77,4 @@ it('2nd Hello w/ param', function (done) { | ||
.get('/hello/2') | ||
.expect('Hello param:2', done); | ||
}); | ||
.expect('Hello param:2', done) | ||
}) | ||
@@ -83,4 +84,4 @@ it('1st Redirect to hello', function (done) { | ||
.get('/redirect-to-hello') | ||
.expect(302, /\/hello/, done); | ||
}); | ||
.expect(302, /\/hello/, done) | ||
}) | ||
@@ -90,4 +91,4 @@ it('2nd Redirect to hello', function (done) { | ||
.get('/redirect-to-hello') | ||
.expect(302, /\/hello/, done); | ||
}); | ||
.expect(302, /\/hello/, done) | ||
}) | ||
@@ -97,4 +98,4 @@ it('301 Redirect to hello', function (done) { | ||
.get('/301-redirect-to-hello') | ||
.expect(301, /\/hello/, done); | ||
}); | ||
.expect(301, /\/hello/, done) | ||
}) | ||
@@ -105,5 +106,5 @@ it('~ delayed 301 Redirect to hello', function (done) { | ||
.get('/301-redirect-to-hello') | ||
.expect(301, /\/hello/, done); | ||
}, 1200); | ||
}); | ||
.expect(301, /\/hello/, done) | ||
}, 1200) | ||
}) | ||
@@ -113,4 +114,4 @@ it('Explicit 302 Redirect to hello', function (done) { | ||
.get('/302-redirect-to-hello') | ||
.expect(302, /\/hello/, done); | ||
}); | ||
.expect(302, /\/hello/, done) | ||
}) | ||
@@ -121,20 +122,25 @@ it('~ delayed: Hello 2', function (done) { | ||
.get('/hello') | ||
.expect('Hello 2', done); | ||
}, 1200); | ||
}); | ||
.expect('Hello 2', done) | ||
}, 1200) | ||
}) | ||
it('Error states don\'t get cached', function (done) { | ||
var message; | ||
it('Hello test with param', function (done) { | ||
agent | ||
.get('/hello?a=1') | ||
.expect('Hello 3', done) | ||
}) | ||
it("Error states don't get cached", function (done) { | ||
var message | ||
agent.get('/500').expect(500).end(function (req, res) { | ||
message = res.text; | ||
message = res.text | ||
agent.get('/500').expect(500).end(function (req, res) { | ||
if (message == res.text) return done(Error('Got same error message as before')); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
if (message == res.text) return done(Error('Got same error message as before')) | ||
done() | ||
}) | ||
}) | ||
}) | ||
it('test removeCache', function (done) { | ||
@@ -144,19 +150,16 @@ agent | ||
.expect('Hello remove 1').end(function (req, res) { | ||
setTimeout(function () { | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 1').end(function (req, res) { | ||
routeCache.removeCache('/hello-remove') | ||
setTimeout(function () { | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 1').end(function (req, res) { | ||
.expect('Hello remove 2', done) | ||
}) | ||
}, 1200) | ||
}) | ||
}) | ||
routeCache.removeCache('/hello-remove'); | ||
agent | ||
.get('/hello-remove') | ||
.expect('Hello remove 2', done); | ||
}); | ||
}, 1200); | ||
}); | ||
}); | ||
it('res.json headers', function (done) { | ||
@@ -166,9 +169,9 @@ agent | ||
.expect('Content-Type', /json/).end(function (req, res) { | ||
setTimeout(function () { | ||
agent | ||
.get('/hello-api') | ||
.expect('Content-Type', /json/, done); | ||
}, 200); | ||
}); | ||
}); | ||
}); | ||
setTimeout(function () { | ||
agent | ||
.get('/hello-api') | ||
.expect('Content-Type', /json/, done) | ||
}, 200) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25957
15
674
1