Last active
August 29, 2015 14:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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