250 lines
48 KiB
HTML
250 lines
48 KiB
HTML
|
<!DOCTYPE html> <html> <head> <title>redis.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="../../docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To … <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="../..//nconf/stores/file.html"> nconf/stores/file.html </a> <a class="source" href="../..//nconf/stores/memory.html"> nconf/stores/memory.html </a> <a class="source" href="../..//nconf/stores/redis.html"> nconf/stores/redis.html </a> <a class="source" href="../..//nconf/stores.html"> nconf/stores.html </a> <a class="source" href="../..//nconf.html"> nconf.html </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> redis.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">¶</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="cm">/*</span>
|
||
|
<span class="cm"> * redis.js: Redis storage engine for nconf configuration(s)</span>
|
||
|
<span class="cm"> *</span>
|
||
|
<span class="cm"> * (C) 2011, Charlie Robbins</span>
|
||
|
<span class="cm"> *</span>
|
||
|
<span class="cm"> */</span>
|
||
|
|
||
|
<span class="kd">var</span> <span class="nx">async</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'async'</span><span class="p">),</span>
|
||
|
<span class="nx">eyes</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'eyes'</span><span class="p">),</span>
|
||
|
<span class="nx">redis</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'redis'</span><span class="p">),</span>
|
||
|
<span class="nx">nconf</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'nconf'</span><span class="p">),</span>
|
||
|
<span class="nx">Memory</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">'./Memory'</span><span class="p">).</span><span class="nx">Memory</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">¶</a> </div> <h3>function Redis (options)</h3>
|
||
|
|
||
|
<h4>@options {Object} Options for this instance</h4>
|
||
|
|
||
|
<p>Constructor function for the Redis nconf store which maintains
|
||
|
a nested Redis key structure based on key delimiters <code>:</code>.</p>
|
||
|
|
||
|
<p>e.g.
|
||
|
my:nested:key, 'value'
|
||
|
namespace:keys ==> ['my']
|
||
|
namespace:nested:keys ==> ['key']
|
||
|
namespace:nested:key ==> 'value'</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">Redis</span> <span class="o">=</span> <span class="nx">exports</span><span class="p">.</span><span class="nx">Redis</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{}</span>
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">namespace</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">namespace</span> <span class="o">||</span> <span class="s1">'nconf'</span><span class="p">;</span>
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">host</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span> <span class="o">||</span> <span class="s1">'localhost'</span><span class="p">;</span>
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">port</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">port</span> <span class="o">||</span> <span class="mi">6379</span><span class="p">;</span>
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">ttl</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">ttl</span> <span class="o">||</span> <span class="mi">60</span> <span class="o">*</span> <span class="mi">60</span> <span class="o">*</span> <span class="mi">1000</span><span class="p">;</span>
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">cache</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Memory</span><span class="p">();</span>
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">redis</span> <span class="o">=</span> <span class="nx">redis</span><span class="p">.</span><span class="nx">createClient</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">port</span><span class="p">,</span> <span class="nx">options</span><span class="p">.</span><span class="nx">host</span><span class="p">);</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">¶</a> </div> <h3>function get (key, callback)</h3>
|
||
|
|
||
|
<h4>@key {string} Key to retrieve for this instance.</h4>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Retrieves the value for the specified key (if any).</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">get</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
|
||
|
<span class="nx">result</span> <span class="o">=</span> <span class="p">{},</span>
|
||
|
<span class="nx">now</span> <span class="o">=</span> <span class="nb">Date</span><span class="p">.</span><span class="nx">now</span><span class="p">(),</span>
|
||
|
<span class="nx">mtime</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">cache</span><span class="p">.</span><span class="nx">mtimes</span><span class="p">[</span><span class="nx">key</span><span class="p">],</span>
|
||
|
<span class="nx">fullKey</span> <span class="o">=</span> <span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">namespace</span><span class="p">,</span> <span class="nx">key</span><span class="p">);</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">¶</a> </div> <p>If the key exists in the cache and the ttl is less than
|
||
|
the value set for this instance, return from the cache.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">mtime</span> <span class="o">&&</span> <span class="nx">now</span> <span class="o">-</span> <span class="nx">mtime</span> <span class="o"><</span> <span class="k">this</span><span class="p">.</span><span class="nx">ttl</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">cache</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">key</span><span class="p">));</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">¶</a> </div> <p>Get the set of all children keys for the <code>key</code> supplied. If the value
|
||
|
to be returned is an Object, this list will not be empty.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">smembers</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="s1">'keys'</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">function</span> <span class="nx">addValue</span> <span class="p">(</span><span class="nx">source</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">source</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">result</span><span class="p">[</span><span class="nx">source</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
|
||
|
<span class="nx">next</span><span class="p">();</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">keys</span> <span class="o">&&</span> <span class="nx">keys</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">¶</a> </div> <p>If the value to be retrieved is an Object, recursively attempt
|
||
|
to get the value from redis. Here we use a recursive call to <code>this.get</code>
|
||
|
to support nested Object keys.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="nx">addValue</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">cache</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">result</span><span class="p">);</span>
|
||
|
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">result</span><span class="p">);</span>
|
||
|
<span class="p">})</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">¶</a> </div> <p>If there are no keys, then the value to be retrieved is a literal
|
||
|
and we can simply return the value from redis directly. </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">result</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">cache</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">result</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">result</span><span class="p">);</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">¶</a> </div> <h3>function set (key, value, callback)</h3>
|
||
|
|
||
|
<h4>@key {string} Key to set in this instance</h4>
|
||
|
|
||
|
<h4>@value {literal|Object} Value for the specified key</h4>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Sets the <code>value</code> for the specified <code>key</code> in this instance.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">set</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
|
||
|
<span class="nx">path</span> <span class="o">=</span> <span class="nx">nconf</span><span class="p">.</span><span class="nx">path</span><span class="p">(</span><span class="nx">key</span><span class="p">);</span>
|
||
|
|
||
|
<span class="kd">function</span> <span class="nx">addKey</span> <span class="p">(</span><span class="nx">partial</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">index</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">partial</span><span class="p">),</span>
|
||
|
<span class="nx">base</span> <span class="o">=</span> <span class="p">[</span><span class="nx">self</span><span class="p">.</span><span class="nx">namespace</span><span class="p">].</span><span class="nx">concat</span><span class="p">(</span><span class="nx">path</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">index</span><span class="p">)),</span>
|
||
|
<span class="nx">parent</span> <span class="o">=</span> <span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">base</span><span class="p">.</span><span class="nx">concat</span><span class="p">([</span><span class="s1">'keys'</span><span class="p">]));</span>
|
||
|
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">sadd</span><span class="p">(</span><span class="nx">parent</span><span class="p">,</span> <span class="nx">partial</span><span class="p">,</span> <span class="nx">next</span><span class="p">);</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">¶</a> </div> <p>Iterate over the entire key path and add each key to the
|
||
|
parent key-set if it doesn't exist already.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">path</span><span class="p">,</span> <span class="nx">addKey</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="kd">var</span> <span class="nx">fullKey</span> <span class="o">=</span> <span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">self</span><span class="p">.</span><span class="nx">namespace</span><span class="p">,</span> <span class="nx">key</span><span class="p">);</span>
|
||
|
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">&&</span> <span class="k">typeof</span> <span class="nx">value</span> <span class="o">===</span> <span class="s1">'object'</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">¶</a> </div> <p>If the value is an <code>Object</code> (and not an <code>Array</code>) then
|
||
|
nest into the value and set the child keys appropriately.
|
||
|
This is done for efficient lookup when setting Object keys.
|
||
|
(i.e. If you set and Object then wish to later retrieve only a
|
||
|
member of that Object, the entire Object need not be retrieved). </p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">_setObject</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">¶</a> </div> <p>If the value is a simple literal (or an <code>Array</code>) then JSON
|
||
|
stringify it and put it into Redis.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">value</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">cache</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">);</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">¶</a> </div> <h3>function clear (key, callback)</h3>
|
||
|
|
||
|
<h4>@key {string} Key to remove from this instance</h4>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Removes the value for the specified <code>key</code> from this instance.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">clear</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
|
||
|
<span class="nx">result</span> <span class="o">=</span> <span class="p">{},</span>
|
||
|
<span class="nx">path</span> <span class="o">=</span> <span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">namespace</span><span class="p">].</span><span class="nx">concat</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">path</span><span class="p">(</span><span class="nx">key</span><span class="p">)),</span>
|
||
|
<span class="nx">last</span> <span class="o">=</span> <span class="nx">path</span><span class="p">.</span><span class="nx">pop</span><span class="p">(),</span>
|
||
|
<span class="nx">fullKey</span> <span class="o">=</span> <span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">namespace</span><span class="p">,</span> <span class="nx">key</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">¶</a> </div> <p>Clear the key from the cache for this instance</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">cache</span><span class="p">.</span><span class="nx">clear</span><span class="p">(</span><span class="nx">key</span><span class="p">);</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">¶</a> </div> <p>Remove the <code>key</code> from the parent set of keys.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">srem</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">path</span><span class="p">.</span><span class="nx">concat</span><span class="p">([</span><span class="s1">'keys'</span><span class="p">])),</span> <span class="nx">last</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">¶</a> </div> <p>Remove the value from redis by iterating over the set of keys (if any)
|
||
|
and deleting each value. If no keys, then just delete the simple literal.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">smembers</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="s1">'keys'</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">function</span> <span class="nx">removeValue</span> <span class="p">(</span><span class="nx">child</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">¶</a> </div> <p>Recursively call <code>self.clear</code> here to ensure we remove any
|
||
|
nested Objects completely from this instance.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">clear</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">child</span><span class="p">),</span> <span class="nx">next</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">keys</span> <span class="o">&&</span> <span class="nx">keys</span><span class="p">.</span><span class="nx">length</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">¶</a> </div> <p>If there are child keys then iterate over them,
|
||
|
removing each child along the way.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="nx">removeValue</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">¶</a> </div> <p>Otherwise if this is just a simple literal, then
|
||
|
simply remove it from Redis directly.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">del</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="nx">callback</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-19"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-19">¶</a> </div> <h3>function save (value, callback)</h3>
|
||
|
|
||
|
<h4>@value {Object} Config object to set for this instance</h4>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Removes any existing configuration settings that may exist in this
|
||
|
instance and then adds all key-value pairs in <code>value</code>. </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">save</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">||</span> <span class="k">typeof</span> <span class="nx">value</span> <span class="o">!==</span> <span class="s1">'object'</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s1">'`value` to be saved must be an object.'</span><span class="p">));</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
|
||
|
<span class="nx">keys</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-20"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-20">¶</a> </div> <p>Clear all existing keys associated with this instance.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">reset</span><span class="p">(</span><span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-21"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-21">¶</a> </div> <p>Iterate over the keys in the new value, setting each of them.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">[</span><span class="nx">key</span><span class="p">],</span> <span class="nx">next</span><span class="p">);</span>
|
||
|
<span class="p">},</span> <span class="nx">callback</span><span class="p">);</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-22"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-22">¶</a> </div> <h3>function load (callback)</h3>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Responds with an Object representing all keys associated in this instance.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">load</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
|
||
|
<span class="nx">result</span> <span class="o">=</span> <span class="p">{};</span>
|
||
|
|
||
|
<span class="k">this</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">smembers</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">namespace</span><span class="p">,</span> <span class="s1">'keys'</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="kd">function</span> <span class="nx">addValue</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">get</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">result</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">value</span><span class="p">;</span>
|
||
|
<span class="nx">next</span><span class="p">();</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="nx">addValue</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">err</span> <span class="o">?</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">:</span> <span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span> <span class="nx">result</span><span class="p">);</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-23"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-23">¶</a> </div> <h3>function reset (callback)</h3>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Clears all keys associated with this instance.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">reset</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-24"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-24">¶</a> </div> <p>Get the list of of top-level keys, then clear each of them</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">smembers</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">namespace</span><span class="p">,</span> <span class="s1">'keys'</span><span class="p">),</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">existing</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">existing</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">clear</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">next</span><span class="p">);</span>
|
||
|
<span class="p">},</span> <span class="nx">callback</span><span class="p">);</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span></pre></div> </td> </tr> <tr id="section-25"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-25">¶</a> </div> <h3>@private function _setObject (key, value, callback)</h3>
|
||
|
|
||
|
<h4>@key {string} Key to set in this instance</h4>
|
||
|
|
||
|
<h4>@value {Object} Value for the specified key</h4>
|
||
|
|
||
|
<h4>@callback {function} Continuation to respond to when complete.</h4>
|
||
|
|
||
|
<p>Internal helper function for setting all keys of a nested object.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">Redis</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">_setObject</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">value</span><span class="p">,</span> <span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="kd">var</span> <span class="nx">self</span> <span class="o">=</span> <span class="k">this</span><span class="p">,</span>
|
||
|
<span class="nx">keys</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">keys</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
|
||
|
|
||
|
<span class="kd">function</span> <span class="nx">addValue</span> <span class="p">(</span><span class="nx">child</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-26"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-26">¶</a> </div> <p>Add the child key to the parent key-set, then set the value.
|
||
|
Recursively call <code>_setObject</code> in the event of nested Object(s).</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">sadd</span><span class="p">(</span><span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="s1">'keys'</span><span class="p">),</span> <span class="nx">child</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="kd">var</span> <span class="nx">fullKey</span> <span class="o">=</span> <span class="nx">nconf</span><span class="p">.</span><span class="nx">key</span><span class="p">(</span><span class="nx">key</span><span class="p">,</span> <span class="nx">child</span><span class="p">),</span>
|
||
|
<span class="nx">childValue</span> <span class="o">=</span> <span class="nx">value</span><span class="p">[</span><span class="nx">child</span><span class="p">];</span>
|
||
|
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nb">Array</span><span class="p">.</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">childValue</span><span class="p">)</span> <span class="o">&&</span> <span class="k">typeof</span> <span class="nx">childValue</span> <span class="o">===</span> <span class="s1">'object'</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">_setObject</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="nx">childValue</span><span class="p">,</span> <span class="nx">next</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="k">else</span> <span class="p">{</span>
|
||
|
<span class="nx">childValue</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">childValue</span><span class="p">);</span>
|
||
|
<span class="nx">self</span><span class="p">.</span><span class="nx">redis</span><span class="p">.</span><span class="nx">set</span><span class="p">(</span><span class="nx">fullKey</span><span class="p">,</span> <span class="nx">childValue</span><span class="p">,</span> <span class="nx">next</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div> </td> </tr> <tr id="section-27"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-27">¶</a> </div> <p>Iterate over the keys of the Object and set the appropriate values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">async</span><span class="p">.</span><span class="nx">forEach</span><span class="p">(</span><span class="nx">keys</span><span class="p">,</span> <span class="nx">addValue</span><span class="p">,</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">return</span> <span class="nx">err</span> <span class="o">?</span> <span class="nx">callback</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="o">:</span> <span class="nx">callback</span><span class="p">();</span>
|
||
|
<span class="p">});</span>
|
||
|
<span class="p">};</span>
|
||
|
|
||
|
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
|