route-cache
Advanced tools
Comparing version 0.1.0 to 0.1.1
20
index.js
@@ -12,5 +12,5 @@ var Eidetic = require('eidetic'); | ||
return function(req, res, next) { | ||
var key = req.originalUrl; | ||
var self = this; | ||
var cache = cachestore.get(req.path); | ||
//var send = res.send; | ||
var cache = cachestore.get(key); | ||
res.original_send = res.send; | ||
@@ -24,13 +24,13 @@ | ||
if (!queues[req.path]) { | ||
queues[req.path] = []; | ||
if (!queues[key]) { | ||
queues[key] = []; | ||
} | ||
// first request will get rendered output | ||
if (queues[req.path].length === 0 | ||
&& queues[req.path].push(function noop(){})) { | ||
if (queues[key].length === 0 | ||
&& queues[key].push(function noop(){})) { | ||
res.send = function (string) { | ||
var body = string instanceof Buffer ? string.toString() : string; | ||
cachestore.put(req.url, body, ttl); | ||
cachestore.put(key, body, ttl); | ||
@@ -40,3 +40,3 @@ // drain the queue so anyone else waiting for | ||
var subscriber = null; | ||
while (subscriber = queues[req.path].shift()) { | ||
while (subscriber = queues[key].shift()) { | ||
if (subscriber) { | ||
@@ -52,4 +52,4 @@ process.nextTick(subscriber); | ||
} else { | ||
queues[req.path].push(function() { | ||
var body = cachestore.get(req.path); | ||
queues[key].push(function() { | ||
var body = cachestore.get(key); | ||
res.send(body); | ||
@@ -56,0 +56,0 @@ }); |
{ | ||
"name": "route-cache", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "express middleware for caching your routes", | ||
@@ -25,4 +25,6 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "~1.10.0", | ||
"express": "~4.4.5" | ||
"mocha": "~1.13.0", | ||
"express": "~4.4.5", | ||
"should": "2", | ||
"supertest": "0.9.2" | ||
}, | ||
@@ -29,0 +31,0 @@ "bugs": { |
@@ -0,1 +1,3 @@ | ||
[![Build Status](https://travis-ci.org/bradoyler/route-cache.svg?branch=master)](https://travis-ci.org/bradoyler/route-cache) | ||
# Route-Cache | ||
@@ -14,2 +16,7 @@ Simple middleware for Express route caching with a given TTL (in seconds) | ||
## Test | ||
```sh | ||
npm test | ||
``` | ||
## How to use | ||
@@ -31,8 +38,5 @@ ```javascript | ||
## Future plans / todos | ||
- tests (via mocha & supertest) | ||
- client-side Cache-Control | ||
- support for distributed caches (redis or memcache) | ||
- accomodate for "Dog piling/thundering herd" when routes logic is extra slow | ||
------ | ||
@@ -39,0 +43,0 @@ The MIT License (MIT) |
@@ -1,7 +0,45 @@ | ||
var routeCache = require("../index"); | ||
describe ("routeCache", function() { | ||
it('testing...', function() { | ||
console.log('test coming soon...'); | ||
}); | ||
}); | ||
var request = require('supertest'), | ||
should = require('should'), | ||
routeCache = require('../index'), | ||
express = require('express'); | ||
var app = express(); | ||
var testindex = 0; | ||
describe('# RouteCache middleware test', function(){ | ||
var app = express(); | ||
app.get('/hello', routeCache.cacheSeconds(1), function(req, res){ | ||
testindex++; | ||
res.send('Hello ' + testindex) | ||
}); | ||
var agent = request.agent(app); | ||
it('GET #1: Hello 1', function(done){ | ||
agent | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}) | ||
it('GET #2: Hello 1', function(done){ | ||
agent | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}) | ||
it('GET #3: Hello 1', function(done){ | ||
agent | ||
.get('/hello') | ||
.expect('Hello 1', done); | ||
}) | ||
it('GET #4 ~ delayed: Hello 2', function(done){ | ||
setTimeout(function() { | ||
agent | ||
.get('/hello') | ||
.expect('Hello 2', done); | ||
}, 1200); | ||
}) | ||
}) |
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
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
4967
6
81
62
4