Permalink
Browse files

Add redis adapter

  • Loading branch information...
thisandagain committed Oct 3, 2012
1 parent 1f44c32 commit 9f687b5eb2c61269f4edf1bcc13d3f322ef5f28d
Showing with 56 additions and 0 deletions.
  1. +56 −0 lib/adapter.js
View
@@ -0,0 +1,56 @@
+/**
+ * Redis list utilities for troll.
+ *
+ * @package troll
+ * @author Andrew Sliwinski <andrew@diy.org>
+ */
+
+/**
+ * Dependencies
+ */
+var async = require('async');
+
+/**
+ * Constructor
+ */
+function Adapter (client) {
+ var self = this;
+
+ self.client = client;
+ self.ns = 'troll:';
+};
+
+Adapter.prototype.push = function (key, value, callback) {
+ var self = this;
+ self.client.lpush(self.ns + key, value, function (err) {
+ callback(err, value);
+ });
+};
+
+Adapter.prototype.length = function (key, callback) {
+ var self = this;
+ self.client.llen(self.ns + key, callback);
+};
+
+Adapter.prototype.all = function (key, callback) {
+ var self = this;
+ self.client.lrange(self.ns + key, 0, -1, callback);
+};
+
+Adapter.prototype.sum = function (key, callback) {
+ var self = this;
+ self.all(key, function (err, obj) {
+ if (err) {
+ callback(err);
+ } else {
+ async.reduce(obj, 0, function(memo, item, callback){
+ callback(null, Number(item) + memo);
+ }, callback);
+ }
+ });
+};
+
+/**
+ * Export
+ */
+module.exports = Adapter;

0 comments on commit 9f687b5

Please sign in to comment.