route-cache
Advanced tools
Comparing version 0.0.3 to 0.1.0
42
index.js
@@ -7,20 +7,52 @@ var Eidetic = require('eidetic'); | ||
var queues = {}; | ||
module.exports.cacheSeconds = function(ttl) { | ||
return function(req, res, next) { | ||
var self = this; | ||
var cache = cachestore.get(req.path); | ||
//var send = res.send; | ||
res.original_send = res.send; | ||
// returns the value immediately | ||
if (cache) { | ||
res.send(cache); | ||
} else { | ||
var send = res.send; | ||
res.send = function(string) { | ||
return; | ||
} | ||
if (!queues[req.path]) { | ||
queues[req.path] = []; | ||
} | ||
// first request will get rendered output | ||
if (queues[req.path].length === 0 | ||
&& queues[req.path].push(function noop(){})) { | ||
res.send = function (string) { | ||
var body = string instanceof Buffer ? string.toString() : string; | ||
cachestore.put(req.url, body, ttl); | ||
send.call(this, body); | ||
// drain the queue so anyone else waiting for | ||
// this value will get their responses. | ||
var subscriber = null; | ||
while (subscriber = queues[req.path].shift()) { | ||
if (subscriber) { | ||
process.nextTick(subscriber); | ||
} | ||
} | ||
res.original_send(body); | ||
}; | ||
next(); | ||
// subsequent requests will batch while the first computes | ||
} else { | ||
queues[req.path].push(function() { | ||
var body = cachestore.get(req.path); | ||
res.send(body); | ||
}); | ||
} | ||
} | ||
}; |
{ | ||
"name": "route-cache", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "express middleware for caching your routes", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
4120
51