Guest User

redis works

a guest
Mar 29th, 2013
37
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var express = require('express')
  2.   , redis = require('redis');
  3.  
  4. var db = redis.createClient();
  5. var app = express();
  6.  
  7. app.use(function(req, res, next){
  8.   var ua = req.headers['user-agent'];
  9.   db.zadd('online', Date.now(), ua, next);
  10. });
  11.  
  12. app.use(function(req, res, next){
  13.   var min = 60 * 1000;
  14.   var ago = Date.now() - min;
  15.   db.zrevrangebyscore('online', '+inf', ago, function(err, users){
  16.     if (err) return next(err);
  17.     req.online = users;
  18.     next();
  19.   });
  20. });
  21.  
  22. app.get('/', function(req, res){
  23.   res.send(req.online.length + ' users online');
  24. });
  25.  
  26. app.listen(3000);
RAW Paste Data