title: Node.js, (Hi)Redis and the multi command
body: <p>I'm playing around with node.js and redis and installed the hiredis library via this command</p>
  
  <pre><code>npm install hiredis redis
  </code></pre>
  
  <p>I looked at the multi examples here: </p>
  
  <p><a href="https://github.com/mranney/node_redis/blob/master/examples/multi2.js">https://github.com/mranney/node_redis/blob/master/examples/multi2.js</a></p>
  
  <p>At line 17 it says </p>
  
  <pre><code>// you can re-run the same transaction if you like
  </code></pre>
  
  <p>which implies that the internal multi.queue object is never cleared once the commands finished executing. </p>
  
  <p>My question is: How would you handle the situation in an http environment? For example, tracking the last connected user (this doesn't really need multi as it just executes one command but it's easy to follow)</p>
  
  <pre><code>var http = require('http');
  redis = require('redis');
  
  client = redis.createClient()
  multi = client.multi();
  
  http.createServer(function (request, response) {
    multi.set('lastconnected', request.ip); // won't work, just an example
    multi.exec(function(err, replies) {
        console.log(replies);
    });
  });
  </code></pre>
  
  <p>In this case, multi.exec would execute 1 transaction for the first connected user, and 100 transactions for the 100th user (because the internal multi.queue object is never cleared). </p>
  
  <p><strong>Option 1:</strong> Should I create the multi object inside the http.createServer callback function, which would effectivly kill it at the end of the function's execution? How expensive in terms of CPU cycles would creating and destroying of this object be? </p>
  
  <p><strong>Option 2:</strong> The other option would be to create a new version of multi.exec(), something like multi.execAndClear() which will clear the queue the moment redis executed <em>that</em> bunch of commands. </p>
  
  <p>Which option would you take? I suppose option 1 is better - we're killing one object instead of cherry picking parts of it - I just want to be sure as I'm brand new to both node and javascript.</p>