Skip to content

Commit

Permalink
Merge pull request #11 from rundef/master
Browse files Browse the repository at this point in the history
Implemented removeCache function
  • Loading branch information
bradoyler committed Sep 10, 2015
2 parents 713efd8 + f27c2c6 commit 8ca74dc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
41 changes: 35 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});

Expand All @@ -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){
Expand Down Expand Up @@ -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);
});

Expand All @@ -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);

});
});

});

0 comments on commit 8ca74dc

Please sign in to comment.