plugin.ext('onPreResponse', function (request, next) { var startDate = cache.get(request.id); if (startDate) { var statusCode = (request.response.isBoom) ? request.response.output.statusCode : request.response.statusCode; var statName = settings.template .replace('{path}', normalizePath(request._route.path)) .replace('{method}', request.method.toUpperCase()) .replace('{statusCode}', statusCode); statName = (statName.indexOf('.') === 0) ? statName.substr(1) : statName; statsdClient.increment(statName); statsdClient.timing(statName, startDate); cache.del(request.id); } next(); });
return function(req, res, next) { var self = this; var cache = cachestore.get(req.path); res.original_send = res.send; // returns the value immediately if (cache) { res.send(cache); 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); // 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); }); } }
queues[req.path].push(function() { var body = cachestore.get(req.path); res.send(body); });