Skip to content

Instantly share code, notes, and snippets.

@cybo42
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save cybo42/fa73f9d57f3bce239c73 to your computer and use it in GitHub Desktop.

Select an option

Save cybo42/fa73f9d57f3bce239c73 to your computer and use it in GitHub Desktop.
Expressjs caching middleware, taken from http://slides.com/bradoyler/nodejs-content-sites presentation
module.exports.cacheSeconds = function(ttl) {
return function(req, res, next) {
var cache = cachestore.get(req.path);
if (cache) {
res.send(cache);
} else {
var send = res.send;
res.send = function(string) {
var body = string instanceof Buffer ? string.toString() : string;
cachestore.put(req.url, body, ttl);
send.call(this, body);
};
next();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment