Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
3829 lines (3374 sloc) 279 KB
<a href="https://github.com/chriso/redback"><img alt="Fork me on GitHub" id="ribbon" src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png"></a><html>
<head>
<title>Redback</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<style>body {
margin: 0;
padding: 0;
font: 14px/1.5 'Palatino Linotype', 'Book Antiqua', Palatino, FreeSerif, serif;
color: #252519;
}
a {
color: #252519;
}
a:hover {
text-decoration: underline;
color: #19469D;
}
p {
margin: 12px 0;
}
h1, h2, h3 {
margin: 0;
padding: 0;
}
table#source {
width: 100%;
border-collapse: collapse;
}
table#source td:first-child {
padding: 30px 40px 30px 40px;
vertical-align: top;
}
table#source td:first-child,
table#source td:first-child pre {
width: 450px;
}
table#source td:last-child {
padding: 30px 0 30px 40px;
border-left: 1px solid #E5E5EE;
background: #F5F5FF;
}
table#source tr {
border-bottom: 1px solid #E5E5EE;
}
table#source tr.filename {
padding-top: 40px;
border-top: 1px solid #E5E5EE;
}
table#source tr.filename td:first-child {
text-transform: capitalize;
}
table#source tr.filename td:last-child {
font-size: 12px;
}
table#source tr.filename h2 {
margin: 0;
padding: 0;
cursor: pointer;
}
table#source tr.code h1,
table#source tr.code h2,
table#source tr.code h3 {
margin-top: 30px;
font-family: "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
font-size: 18px;
}
table#source tr.code h2 {
font-size: 16px;
}
table#source tr.code h3 {
font-size: 14px;
}
table#source tr.code ul {
margin: 15px 0 15px 35px;
padding: 0;
}
table#source tr.code ul li {
margin: 0;
padding: 1px 0;
}
table#source tr.code ul li p {
margin: 0;
padding: 0;
}
table#source tr.code td:first-child pre {
padding: 20px;
}
#ribbon {
position: fixed;
top: 0;
right: 0;
}
code .string { color: #219161; }
code .regexp { color: #219161; }
code .keyword { color: #954121; }
code .number { color: #19469D; }
code .comment { color: #bbb; }
code .this { color: #19469D; }</style>
<script>
$(function(){
$('tr.code').hide();
$('tr.filename').toggle(function(){
$(this).nextUntil('.filename').fadeIn();
}, function(){
$(this).nextUntil('.filename').fadeOut();
});
});
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-286321-6']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<table id="source"><tbody><tr><td><h1>Redback</h1><p>A high-level <strong>Redis</strong> library for <strong>Node.JS</strong></p></td><td></td></tr><tr class="filename"><td><h2 id="lib/Structure.js"><a href="#">Structure</a></h2></td><td>lib/Structure.js</td></tr><tr class="code">
<td class="docs">
<p>All Redback structures inherit from this.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">exports</span>.<span class="class">Structure</span> = <span class="keyword">function</span> () {};</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Create a new Structure by extending the base Structure.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Object</em> methods (optional)</p></li><li><p><strong>return</strong>: <em>structure</em></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="keyword">new</span> = <span class="keyword">function</span> (<span class="variable">methods</span>) {
<span class="keyword">return</span> <span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">extend</span>(<span class="variable">methods</span>);
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Expire the structure after a certain number of seconds.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> ttl</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">expire</span> = <span class="keyword">function</span> (<span class="variable">ttl</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">expire</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">ttl</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Expire the structure at a certain date.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Date</em> when</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">expireAt</span> = <span class="keyword">function</span> (<span class="variable">when</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">when</span>.<span class="variable">getTime</span> === <span class="string">'function'</span>) {
<span class="variable">when</span> = <span class="class">Math</span>.<span class="variable">round</span>(<span class="variable">when</span>.<span class="variable">getTime</span>() <span class="regexp">/ 1000); /</span>/<span class="variable">ms</span> =&<span class="variable">gt</span>; <span class="variable">s</span>
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">expireat</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">when</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the number of seconds before the structure expires.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">ttl</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">ttl</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks whether the structure has an expiry.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">isVolatile</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">ttl</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">ttl</span>) {
<span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>, <span class="keyword">null</span>);
<span class="variable">callback</span>(<span class="keyword">null</span>, <span class="variable">ttl</span> != -<span class="number integer">1</span>);
});
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove the structure's associated expiry.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">persist</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">persist</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove the structure from the Redis database.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">destroy</span> =
<span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">flush</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">del</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A helper for creating atomically auto-incrementing keys.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">autoincrement</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="keyword">var</span> <span class="variable">key</span> = <span class="this">this</span>.<span class="variable">key</span> + <span class="string">':_autoinc'</span>,
<span class="variable">multi</span> = <span class="this">this</span>.<span class="variable">client</span>.<span class="variable">multi</span>();
<span class="variable">multi</span>.<span class="variable">setnx</span>(<span class="variable">key</span>, <span class="number integer">1</span>).<span class="variable">get</span>(<span class="variable">key</span>).<span class="variable">incr</span>(<span class="variable">key</span>);
<span class="variable">multi</span>.<span class="variable">exec</span>(<span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">replies</span>) {
<span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>, <span class="keyword">null</span>);
<span class="variable">callback</span>(<span class="keyword">null</span>, <span class="variable">replies</span>[<span class="number integer">1</span>]);
});
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Takes a redback structure or key string and returns the key.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Object</em> key</p></li><li><p><strong>return</strong>: <em>string</em> key</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">getKey</span> = <span class="keyword">function</span> (<span class="variable">key</span>, <span class="variable">which</span>) {
<span class="variable">which</span> = <span class="variable">which</span> || <span class="string">'key'</span>;
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">key</span>[<span class="variable">which</span>] !== <span class="string">'undefined'</span>) {
<span class="keyword">return</span> <span class="variable">key</span>[<span class="variable">which</span>];
}
<span class="keyword">return</span> <span class="variable">key</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A helper that extracts the Redis keys from many Structure or string arguments.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Array</em> structures</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">getKeys</span> = <span class="keyword">function</span> (<span class="variable">structures</span>, <span class="variable">which</span>) {
<span class="keyword">var</span> <span class="variable">structures</span> = <span class="class">Array</span>.<span class="variable">prototype</span>.<span class="variable">slice</span>.<span class="variable">call</span>(<span class="variable">structures</span>),
<span class="variable">callback</span> = <span class="variable">structures</span>.<span class="variable">pop</span>(),
<span class="variable">self</span> = <span class="this">this</span>,
<span class="variable">keys</span> = [];
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> = <span class="number integer">0</span>, <span class="variable">l</span> = <span class="variable">structures</span>.<span class="variable">length</span>; <span class="variable">i</span> &<span class="variable">lt</span>; <span class="variable">l</span>; <span class="variable">i</span>++) {
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">structures</span>[<span class="variable">i</span>])) {
<span class="variable">structures</span>[<span class="variable">i</span>].<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">structure</span>) {
<span class="variable">keys</span>.<span class="variable">push</span>(<span class="variable">self</span>.<span class="variable">getKey</span>(<span class="variable">structure</span>, <span class="variable">which</span>));
});
} <span class="keyword">else</span> {
<span class="variable">keys</span>.<span class="variable">push</span>(<span class="this">this</span>.<span class="variable">getKey</span>(<span class="variable">structures</span>[<span class="variable">i</span>], <span class="variable">which</span>));
}
}
<span class="variable">keys</span>.<span class="variable">push</span>(<span class="variable">callback</span>);
<span class="keyword">return</span> <span class="variable">keys</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add the namespace on to a key.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> key</p></li><li><p><strong>return</strong>: <em>string</em> namespaced_key</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">namespaceKey</span> = <span class="keyword">function</span> (<span class="variable">key</span>) {
<span class="variable">key</span> = <span class="variable">key</span> || <span class="string">''</span>;
<span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">namespace</span>.<span class="variable">length</span>) {
<span class="variable">key</span> = <span class="this">this</span>.<span class="variable">namespace</span> + <span class="string">':'</span> + <span class="variable">key</span>;
}
<span class="keyword">return</span> <span class="variable">key</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Extend the structure.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Object</em> methods (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">extend</span> = <span class="keyword">function</span> (<span class="variable">methods</span>) {
<span class="keyword">var</span> <span class="variable">structure</span> = <span class="keyword">function</span> (<span class="variable">client</span>, <span class="variable">key</span>, <span class="variable">namespace</span>, <span class="variable">init_args</span>) {
<span class="this">this</span>.<span class="variable">client</span> = <span class="variable">client</span>;
<span class="this">this</span>.<span class="variable">namespace</span> = <span class="variable">namespace</span> || <span class="string">''</span>;
<span class="keyword">if</span> (!<span class="variable">key</span>) {
<span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'A key is required'</span>);
}
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">key</span>)) {
<span class="variable">key</span> = <span class="variable">key</span>.<span class="variable">join</span>(<span class="string">':'</span>);
}
<span class="this">this</span>.<span class="variable">id</span> = <span class="variable">key</span>;
<span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">namespace</span>.<span class="variable">length</span>) {
<span class="variable">key</span> = <span class="this">this</span>.<span class="variable">namespace</span> + <span class="string">':'</span> + <span class="variable">key</span>;
}
<span class="this">this</span>.<span class="variable">key</span> = <span class="variable">key</span>;
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="this">this</span>.<span class="variable">init</span> === <span class="string">'function'</span>) {
<span class="this">this</span>.<span class="variable">init</span>.<span class="variable">apply</span>(<span class="this">this</span>, <span class="variable">init_args</span>);
}
}, <span class="variable">ctor</span> = <span class="keyword">function</span> () {
<span class="this">this</span>.<span class="variable">constructor</span> = <span class="variable">structure</span>;
}
<span class="variable">ctor</span>.<span class="variable">prototype</span> = <span class="this">this</span>;
<span class="variable">structure</span>.<span class="variable">prototype</span> = <span class="keyword">new</span> <span class="variable">ctor</span>;
<span class="variable">structure</span>.<span class="variable">__super__</span> = <span class="this">this</span>;
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">methods</span> === <span class="string">'object'</span>) {
<span class="keyword">for</span> (<span class="keyword">var</span> <span class="variable">i</span> <span class="keyword">in</span> <span class="variable">methods</span>) {
<span class="variable">structure</span>.<span class="variable">prototype</span>[<span class="variable">i</span>] = <span class="variable">methods</span>[<span class="variable">i</span>];
}
}
<span class="keyword">return</span> <span class="variable">structure</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Create a random key for temporary use.</p>
<h2></h2>
<ul><li><p><strong>return</strong>: <em>string</em> random_key</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">randomKey</span> = <span class="keyword">function</span> () {
<span class="keyword">return</span> <span class="class">Math</span>.<span class="variable">random</span>();
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the type of the current structure.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">type</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">type</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Rename the structure (change the Redis key).</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> new_key</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">rename</span> = <span class="keyword">function</span> (<span class="variable">new_key</span>, <span class="variable">callback</span>) {
<span class="keyword">var</span> <span class="variable">self</span> = <span class="this">this</span>;
<span class="variable">new_key</span> = <span class="this">this</span>.<span class="variable">namespaceKey</span>(<span class="variable">new_key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">rename</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">new_key</span>, <span class="keyword">function</span> (<span class="variable">err</span>) {
<span class="keyword">if</span> (<span class="variable">err</span>) <span class="keyword">return</span> <span class="variable">callback</span>(<span class="variable">err</span>, <span class="keyword">null</span>);
<span class="variable">self</span>.<span class="variable">key</span> = <span class="variable">new_key</span>;
<span class="variable">callback</span>();
});
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sort all elements in the structure.</p>
<h2>Options</h2>
<p> limit, offset, by, get, alpha, desc, store</p>
<h2>Reference</h2>
<p> http://redis.io/commands/sort</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>object</em> options</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Structure</span>.<span class="variable">prototype</span>.<span class="variable">sort</span> = <span class="keyword">function</span> (<span class="variable">options</span>, <span class="variable">callback</span>) {
<span class="keyword">var</span> <span class="variable">args</span> = [<span class="this">this</span>.<span class="variable">key</span>];
<span class="comment">//SORT key [BY pattern] [LIMIT offset count]</span>
<span class="comment">// [GET pattern [GET pattern ...]] [ASC|DESC] [ALPHA] [STORE destination]</span>
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">by</span> !== <span class="string">'undefined'</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'BY'</span>, <span class="variable">options</span>.<span class="variable">by</span>);
}
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">limit</span> !== <span class="string">'undefined'</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'LIMIT'</span>);
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">offset</span> !== <span class="string">'undefined'</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="variable">options</span>.<span class="variable">offset</span>);
}
<span class="variable">args</span>.<span class="variable">push</span>(<span class="variable">options</span>.<span class="variable">limit</span>);
}
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">get</span> !== <span class="string">'undefined'</span>) {
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">options</span>.<span class="variable">get</span>)) {
<span class="variable">options</span>.<span class="variable">get</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">pattern</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'GET'</span>, <span class="variable">pattern</span>);
});
} <span class="keyword">else</span> {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'GET'</span>, <span class="variable">options</span>.<span class="variable">get</span>);
}
}
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">desc</span> !== <span class="string">'undefined'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span>.<span class="variable">desc</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'DESC'</span>);
}
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">alpha</span> !== <span class="string">'undefined'</span> &<span class="variable">amp</span>;&<span class="variable">amp</span>; <span class="variable">options</span>.<span class="variable">alpha</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'ALPHA'</span>);
}
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">options</span>.<span class="variable">store</span> !== <span class="string">'undefined'</span>) {
<span class="variable">args</span>.<span class="variable">push</span>(<span class="string">'STORE'</span>, <span class="variable">options</span>.<span class="variable">store</span>);
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sort</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">args</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/Redback.js"><a href="#">Redback</a></h2></td><td>lib/Redback.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">redis</span> = <span class="variable">require</span>(<span class="string">'redis'</span>),
<span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'./Structure'</span>),
<span class="class">Channel</span> = <span class="variable">require</span>(<span class="string">'./Channel'</span>).<span class="class">Channel</span>,
<span class="class">Cache</span> = <span class="variable">require</span>(<span class="string">'./Cache'</span>).<span class="class">Cache</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Define the available Redis structures.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">base</span> = [<span class="string">'Hash'</span>,<span class="string">'List'</span>,<span class="string">'Set'</span>,<span class="string">'SortedSet'</span>,<span class="string">'Bitfield'</span>];</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Define the available advanced structures.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="variable">advanced</span> = [<span class="string">'KeyPair'</span>,<span class="string">'DensitySet'</span>,<span class="string">'CappedList'</span>,<span class="string">'SocialGraph'</span>,
<span class="string">'FullText'</span>, <span class="string">'Queue'</span>, <span class="string">'RateLimit'</span>, <span class="string">'BloomFilter'</span>];</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>The Redback object wraps the Redis client and acts as a factory
for structures.</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></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="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>
<tr class="code">
<td class="docs">
<p>Create a new Cache.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> namespace (optional)</p></li><li><p><strong>return</strong>: <em>Cache</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="variable">createCache</span> = <span class="keyword">function</span> (<span class="variable">namespace</span>) {
<span class="variable">namespace</span> = <span class="variable">namespace</span> || <span class="string">'cache'</span>;
<span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">namespace</span>.<span class="variable">length</span>) {
<span class="variable">namespace</span> = <span class="this">this</span>.<span class="variable">namespace</span> + <span class="string">':'</span> + <span class="variable">namespace</span>;
}
<span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Cache</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">namespace</span>);
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Create a new Channel.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> channel - the channel name</p></li><li><p><strong>return</strong>: <em>Channel</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="variable">createChannel</span> = <span class="keyword">function</span> (<span class="variable">channel</span>) {
<span class="keyword">if</span> (!<span class="variable">channel</span>) {
<span class="keyword">throw</span> <span class="keyword">new</span> <span class="class">Error</span>(<span class="string">'A channel key is required'</span>);
}
<span class="keyword">if</span> (<span class="this">this</span>.<span class="variable">namespace</span>.<span class="variable">length</span>) {
<span class="variable">channel</span> = <span class="this">this</span>.<span class="variable">namespace</span> + <span class="string">':'</span> + <span class="variable">channel</span>;
}
<span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Channel</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">channel</span>);
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Send a (BG)SAVE command to Redis.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> background (optional - default is false)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="variable">save</span> = <span class="keyword">function</span> (<span class="variable">background</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">background</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">background</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">save</span>(<span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">bgsave</span>(<span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Close the connection to Redis.</p>
<ul><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="variable">quit</span> = <span class="keyword">function</span> () {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">quit</span>();
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Create a new Redback client.</p>
<h2></h2>
<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="variable">exports</span>.<span class="variable">createClient</span> = <span class="keyword">function</span> (<span class="variable">host</span>, <span class="variable">port</span>, <span class="variable">options</span>) {
<span class="keyword">return</span> <span class="keyword">new</span> <span class="class">Redback</span>(<span class="variable">host</span>, <span class="variable">port</span>, <span class="variable">options</span>);
}</code></pre>
</td>
</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>
<td class="code">
<pre><code><span class="variable">base</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">structure</span>) {
<span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="variable">addStructure</span>(<span class="variable">structure</span>,
<span class="variable">require</span>(<span class="string">'./base_structures/'</span> + <span class="variable">structure</span>)[<span class="variable">structure</span>]);
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add the advanced structures from ./advanced_structures
</p>
</td>
<td class="code">
<pre><code><span class="variable">advanced</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">structure</span>) {
<span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="variable">addStructure</span>(<span class="variable">structure</span>,
<span class="variable">require</span>(<span class="string">'./advanced_structures/'</span> + <span class="variable">structure</span>)[<span class="variable">structure</span>]);
});</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Redis constants.
</p>
</td>
<td class="code">
<pre><code><span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="class">INF</span> = <span class="variable">exports</span>.<span class="class">INF</span> = <span class="string">'+inf'</span>;
<span class="class">Redback</span>.<span class="variable">prototype</span>.<span class="class">NINF</span> = <span class="variable">exports</span>.<span class="class">NINF</span> = <span class="string">'-inf'</span>;</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Export prototypes so that they can be extended.
</p>
</td>
<td class="code">
<pre><code><span class="variable">exports</span>.<span class="class">Client</span> = <span class="variable">redis</span>.<span class="class">RedisClient</span>;
<span class="variable">exports</span>.<span class="class">Structure</span> = <span class="class">Structure</span>.<span class="class">Structure</span>;
<span class="variable">exports</span>.<span class="class">Cache</span> = <span class="class">Cache</span>;
<span class="variable">exports</span>.<span class="class">Channel</span> = <span class="class">Channel</span>;
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/base_structures/Bitfield.js"><a href="#">Bitfield</a></h2></td><td>lib/base_structures/Bitfield.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'../Structure'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Wrap the Redis bit commands.</p>
<h2>Usage</h2>
<p> <code>redback.createBitfield(key);</code></p>
<h2>Reference</h2>
<p> http://redis.io/commands#string</p>
<h2>Redis Structure</h2>
<p> <code>(namespace:)key = string</code>
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Bitfield</span> = <span class="variable">exports</span>.<span class="class">Bitfield</span> = <span class="class">Structure</span>.<span class="keyword">new</span>();</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get a single bit</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> bit</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Bitfield</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">bit</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">getbit</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">bit</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Set a single bit. The callback receives the previous value.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> bit</p></li><li><p><strong>param</strong>: <em>bool</em> value</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Bitfield</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">bit</span>, <span class="variable">value</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">setbit</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">bit</span>, <span class="variable">value</span> ? <span class="number integer">1</span> : <span class="number integer">0</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/base_structures/Hash.js"><a href="#">Hash</a></h2></td><td>lib/base_structures/Hash.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'../Structure'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A wrapper for the Redis hash type.</p>
<h2>Usage</h2>
<p> <code>redback.createHash(key);</code></p>
<h2>Reference</h2>
<p> http://redis.io/topics/data-types#hashes</p>
<h2>Redis Structure</h2>
<p> <code>(namespace:)key = hash(key =&gt; value)</code>
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Hash</span> = <span class="variable">exports</span>.<span class="class">Hash</span> = <span class="class">Structure</span>.<span class="keyword">new</span>();</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get an array of hash keys.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">keys</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hkeys</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get an array of hash values.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">values</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hvals</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the number of hash keys.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">length</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hlen</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Delete a hash key.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> hash_key</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="keyword">delete</span> = <span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">del</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hdel</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Checks whether a hash key exists.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> hash_key</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">exists</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hexists</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets one or more key/value pairs.</p>
<p>To set one key/value pair:
<code>hash.set('foo', 'bar', callback);</code></p>
<p>To set multiple:
<code>hash.set({key1:'value1', key2:'value2}, callback);</code></p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Object</em> hash_key</p></li><li><p><strong>param</strong>: <em>string</em> value (optional)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">value</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">hash_key</span> === <span class="string">'object'</span>) {
<span class="variable">callback</span> = <span class="variable">value</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hmset</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hset</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">value</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Sets a key/value pair if the key doesn't already exist.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> hash_key</p></li><li><p><strong>param</strong>: <em>string</em> value</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">value</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hsetnx</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">value</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Gets one or more key/value pairs.</p>
<p>To get all key/value pairs in the hash:
<code>hash.get('foo', callback);</code></p>
<p>To get certain key/value pairs:
<code>hash.get(['foo','bar'], callback);</code>
<code>hash.get('foo', callback);</code></p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> hash_key (optional)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">hash_key</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">hash_key</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hgetall</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">hash_key</span>)) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hmget</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">callback</span>)
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hget</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Increment the specified hash value.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> hash_key</p></li><li><p><strong>param</strong>: <em>int</em> amount (optional - default is 1)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">increment</span> =
<span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">incrBy</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">amount</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">amount</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">amount</span>;
<span class="variable">amount</span> = <span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hincrby</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, <span class="variable">amount</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Decrement the specified hash value.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> hash_key</p></li><li><p><strong>param</strong>: <em>int</em> amount (optional - default is 1)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">decrement</span> =
<span class="class">Hash</span>.<span class="variable">prototype</span>.<span class="variable">decrBy</span> = <span class="keyword">function</span> (<span class="variable">hash_key</span>, <span class="variable">amount</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">amount</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">amount</span>;
<span class="variable">amount</span> = <span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">hincrby</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">hash_key</span>, -<span class="number integer">1</span> * <span class="variable">amount</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/base_structures/List.js"><a href="#">List</a></h2></td><td>lib/base_structures/List.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'../Structure'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A wrapper for the Redis list type.</p>
<h2>Usage</h2>
<p> <code>redback.createList(key);</code></p>
<h2>Reference</h2>
<p> http://redis.io/topics/data-types#lists</p>
<h2>Redis Structure</h2>
<p> <code>(namespace:)key = list(values)</code>
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">List</span> = <span class="variable">exports</span>.<span class="class">List</span> = <span class="class">Structure</span>.<span class="keyword">new</span>();</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the list as an array.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">values</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lrange</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="number integer">0</span>, -<span class="number integer">1</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get a range of list elements.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> start</p></li><li><p><strong>param</strong>: <em>count</em> end (optional - defaults to the last element)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">range</span> = <span class="keyword">function</span> (<span class="variable">start</span>, <span class="variable">end</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">end</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">end</span>;
<span class="variable">end</span> = -<span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lrange</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">start</span>, <span class="variable">end</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get one or more elements starting at the specified index.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> index</p></li><li><p><strong>param</strong>: <em>count</em> count (optional - default is 1)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">index</span>, <span class="variable">count</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">count</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">count</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lindex</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">index</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lrange</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">index</span>, <span class="variable">index</span> + <span class="variable">count</span> - <span class="number integer">1</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Cap the length of the list.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> length</p></li><li><p><strong>param</strong>: <em>bool</em> keep_earliest (optional - default is false)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">cap</span> = <span class="keyword">function</span> (<span class="variable">length</span>, <span class="variable">keep_earliest</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">var</span> <span class="variable">start</span> = <span class="number integer">0</span>, <span class="variable">end</span> = -<span class="number integer">1</span>;
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">keep_earliest</span> === <span class="string">'function'</span>) {
<span class="comment">//Keep the last `length` elements</span>
<span class="variable">start</span> = -<span class="number integer">1</span> * <span class="variable">length</span>;
<span class="variable">callback</span> = <span class="variable">keep_earliest</span>;
} <span class="keyword">else</span> {
<span class="comment">//Keep the first `length` elements</span>
<span class="variable">end</span> = <span class="variable">length</span> - <span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">ltrim</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">start</span>, <span class="variable">end</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove one or more list elements matching the value.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> value</p></li><li><p><strong>param</strong>: <em>bool</em> count (optional - default is 1)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> (<span class="variable">value</span>, <span class="variable">count</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">count</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">count</span>;
<span class="variable">count</span> = <span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lrem</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">count</span>, <span class="variable">value</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Trim a list to the specified bounds.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> start</p></li><li><p><strong>param</strong>: <em>int</em> end</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">trim</span> = <span class="keyword">function</span> (<span class="variable">start</span>, <span class="variable">end</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">ltrim</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">start</span>, <span class="variable">end</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Insert an element before the specified pivot.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> pivot</p></li><li><p><strong>param</strong>: <em>string</em> value</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">insertBefore</span> = <span class="keyword">function</span> (<span class="variable">pivot</span>, <span class="variable">value</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">linsert</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="string">'BEFORE'</span>, <span class="variable">pivot</span>, <span class="variable">value</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Insert an element after the specified pivot.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> pivot</p></li><li><p><strong>param</strong>: <em>string</em> value</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">insertAfter</span> = <span class="keyword">function</span> (<span class="variable">pivot</span>, <span class="variable">value</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">linsert</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="string">'AFTER'</span>, <span class="variable">pivot</span>, <span class="variable">value</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Set the element at the specified index.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> index</p></li><li><p><strong>param</strong>: <em>string</em> value</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">set</span> = <span class="keyword">function</span> (<span class="variable">index</span>, <span class="variable">value</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lset</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">index</span>, <span class="variable">value</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the number of elements in the list.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">length</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">llen</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get and remove the last element in the list. The first param can be used
to block the process and wait until list elements are available. If the list
is empty in both examples below, the first example will return <code>null</code>, while
the second will wait for up to 3 seconds. If a list element becomes available
during the 3 seconds it will be returned, otherwise <code>null</code> will be returned.</p>
<h2>Example</h2>
<p> <code>list.shift(callback);</code></p>
<h2>Blocking Example</h2>
<p> <code>list.shift(3, callback)</code></p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> wait (optional) - seconds to block</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">shift</span> = <span class="keyword">function</span> (<span class="variable">wait</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">wait</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">wait</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lpop</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">blpop</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">wait</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get and remove the last element in the list. The first param can be used
to block the process and wait until list elements are available. If the list
is empty in both examples below, the first example will return <code>null</code>, while
the second will wait for up to 3 seconds. If a list element becomes available
during the 3 seconds it will be returned, otherwise <code>null</code> will be returned.</p>
<h2>Example</h2>
<p> <code>list.pop(callback);</code></p>
<h2>Blocking Example</h2>
<p> <code>list.pop(3, callback)</code></p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> wait (optional) - seconds to block</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">pop</span> = <span class="keyword">function</span> (<span class="variable">wait</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">wait</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">wait</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">rpop</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">brpop</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">wait</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add one or more elements to the start of the list.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | array</em> value(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">unshift</span> = <span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">lpush</span> = <span class="keyword">function</span> (<span class="variable">values</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">values</span>)) {
<span class="keyword">var</span> <span class="variable">multi</span> = <span class="this">this</span>.<span class="variable">client</span>.<span class="variable">multi</span>(), <span class="variable">key</span> = <span class="this">this</span>.<span class="variable">key</span>;
<span class="variable">values</span>.<span class="variable">reverse</span>().<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">value</span>) {
<span class="variable">multi</span>.<span class="variable">lpush</span>(<span class="variable">key</span>, <span class="variable">value</span>);
});
<span class="variable">multi</span>.<span class="variable">exec</span>(<span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">lpush</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">values</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add one or more elements to the end of the list.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | array</em> value(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">push</span> = <span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> (<span class="variable">values</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">values</span>)) {
<span class="keyword">var</span> <span class="variable">multi</span> = <span class="this">this</span>.<span class="variable">client</span>.<span class="variable">multi</span>(), <span class="variable">key</span> = <span class="this">this</span>.<span class="variable">key</span>;
<span class="variable">values</span>.<span class="variable">forEach</span>(<span class="keyword">function</span> (<span class="variable">value</span>) {
<span class="variable">multi</span>.<span class="variable">rpush</span>(<span class="variable">key</span>, <span class="variable">value</span>);
});
<span class="variable">multi</span>.<span class="variable">exec</span>(<span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">rpush</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">values</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove the last element of the list and add it to the start
of another list.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>String | List</em> list</p></li><li><p><strong>param</strong>: <em>bool</em> wait (optional) - seconds to block while waiting</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">List</span>.<span class="variable">prototype</span>.<span class="variable">popShift</span> = <span class="keyword">function</span> (<span class="variable">list</span>, <span class="variable">wait</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="variable">list</span> = <span class="this">this</span>.<span class="variable">getKey</span>(<span class="variable">list</span>);
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">wait</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">wait</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">rpoplpush</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">list</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">brpoplpush</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">list</span>, <span class="variable">wait</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}
</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/base_structures/Set.js"><a href="#">Set</a></h2></td><td>lib/base_structures/Set.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'../Structure'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A wrapper for the Redis set type.</p>
<h2>Usage</h2>
<p> <code>redback.createSet(key);</code></p>
<h2>Reference</h2>
<p> http://redis.io/topics/data-types#sets</p>
<h2>Redis Structure</h2>
<p> <code>(namespace:)key = set(elements)</code>
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Set</span> = <span class="variable">exports</span>.<span class="class">Set</span> = <span class="class">Structure</span>.<span class="keyword">new</span>();</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add one or more elements to the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Array</em> element(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">element</span>)) {
<span class="keyword">return</span> <span class="this">this</span>.<span class="variable">addAll</span>(<span class="variable">element</span>, <span class="variable">callback</span>);
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sadd</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove one or more elements from the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Array</em> element(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">element</span>)) {
<span class="keyword">return</span> <span class="this">this</span>.<span class="variable">removeAll</span>(<span class="variable">element</span>, <span class="variable">callback</span>);
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">srem</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get an array of elements in the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">elements</span> = <span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">members</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">smembers</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Move an element to another set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set</em> dest</p></li><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">move</span> = <span class="keyword">function</span> (<span class="variable">dest</span>, <span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">smove</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="this">this</span>.<span class="variable">getKey</span>(<span class="variable">dest</span>), <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Check whether an element exists in the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">exists</span> = <span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">contains</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sismember</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the length (cardinality) of the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">length</span> = <span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">cardinality</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">scard</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get a random element from the set and optionally remove it.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>bool</em> remove (optional - default is false)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">random</span> = <span class="keyword">function</span> (<span class="variable">remove</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">remove</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">remove</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">srandmember</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">spop</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the intersection of one or more sets.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set | Array</em> set(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">inter</span> = <span class="keyword">function</span> (<span class="variable">sets</span>, <span class="variable">callback</span>) {
<span class="variable">sets</span> = <span class="this">this</span>.<span class="variable">getKeys</span>(<span class="variable">arguments</span>);
<span class="variable">sets</span>.<span class="variable">unshift</span>(<span class="this">this</span>.<span class="variable">key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sinter</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">sets</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the intersection of one or more sets and store it another
set (dest).</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set</em> dest</p></li><li><p><strong>param</strong>: <em>string | Set | Array</em> set(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">interStore</span> = <span class="keyword">function</span> (<span class="variable">dest</span>, <span class="variable">sets</span>, <span class="variable">callback</span>) {
<span class="variable">sets</span> = <span class="this">this</span>.<span class="variable">getKeys</span>(<span class="variable">arguments</span>);
<span class="variable">dest</span> = <span class="variable">sets</span>.<span class="variable">shift</span>();
<span class="variable">sets</span>.<span class="variable">unshift</span>(<span class="variable">dest</span>, <span class="this">this</span>.<span class="variable">key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sinterstore</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">sets</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the union of one or more sets.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set | Array</em> set(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">union</span> = <span class="keyword">function</span> (<span class="variable">sets</span>, <span class="variable">callback</span>) {
<span class="variable">sets</span> = <span class="this">this</span>.<span class="variable">getKeys</span>(<span class="variable">arguments</span>);
<span class="variable">sets</span>.<span class="variable">unshift</span>(<span class="this">this</span>.<span class="variable">key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sunion</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">sets</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the union of one or more sets and store it another
set (dest).</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set</em> dest</p></li><li><p><strong>param</strong>: <em>string | Set | Array</em> set(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">unionStore</span> = <span class="keyword">function</span> (<span class="variable">dest</span>, <span class="variable">sets</span>, <span class="variable">callback</span>) {
<span class="variable">sets</span> = <span class="this">this</span>.<span class="variable">getKeys</span>(<span class="variable">arguments</span>);
<span class="variable">dest</span> = <span class="variable">sets</span>.<span class="variable">shift</span>();
<span class="variable">sets</span>.<span class="variable">unshift</span>(<span class="variable">dest</span>, <span class="this">this</span>.<span class="variable">key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sunionstore</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">sets</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the difference of one or more sets.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set | Array</em> set(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">diff</span> = <span class="keyword">function</span> (<span class="variable">sets</span>, <span class="variable">callback</span>) {
<span class="variable">sets</span> = <span class="this">this</span>.<span class="variable">getKeys</span>(<span class="variable">arguments</span>);
<span class="variable">sets</span>.<span class="variable">unshift</span>(<span class="this">this</span>.<span class="variable">key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sdiff</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">sets</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the difference of one or more sets and store it another
set (dest).</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Set</em> dest</p></li><li><p><strong>param</strong>: <em>string | Set | Array</em> set(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">Set</span>.<span class="variable">prototype</span>.<span class="variable">diffStore</span> = <span class="keyword">function</span> (<span class="variable">dest</span>, <span class="variable">sets</span>, <span class="variable">callback</span>) {
<span class="variable">sets</span> = <span class="this">this</span>.<span class="variable">getKeys</span>(<span class="variable">arguments</span>);
<span class="variable">dest</span> = <span class="variable">sets</span>.<span class="variable">shift</span>();
<span class="variable">sets</span>.<span class="variable">unshift</span>(<span class="variable">dest</span>, <span class="this">this</span>.<span class="variable">key</span>);
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">sdiffstore</span>.<span class="variable">apply</span>(<span class="this">this</span>.<span class="variable">client</span>, <span class="variable">sets</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr><tr class="filename"><td><h2 id="lib/base_structures/SortedSet.js"><a href="#">SortedSet</a></h2></td><td>lib/base_structures/SortedSet.js</td></tr><tr class="code">
<td class="docs">
<p>Module dependencies.
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">Structure</span> = <span class="variable">require</span>(<span class="string">'../Structure'</span>);</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>A wrapper for the Redis sorted set (zset) type. Each element has a
score which is used to rank and order all elements in the set. Elements
are ranked from lowest score to highest (the lowest score has
a rank of 0)</p>
<h2>Usage</h2>
<p> <code>redback.createSortedSet(key);</code></p>
<h2>Reference</h2>
<p> http://redis.io/topics/data-types#sorted-sets</p>
<h2>Redis Structure</h2>
<p> <code>(namespace:)key = zset(score =&gt; element)</code>
</p>
</td>
<td class="code">
<pre><code><span class="keyword">var</span> <span class="class">SortedSet</span> = <span class="variable">exports</span>.<span class="class">SortedSet</span> = <span class="class">Structure</span>.<span class="keyword">new</span>();</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Add one or more elements to the set.</p>
<p>To add a single element and score:
<code>set.add(12, 'foo', callback);</code></p>
<p>To add multiple elements/scores:
<code>set.add({foo:12, bar:3}, callback);</code></p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> score (optional)</p></li><li><p><strong>param</strong>: <em>string | Object</em> element(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">add</span> = <span class="keyword">function</span> (<span class="variable">score</span>, <span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">score</span> === <span class="string">'object'</span>) {
<span class="variable">callback</span> = <span class="variable">element</span>;
<span class="variable">element</span> = <span class="variable">score</span>;
<span class="keyword">return</span> <span class="this">this</span>.<span class="variable">addAll</span>(<span class="variable">element</span>, <span class="variable">callback</span>);
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zadd</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">score</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Remove one or more elements from the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string | Array</em> element(s)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">remove</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="class">Array</span>.<span class="variable">isArray</span>(<span class="variable">element</span>)) {
<span class="keyword">return</span> <span class="this">this</span>.<span class="variable">removeAll</span>(<span class="variable">element</span>, <span class="variable">callback</span>);
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zrem</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the number of elements in the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">length</span> = <span class="keyword">function</span> (<span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zcard</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Check whether an element exists in the set.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">exists</span> =
<span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">contains</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zscore</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="keyword">function</span> (<span class="variable">err</span>, <span class="variable">score</span>) {
<span class="variable">callback</span>(<span class="variable">err</span>, <span class="variable">score</span> != <span class="keyword">null</span>);
});
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the rank of the specified element.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">rank</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zrank</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="variable">callback</span>)
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get the score of the specified element.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">score</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">callback</span>) {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zscore</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">element</span>, <span class="variable">callback</span>)
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Increment the specified element's score.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>int</em> amount (optional - default is 1)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em>
;</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">increment</span> =
<span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">incrBy</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">amount</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">amount</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">amount</span>;
<span class="variable">amount</span> = <span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zincrby</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">amount</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Decrement the specified element's score.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>string</em> element</p></li><li><p><strong>param</strong>: <em>int</em> amount (optional - default is 1)</p></li><li><p><strong>param</strong>: <em>Function</em> callback (optional)</p></li><li><p><strong>return</strong>: <em>this</em>
;</p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">decrement</span> =
<span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">decrBy</span> = <span class="keyword">function</span> (<span class="variable">element</span>, <span class="variable">amount</span>, <span class="variable">callback</span>) {
<span class="variable">callback</span> = <span class="variable">callback</span> || <span class="keyword">function</span> () {};
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">amount</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">amount</span>;
<span class="variable">amount</span> = <span class="number integer">1</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zincrby</span>(<span class="this">this</span>.<span class="variable">key</span>, -<span class="number integer">1</span> * <span class="variable">amount</span>, <span class="variable">element</span>, <span class="variable">callback</span>);
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get all elements in the set as an object <code>{element: score, ...}</code>.
If <code>without_scores</code> is true then just an array of elements is returned.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>bool</em> without_scores (optional - scores are included by default)</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">get</span> = <span class="keyword">function</span> (<span class="variable">without_scores</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">without_scores</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">without_scores</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zrange</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="number integer">0</span>, -<span class="number integer">1</span>, <span class="string">'WITHSCORES'</span>, <span class="this">this</span>.<span class="variable">parseScores</span>(<span class="variable">callback</span>));
} <span class="keyword">else</span> {
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zrange</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="number integer">0</span>, -<span class="number integer">1</span>, <span class="variable">callback</span>);
}
<span class="keyword">return</span> <span class="this">this</span>;
}</code></pre>
</td>
</tr>
<tr class="code">
<td class="docs">
<p>Get elements with scores between the specified range. Elements are returned
as an object <code>{element: score, ..}</code> and ordered from highest score to lowest.</p>
<p>Note that the <code>start</code> and <code>end</code> range is inclusive and can be an integer or
the constants <code>redback.INF</code> to represent infinity, or <code>redback.NINF</code> to
represent negative infinity. <code>start</code> must be &lt;= <code>end</code>.</p>
<h2></h2>
<ul><li><p><strong>param</strong>: <em>int</em> start</p></li><li><p><strong>param</strong>: <em>int</em> end</p></li><li><p><strong>param</strong>: <em>int</em> count (optional) - the maximum number of elements to return</p></li><li><p><strong>param</strong>: <em>int</em> offset (optional) - if using count, start at this offset</p></li><li><p><strong>param</strong>: <em>Function</em> callback</p></li><li><p><strong>return</strong>: <em>this</em></p></li><li><p><strong>api</strong>: <em>public</em></p></li></ul>
</td>
<td class="code">
<pre><code><span class="class">SortedSet</span>.<span class="variable">prototype</span>.<span class="variable">getScores</span> = <span class="keyword">function</span> (<span class="variable">start</span>, <span class="variable">end</span>, <span class="variable">count</span>, <span class="variable">offset</span>, <span class="variable">callback</span>) {
<span class="keyword">if</span> (<span class="keyword">null</span> === <span class="variable">start</span>) <span class="variable">start</span> = <span class="string">'-inf'</span>;
<span class="keyword">if</span> (<span class="keyword">null</span> === <span class="variable">end</span>) <span class="variable">end</span> = <span class="string">'+inf'</span>;
<span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">count</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">count</span>;
<span class="this">this</span>.<span class="variable">client</span>.<span class="variable">zrangebyscore</span>(<span class="this">this</span>.<span class="variable">key</span>, <span class="variable">start</span>, <span class="variable">end</span>,
<span class="string">'WITHSCORES'</span>, <span class="this">this</span>.<span class="variable">parseScores</span>(<span class="variable">callback</span>));
<span class="keyword">return</span> <span class="this">this</span>;
} <span class="keyword">else</span> <span class="keyword">if</span> (<span class="keyword">typeof</span> <span class="variable">offset</span> === <span class="string">'function'</span>) {
<span class="variable">callback</span> = <span class="variable">offset</span>;
<span class="variable">offset</span> = <span class="number integer">0</span>;
}
<span class="this">this</span>.<span class="variable">client</span>.<