Skip to content

Instantly share code, notes, and snippets.

@dvbportal
Created February 5, 2012 12:30
Show Gist options
  • Save dvbportal/1745223 to your computer and use it in GitHub Desktop.
Save dvbportal/1745223 to your computer and use it in GitHub Desktop.
Storing Counters In Redis
// Additional code in node-http-proxy.js
var crypto = require('crypto');
var redis = require('redis');
var client = redis.createClient(config.opt.redis_port, 'cloudno.de');
client.auth(config.opt.redis_auth, function(result) {
util.log("Redis authenticated.");
})
client.on("error", function (err) {
util.log("Redis error: " + err);
});
// .. code to process the request ..
// log to redis
if (client && client.connected) {
var appName = lookup_app(options.port);
if (appName) {
var ip = req.headers["x-forwarded-for"];
var referer = req.headers.referer;
var date = new Date();
var month = date.getUTCMonth() + 1;
var day = date.getUTCFullYear() + "-" + month + "-" + date.getUTCDate();
var urlhash;
var keys = [
"hits-by-app:" + appName,
"hits-by-day:" + day,
"hits-by-app-by-day:" + appName + ':' + day,
"hits-by-app-by-month:" + appName + ':' + month,
"hits-by-ip:" + ip,
"hits-by-ip-by-day:" + ip + ':' + day
];
if (referer) {
urlhash = crypto.createHash("md5").update(referer).digest("hex");
keys.push("hits-by-url:" + urlhash);
keys.push("hits-by-url-by-day:" + urlhash + ":" + day);
client.set("url:" + urlhash, referer);
}
for (i in keys) {
client.incr(keys[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment