默认, Kue使用客户端默认设置连接Redis (默认端口 6379
, 默认主机 127.0.0.1
, 默认前缀 q
). Queue#createQueue(options)
在 options.redis
键上接受 redis 连接选项.
var kue = require('kue');
var q = kue.createQueue({
prefix: 'q',
redis: {
port: 1234,
host: '10.0.50.20',
auth: 'password',
db: 3, // if provided select a non-default redis db
options: {
// see https://github.com/mranney/node_redis#rediscreateclientport-host-options
}
}
});
prefix
控制在Redis里使用的键名. 默认, 只是简单的 q
. Prefix generally shouldn't be changed unless you need to use one Redis instance for multiple apps. It can also be useful for providing an isolated testbed across your main application.
Since node_redis supports Unix Domain Sockets, you can also tell Kue to do so. See unix-domain-socket for your redis server configuration.
var kue = require('kue');
var q = kue.createQueue({
prefix: 'q',
redis: {
socket: '/data/sockets/redis.sock',
auth: 'password',
options: {
// see https://github.com/mranney/node_redis#rediscreateclientport-host-options
}
}
});
Any node.js redis client library that conforms (or when adapted) to node_redis API can be injected into Kue. You should only provide a createClientFactory
function as a redis connection factory instead of providing node_redis connection options.
Below is a sample code to enable redis-sentinel to connect to Redis Sentinel for automatic master/slave failover.
var kue = require('kue');
var Sentinel = require('redis-sentinel');
var endpoints = [
{host: '192.168.1.10', port: 6379},
{host: '192.168.1.11', port: 6379}
];
var opts = options || {}; // Standard node_redis client options
var masterName = 'mymaster';
var sentinel = Sentinel.Sentinel(endpoints);
var q = kue.createQueue({
redis: {
createClientFactory: function(){
return sentinel.createClient(masterName, opts);
}
}
});
Note that all <0.8.x
client codes should be refactored to pass redis options to Queue#createQueue
instead of monkey patched style overriding of redis#createClient
or they will be broken from Kue 0.8.x
.