Skip to content
Browse files

Allow Redback to take an existing Redis client or create a new client.

Redback can now 'use' or wrap itself around a previously existing Redis client.
  • Loading branch information...
1 parent e12c4bc commit 6f62b2f5b08a2aa17b75a92a8055032cf15e839f @chase chase committed Jul 9, 2011
Showing with 50 additions and 13 deletions.
  1. +5 −1 README.md
  2. +21 −6 lib/Redback.js
  3. +24 −6 site/api.html
View
6 README.md
@@ -28,7 +28,11 @@ It also comes with the following advanced data structures:
```javascript
var redback = require('redback').createClient();
-
+```
+or
+```javascript
+var redis = require('redis').createClient();
+var redback = new request('redback').use(redis);
var user3 = redback.createSocialGraph(3);
user3.follow(1, callback);
View
27 lib/Redback.js
@@ -30,18 +30,21 @@ var advanced = ['KeyPair','DensitySet','CappedList','SocialGraph',
* The Redback object wraps the Redis client and acts as a factory
* for structures.
*
+ * @param {RedisClient} client
+ * @param {Object} options (optional)
+ * or
* @param {string} host (optional)
* @param {int} port (optional)
* @param {Object} options (optional)
* @api public
*/
-var Redback = exports.Redback = function (host, port, options) {
- this.client = redis.createClient.apply(this, arguments);
- this.namespace = '';
- if (typeof options === 'object') {
- this.namespace = options.namespace || '';
- }
+var Redback = exports.Redback = function () {
+ this.client = arguments[0] instanceof redis.RedisClient ?
+ arguments[0] : redis.createClient.apply(this, arguments);
+ var options = arguments[1] instanceof Object ?
+ arguments[1] : arguments[2];
+ this.namespace = options.namespace || '';
}
/**
@@ -142,6 +145,18 @@ exports.createClient = function (host, port, options) {
}
/**
+ * Wrap a Redis client with Redback.
+ *
+ * @param {RedisClient} client
+ * @param {Object} options (optional)
+ * @api public
+ */
+
+exports.use = function (client, options) {
+ return new Redback(client, options);
+}
+
+/**
* Add the Redis structures from ./base_structures
*/
View
30 site/api.html
@@ -514,15 +514,19 @@
<h2></h2>
+<ul><li><p><strong>param</strong>: <em>RedisClient</em> client</p></li><li><p><strong>param</strong>: <em>Object</em> options (optional)</p></li></ul>
+
+<p>or</p>
+
<ul><li><p><strong>param</strong>: <em>string</em> host (optional)</p></li><li><p><strong>param</strong>: <em>int</em> port (optional)</p></li><li><p><strong>param</strong>: <em>Object</em> options (optional)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
-<pre><code><span class="keyword">var</span> <span class="class">Redback</span> = <span class="variable">exports</span>.<span class="class">Redback</span> = <span class="keyword">function</span> (<span class="variable">host</span>, <span class="variable">port</span>, <span class="variable">options</span>) {
- <span class="this">this</span>.<span class="variable">client</span> = <span class="variable">redis</span>.<span class="variable">createClient</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
- <span class="this">this</span>.<span class="variable">namespace</span> = <span class="string">''</span>;
- <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span> === <span class="string">'object'</span>) {
- <span class="this">this</span>.<span class="variable">namespace</span> = <span class="variable">options</span>.<span class="variable">namespace</span> || <span class="string">''</span>;
- }
+<pre><code><span class="keyword">var</span> <span class="class">Redback</span> = <span class="variable">exports</span>.<span class="class">Redback</span> = <span class="keyword">function</span> () {
+ <span class="this">this</span>.<span class="variable">client</span> = <span class="variable">arguments</span>[<span class="number integer">0</span>] <span class="variable">instanceof</span> <span class="variable">redis</span>.<span class="class">RedisClient</span> ?
+ <span class="variable">arguments</span>[<span class="number integer">0</span>] : <span class="variable">redis</span>.<span class="variable">createClient</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">arguments</span>);
+ <span class="keyword">var</span> <span class="variable">options</span> = <span class="variable">arguments</span>[<span class="number integer">1</span>] <span class="variable">instanceof</span> <span class="class">Object</span> ?
+ <span class="variable">arguments</span>[<span class="number integer">1</span>] : <span class="variable">arguments</span>[<span class="number integer">2</span>];
+ <span class="this">this</span>.<span class="variable">namespace</span> = <span class="variable">options</span>.<span class="variable">namespace</span> || <span class="string">''</span>;
}</code></pre>
</td>
</tr>
@@ -613,6 +617,20 @@
</tr>
<tr class="code">
<td class="docs">
+<p>Wrap a Redis client with Redback.</p>
+
+<h2></h2>
+
+<ul><li><p><strong>param</strong>: <em>RedisClient</em> client</p></li><li><p><strong>param</strong>: <em>Object</em> options (optional)</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
+</td>
+<td class="code">
+<pre><code><span class="variable">exports</span>.<span class="variable">use</span> = <span class="keyword">function</span> (<span class="variable">client</span>, <span class="variable">options</span>) {
+ <span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Redback</span>(<span class="variable">client</span>, <span class="variable">options</span>);
+}</code></pre>
+</td>
+</tr>
+<tr class="code">
+<td class="docs">
<p>Add the Redis structures from ./base_structures
</p>
</td>

0 comments on commit 6f62b2f

Please sign in to comment.
Something went wrong with that request. Please try again.