Added strategic cache example (with redis)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 9815b27 · Browse files · 4 files·+105 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+105 added−0 removed
- other:+105 added−0 removed
Provenance
- Commit
a90fd85- Tree
74598db- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
9815b27
4 files changed+105 added−0 removed
added:examples/cache/views/_user.ejs+1 added
1+<li><%= user.name %> <<%= user.email %>></li>added:examples/cache/views/layout.ejs+6 added
1+<html>2+ <body>3+ <h1>Users</h1>4+ <%- body %>5+ </body>6+</html>added:examples/cache/views/users.ejs+3 added
1+<ul id="users">2+ <%- partial('user', users) %>3+</ul>added:examples/cache/app.js+95 added
1+2+// Expose modules in ./support for demo purposes3+require.paths.unshift(__dirname + '/../../support');4+5+/**6+ * Module dependencies.7+ */8+9+var express = require('./../../lib/express')10+ , redis = require('redis')11+ , _cache = redis.createClient()12+ , app = express.createServer();13+14+// $ npm install redis15+// if you do not have the redis client installed16+// and start redis via $ redis-server17+18+app.set('views', __dirname + '/views');19+app.set('view engine', 'ejs');20+21+// Generate 50 random users for display22+var n = 50;23+var users = [];24+while (n--) {25+ users.push({ name: 'user ' + n, email: n + '@vision-media.ca' });26+}27+28+// example cache middleware. we proxy writeHead etc to29+// cache the response, and we could perform additional30+// checks on headers etc to extend our caching logic.31+//32+// Please note in this example you would have to33+// handle write() encodings as well, this is just an example :)34+35+function cache(req, res, next){36+ _cache.get(req.url, function(err, data){37+ try {38+ if (data) {39+ data = JSON.parse(data.toString());40+ delete data.headers['X-Response-Time'];41+ res.writeHead(data.status, data.headers);42+ res.end(data.body);43+ } else {44+ proxyCache(req, res);45+ next();46+ }47+ } catch (err) {48+ // Ignore49+ next();50+ }51+ });52+}53+54+function proxyCache(req, res){55+ var writeHead = res.writeHead56+ , write = res.write57+ , end = res.end58+ , ok = false59+ , cache = { body: '' };60+61+ // Proxy writeHead62+ res.writeHead = function(status, headers){63+ res.writeHead = writeHead;64+ res.writeHead(status, headers);65+ if (status >= 300) return ok = false;66+ cache.status = status;67+ cache.headers = headers;68+ };69+70+ // Proxy write71+ res.write = function(data){72+ res.write = write;73+ cache.body += data;74+ res.write(data);75+ };76+ 77+ // Proxy write78+ res.end = function(data){79+ res.end = end;80+ cache.body += data;81+ res.end(data);82+ 83+ // Serialize / cache data84+ _cache.set(req.url, JSON.stringify(cache));85+ };86+}87+88+// Apply to any route(s) as route-specific middleware89+90+app.get('/', cache, function(req, res){91+ res.render('users', { users: users });92+});93+94+app.listen(3000);95+console.log('Express app started on port 3000');