A redis client wrapper to support cluster connections.
This library can be used as a drop-in replacement for the regular node_redis client.
npm install redis-party
var nodes = [
{"host": "node1.domain.com", "port": 10000},
{"host": "node1.domain.com", "port": 10001},
{"host": "node2.domain.com", "port": 10000},
{"host": "node2.domain.com", "port": 10001},
];
var redis = require("redis-party");
var cluster = new redis.Cluster(nodes, {"max_attempts": 5});
cluster.once("ready", function () {
cluster.SET("hello", "planet");
var multi = cluster.multi();
multi.SET("hello", "world");
multi.GET("hello");
multi.DEL("hello");
multi.exec(function (err, res) {
console.log("hello " + res[1]);
});
var multi2 = cluster.multi();
multi2.SET("key1", "val");
multi2.SET("key2", "val");
multi2.exec(function (err, res) {
//err = Error("Multi comands must operate on the same slot!")
});
cluster.PUBLISH("key1", "hello world!");
});
Alternative node_redis compatible interface:
var client = redis.createClient(port, host, options);
client.SET("foo", "bar", function () {
client.GET("foo", function (err, bar) {
console.log("foo = " + bar);
});
});
Just like node_redis, each redis command is exposed as a function on the client object.
All commands except multi
are passed along to the correct node_redis client depending on the key hash.
The client will emit the ready event once the client is connected to the cluster and has populated the slot-node mapping table.
This client will emit the error event if there is an error with the cluster library.
Error events emitted from the node_redis library.
Client will emit message
for every message received that matches an active subscription.
This event is emitted when a cluster config change has been detected. The cluster slot-node mapping table will be re-initialized,
and the ready
event will be emitted again once that is done.
Create a new cluster instance. It will connect to the specified redis node and discover the rest of the nodes from there.
port
- defaults to6379
host
- defaults to127.0.0.1
options
- same as node_redis client options
Create a new cluster instance. It will connect to the first available redis node and discover the rest of the nodes from there.
nodes
- an array of objects withhost
andport
properties.redisOptions
- node_redis client options. See node_redis documentation.
Cleanly end connections to all cluster nodes by sending the QUIT
command after handling all replies.
Returns the slot which will be used for a key.
Get the node_redis instance for a specific slot.