diff --git a/README.md b/README.md index 531887b..c51e6f9 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,11 @@ app.get('/index', routeCache.cacheSeconds(20), function(req, res){ ``` +## Delete a cached route +```javascript +routeCache.removeCache('/index'); +``` + ## Future plans / todos - client-side Cache-Control - support for distributed caches (redis or memcache) diff --git a/index.js b/index.js index 1d71acd..31a2e8d 100644 --- a/index.js +++ b/index.js @@ -67,4 +67,12 @@ module.exports.cacheSeconds = function(ttl) { }; + +module.exports.removeCache = function(url) { + if (redirects[url]) + delete redirects[url]; + cacheStore.del(url); +}; + + module.exports.cacheStore = cacheStore; diff --git a/test/index.js b/test/index.js index 841bf87..630ad34 100644 --- a/test/index.js +++ b/test/index.js @@ -6,12 +6,13 @@ var request = require('supertest'), var app = express(); var testindex = 0; + var testindexRemove = 0; describe('# RouteCache middleware test', function(){ var app = express(); app.get('/hello', routeCache.cacheSeconds(1), function(req, res){ - testindex++; + testindex++; res.send('Hello ' + testindex) }); @@ -23,6 +24,11 @@ describe('# RouteCache middleware test', function(){ res.redirect('/hello'); }); + app.get('/hello-remove', routeCache.cacheSeconds(3600), function(req, res){ + testindexRemove++; + res.send('Hello remove ' + testindexRemove) + }); + var agent = request.agent(app); it('GET #1: Hello 1', function(done){ @@ -56,10 +62,10 @@ describe('# RouteCache middleware test', function(){ }); it('GET #6 ~ delayed: Hello 2', function(done){ - setTimeout(function() { - agent - .get('/hello') - .expect('Hello 2', done); + setTimeout(function() { + agent + .get('/hello') + .expect('Hello 2', done); }, 1200); }); @@ -74,5 +80,28 @@ describe('# RouteCache middleware test', function(){ done(); }); }) - }) + }); + + + it('GET #8: test removeCache', function(done){ + agent + .get('/hello-remove') + .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'); + + agent + .get('/hello-remove') + .expect('Hello remove 2', done) + }); + }, 1200); + + }); + }); + }); \ No newline at end of file