sol-hiredis-pool

Generic resource pooling for Node.JS, with hiredis support

sol-hiredis-pool

A simple Redis pool for node using generic-pool. There are two example included. See example1.js for a demo.

(Forked from joshuah/sol-redis-pool. This version enables hiredis support for faster processing.)

npm install sol-hiredis-pool
// Example 1: Using the acquireHelper.
// Our Settings
options = {
  redis_host: '127.0.0.1',
  redis_port: 6379,
}
    
var RedisPool = require('./index');
var pool = new RedisPool(options);

// Handle the error here...
function errorCallback(err) {}

function clientCallback(client) {
  // Use the client then release it back to the pool.
  client.ping(function(err, result) {
    console.log(err, result);
    // Release the client...
    pool.release(client);
    // Drain the pool so the example will end.
    pool.drain(function(){
        console.log('Done...');
    });
  })
}
console.log("If everything is working, you should see 'null 'PONG'.")
pool.acquireHelper(errorCallback, clientCallback);

The output should be:

If everything is working you should see 'null 'PONG'.
null 'PONG'

Acquires a redis client from the pool. The callback is passed an err and client. The client object is a normal redis client. Make sure you release the client using the .release(client) method when you are done.

This method will release your client object back into the pool.

This method accepts two callbacks. The error callback is called if the pool cannot return a client. errorCallback(err). The clientCallback(client) returns the redis client connection. Note: You still need to release the client object.

This method will drain the connection pool completely and execute the callback when finished. You should call this when you want to close your application. If you do not your application will continue to run forever.

The Pool argument should be your Pool object.

Combine any the settings you may need into one global named settings.

options = {
  redis_host: '127.0.0.1',
  redis_port: 6379,
  redis_options: {},
  redis_password: 'dingbats'
  max_clients: 10,
  min_clients: 2,
  reapIntervalMillis: 5000,
  idleTimeoutMillis: 30000,
  logging: true
}
options = {
  redis_port: 6379,
  redis_host: '127.0.0.1'
}

The default settings are port 6379 and 127.0.0.1.

options = {
  unix_socket: '/tmp/redis.sock'
}

You can pass any options you normally would pass to the redis.createClient() function in node-redis. See the node-redis documentation for more information.

options = {
   redis_options: redisoptions…
}

You can also provide the redis server password by setting redis_password. NOTE: This still needs testing…

These options are used to control the generic-pool. You will normally not need to use any of these options.

Optional minimum and maximum clients to have ready in the pool. The default values are 10 for the maximum and 2 for the minimum.

options = {
   max_clients: 10,
   min_clients: 5
}

You can adjust the generic-pool reapIntervalMillis and idleTimeoutMillis.

options = {
   reapIntervalMillis: 5000,
   idleTimeoutMillis: 30000
}

If you would like to see what the generic-pool module is doing your can enable console logging by setting logging to true. This feature is off by default.

options = {
  logging: true
}
  • 2013-07-23 - Added hiredis support.
  • 2013-03-06 - Added Unix socket support by setting the unix_socket option.