Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added CappedList

  • Loading branch information...
commit 6f7320ce7aaa10d9a2cf0053617dcb3b056cb4eb 1 parent d86c173
@tj authored
View
83 lib/objects/capped-list.js
@@ -0,0 +1,83 @@
+
+/*!
+ * rediskit - CappedList
+ * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var redis = require('redis')
+ , List = require('./list');
+
+/**
+ * Expose `CappedList`.
+ */
+
+module.exports = CappedList;
+
+/**
+ * Initialize a `List` with the given `key` and optional `limit` / redis `client`.
+ *
+ * @param {String} key
+ * @param {Number} limit
+ * @param {Object} client
+ * @api private
+ */
+
+function CappedList(key, limit, client) {
+ List.call(this, key, client);
+ this.setLimit(limit || 1000);
+}
+
+/**
+ * Inherit from `List.prototype`.
+ */
+
+CappedList.prototype.__proto__ = List.prototype;
+
+/**
+ * Set the cap limit to `n`.
+ *
+ * @param {Number} n
+ * @return {CappedList} for chaining
+ * @api public
+ */
+
+CappedList.prototype.setLimit = function(n){
+ this.limit = n - 1;
+ return this;
+};
+
+/**
+ * Push `str` to the head and optionally callback `fn(err)`.
+ *
+ * @param {String} str
+ * @param {Function} fn
+ * @return {CappedList} for chaining
+ * @api public
+ */
+
+CappedList.prototype.lpush = function(str, fn){
+ List.prototype.lpush.call(this, str, fn);
+ this.client.ltrim(this.key, 0, this.limit);
+ return this;
+};
+
+/**
+ * Push `str` to the tail and optionally callback `fn(err)`.
+ *
+ * @param {String} str
+ * @param {Function} fn
+ * @return {CappedList} for chaining
+ * @api public
+ */
+
+CappedList.prototype.rpush = function(str, fn){
+ List.prototype.rpush.call(this, str, fn);
+ this.client.ltrim(this.key, 0, this.limit);
+ return this;
+};
+
View
1  lib/rediskit.js
@@ -27,6 +27,7 @@ exports.List = require('./objects/list');
exports.Set = require('./objects/set');
exports.Hash = require('./objects/hash');
exports.Queue = require('./objects/queue');
+exports.CappedList = require('./objects/capped-list');
/**
* Shared client.
View
42 test/capped-list.test.js
@@ -0,0 +1,42 @@
+
+/**
+ * Module dependencies.
+ */
+
+var CappedList = require('rediskit').CappedList
+ , should = require('should')
+ , redis = require('redis');
+
+var logs = new CappedList('logs', 5);
+
+module.exports = {
+ setup: function(fn){
+ logs.destroy(fn);
+ },
+
+ '.rpush(str)': function(done){
+ var n = 15;
+ while (n--) logs.rpush(n);
+ logs.length(function(err, len){
+ should.equal(null, err);
+ len.should.equal(5);
+ logs.all(function(err, lines){
+ lines.should.eql(['14', '13', '12', '11', '10']);
+ done();
+ });
+ });
+ },
+
+ '.lpush(str)': function(done){
+ var n = 15;
+ while (n--) logs.lpush(n);
+ logs.length(function(err, len){
+ should.equal(null, err);
+ len.should.equal(5);
+ logs.all(function(err, lines){
+ lines.should.eql(['0', '1', '2', '3', '4']);
+ done();
+ });
+ });
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.