Browse files

Initial commit

  • Loading branch information...
0 parents commit 564bea5dfa538146e9800ff1f1a3c0eb2e36df06 @tj committed May 2, 2011
Showing with 287 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +4 −0 .npmignore
  3. +5 −0 History.md
  4. +5 −0 Makefile
  5. +38 −0 Readme.md
  6. +30 −0 examples/scraper.js
  7. +2 −0 index.js
  8. +96 −0 lib/queue.js
  9. +18 −0 lib/redisoo.js
  10. +14 −0 package.json
  11. +59 −0 test/queue.test.js
  12. +13 −0 test/redisoo.test.js
3 .gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+node_modules
+*.sock
4 .npmignore
@@ -0,0 +1,4 @@
+support
+test
+examples
+*.sock
5 History.md
@@ -0,0 +1,5 @@
+
+0.0.1 / 2010-01-03
+==================
+
+ * Initial release
5 Makefile
@@ -0,0 +1,5 @@
+
+test:
+ @expresso -I lib --serial
+
+.PHONY: test
38 Readme.md
@@ -0,0 +1,38 @@
+
+# Redis OO
+
+ Collection of Redis backed data structures for nodejs.
+
+## Structures
+
+ - Queue
+
+## Running Tests
+
+ $ npm install --dev
+ $ make test
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 examples/scraper.js
@@ -0,0 +1,30 @@
+
+/**
+ * Module dependencies.
+ */
+
+var cluster = require('cluster')
+ , lineup = require('../');
+
+var queue = lineup.create();
+
+(function next() {
+ queue.pop(function(err, job){
+ if (job) {
+ console.log('scraping %s', job.url);
+ setTimeout(next, Math.random() * 10000);
+ }
+ });
+})();
+
+setInterval(function(){
+ queue.length(function(err, len){
+ console.log('%d jobs remaining', len);
+ });
+}, 2000);
+
+(function push() {
+ console.log('push job');
+ queue.push({ url: 'https://learnboost.com' });
+ setTimeout(push, Math.random() * 2000);
+})();
2 index.js
@@ -0,0 +1,2 @@
+
+module.exports = require('./lib/lineup');
96 lib/queue.js
@@ -0,0 +1,96 @@
+
+/*!
+ * redisoo - Queue
+ * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var redis = require('redis');
+
+/**
+ * Expose `Queue`.
+ */
+
+module.exports = Queue;
+
+/**
+ * Initialize a `Queue` with the given `key` and optional redis `client`.
+ *
+ * @param {String} key
+ * @param {Object} client
+ * @api private
+ */
+
+function Queue(key, client) {
+ this.key = key;
+ this.client = client || redis.createClient();
+}
+
+/**
+ * Push the given `obj` and callback `fn(err)`.
+ *
+ * @param {String|Object} obj
+ * @param {Function} fn
+ * @return {Queue} for chaining
+ * @api public
+ */
+
+Queue.prototype.push = function(obj, fn){
+ try { obj = JSON.stringify(obj); }
+ catch (err) { return fn(err); }
+ this.client.lpush(this.key, obj, fn || function(){});
+ return this;
+};
+
+/**
+ * Pop and callback `fn(err, obj)`.
+ *
+ * @param {Function} fn
+ * @return {Queue} for chaining
+ * @api public
+ */
+
+Queue.prototype.pop = function(fn){
+ fn = fn || function(){};
+ this.client.rpop(this.key, function(err, obj){
+ if (err) return fn(err);
+ try {
+ fn(null, JSON.parse(obj));
+ } catch (err) {
+ fn(err);
+ }
+ });
+ return this;
+};
+
+/**
+ * Get the length of the queue and callback `fn(err, len)`.
+ *
+ * @param {Function} fn
+ * @return {Queue} for chaining
+ * @api public
+ */
+
+Queue.prototype.length = function(fn){
+ this.client.llen(this.key, fn);
+ return this;
+};
+
+/**
+ * Destroy the queue and invoke `fn(err)`.
+ *
+ * @param {Function} fn
+ * @api public
+ */
+
+Queue.prototype.destroy = function(fn){
+ this.client.del(this.key, fn || function(){});
+ return this;
+};
+
+
+
18 lib/redisoo.js
@@ -0,0 +1,18 @@
+
+/*!
+ * lineup
+ * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Library version.
+ */
+
+exports.version = '0.0.1';
+
+/**
+ * Expose `Queue`.
+ */
+
+exports.Queue = Queue = require('./queue');
14 package.json
@@ -0,0 +1,14 @@
+{
+ "name": "redisoo"
+ , "version": "0.0.1"
+ , "description": "Redis backed data structures"
+ , "keywords": ["redis", "queue"]
+ , "author": "TJ Holowaychuk <tj@vision-media.ca>"
+ , "dependencies": { "redis" : "0.6.x" }
+ , "devDependencies": {
+ "expresso": ">= 0.0.1"
+ , "should": ">= 0.0.1"
+ }
+ , "main": "index"
+ , "engines": { "node": "0.4.x" }
+}
59 test/queue.test.js
@@ -0,0 +1,59 @@
+
+/**
+ * Module dependencies.
+ */
+
+var Queue = require('redisoo').Queue
+ , should = require('should')
+ , redis = require('redis');
+
+var queue = new Queue('todos');
+
+module.exports = {
+ setup: function(fn){
+ queue.destroy(fn);
+ },
+
+ '.push(str)': function(done){
+ queue.push('one');
+ queue.push('two');
+ queue.push('three', function(err){
+ should.equal(null, err);
+ queue.length(function(err, len){
+ should.equal(null, err);
+ len.should.equal(3);
+ done();
+ });
+ });
+ },
+
+ '.pop()': function(done){
+ queue.push('one');
+ queue.push('two');
+ queue.push('three');
+ queue.pop(function(err, val){
+ should.equal(null, err);
+ val.should.equal('one');
+ queue.pop(function(err, val){
+ should.equal(null, err);
+ val.should.equal('two');
+ queue.length(function(err, len){
+ len.should.equal(1);
+ done();
+ });
+ })
+ });
+ },
+
+ '.push(obj)': function(done){
+ queue.push({ some: 'stuff' });
+ queue.pop(function(err, obj){
+ obj.should.eql({ some: 'stuff' });
+ done();
+ });
+ },
+
+ after: function(){
+ queue.client.end();
+ }
+};
13 test/redisoo.test.js
@@ -0,0 +1,13 @@
+
+/**
+ * Module dependencies.
+ */
+
+var oo = require('redisoo')
+ , should = require('should');
+
+module.exports = {
+ 'test .version': function(){
+ oo.version.should.match(/^\d+\.\d+\.\d+$/);
+ }
+};

0 comments on commit 564bea5

Please sign in to comment.