Simple library to share redis clients in a code base


Simple library to share redis clients in a code base

var redis = require('redis-manager')
var client = redis.createClient();
var client2 = redis.createClient();
console.log('redis-manager pools clients', client === client2);
var redisManager = require('redis-manager');
var client = redisManager.getClient(); // Same options as `redis.createClient`, is an instance of `redis`'s client, but the args can be specified in any order desired 
client.set('key', 'val', callback);
// ... 
// All done? Free the client so the manager can shut it down if no one is using it, anymore. 

create a cached redis client. When you call createClient() we either give you a new redis client or give you a cached one in case you've called createClient() with the exact same parameters.

You can pass in a host and port or pass in { host, port } options object. The other options are passed directly to redis itself.

The Redis library, whenever a subscribe method is called, puts the instance in a special mode that disables all other Redis commands. Therefore this kind of redis connection cannot be pooled unless distinguished during configuration, so a sub property has been added to the options parameter when getting a client. If you need subscribe Redis usage, be sure to pass in this flag. (Currently works on the honor system.)

Further, whenever a select method is called, the semantics of hset/hget/etc change, and a common redis instance shared between code trying to read/write different databases is a Bad Thing(tm).

If two instances of redis-manager pointing at the same actual client register the exact same listener (listenerA === listenerB), when the first instance is freed, both registered listeners will be eliminated as it removes listeners created by the instance being freed. This is very unlikely to ever be the case as even identical code between two files wold be considered different functions (because of differing scope), but is possible.

If you flip any given client into debug mode, you will flip all clients into debug mode. The debug mode feature is global and it globally sets the redis module into debug mode

npm install redis-manager

npm test

  • David Ellis
  • Amos
  • Raynos