如何在redis中将JS对象传递给它自己的事件参数 - 连接事件的哨兵
How to pass JS Object to its own event parameter in redis - sentinal on connect event
在 redis sentinel 客户端程序中 - 第一个程序,当我保留要创建的 redis sentinel 对象时,它可以很好地设置 KEY 。但是当你观察第二个程序时,client.on('connect', runSample(client));我正在传递客户端对象 (redis sentinel client),它的连接参数 runSample。为此,我收到以下错误 ..
错误详情
https://github.com/DocuSignDev/node-redis-sentinel-client/blob/master/index.js
RedisSentinelClient.prototype.send_command undefined
/node_modules/redis-sentinel-client/index.js:293
return client.send_command.apply(client, arguments);
^
TypeError: Cannot read property 'send_command' of undefined
at RedisSentinelClient.send_command (/node_modules/redis-sentinel-client/index.js:293:16)
at RedisSentinelClient.(anonymous function).RedisSentinelClient.(anonymous function) (/node_modules/redis-sentinel-client/index.js:307:23)
at runSample (/msg/lb4.expire.onefile.2m.notworking.js:25:13)
at init (/msg/lb4.expire.onefile.2m.notworking.js:16:26)
at Object.<anonymous> (/msg/lb4.expire.onefile.2m.notworking.js:75:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
RajRajen:pubsub.local rajrajen$
第一个工作程序..
'use strict';
var client = getRedisSentinelObject();
client.on('error', function(err) {
console.log('Error ' + err);
});
client.on('connect', runSample);
function runSample() {
var allStrings = '{abc:123}';
client.get(allStrings, function(err, reply) {
if (reply) {
console.log('Key is ' + reply.toString());
client.ttl(allStrings, writeTTL);
} else {
console.log('string key expired or not set before!');
// Set a value
client.set(allStrings, allStrings);
// Expire in 3 seconds
client.expire(allStrings, 3);
}
client.quit();
});
}
function getRedisSentinelObject() {
var redisSentinelHost = process.env.REDIS_SENTINEL_SERVICE_HOST;
var redisSentinelPort = process.env.REDIS_SENTINEL_SERVICE_PORT;
var options = {
'master_debug': false
};
var redisSentinelMasterDebug = process.env.REDIS_SENTINEL_MASTER_DEBUG;
if (typeof redisSentinelMasterDebug !== "undefined") {
if (redisSentinelMasterDebug === "true") {
options.master_debug = true;
}
}
console.log('redisSentinelHost ', redisSentinelHost, 'redisSentinelPort ', redisSentinelPort);
var RedisSentinel = require('redis-sentinel-client');
var sentinelClient = RedisSentinel.createClient(redisSentinelPort, redisSentinelHost, options);
console.log('sentinelClient ', sentinelClient);
return sentinelClient;
}
function writeTTL(err, data) {
console.log('I live for this long yet: ', data);
}
第二个程序,不起作用
'use strict';
function init() {
var client = getRedisSentinelObject();
client.on('error', function(err) {
console.log('Error ' + err);
});
client.on('connect', runSample(client));
}
function runSample(client1) {
var allStrings = '{abc:123}';
client1.get(allStrings, function(err, reply) {
if (reply) {
console.log('Key is ' , reply.toString());
client1.ttl(allStrings, writeTTL);
} else {
console.log('string key expired or not set before!');
// Set a value
client1.set(allStrings, allStrings);
// Expire in 3 seconds
client1.expire(allStrings, 2);
}
// client1.quit();
});
}
function getRedisSentinelObject() {
var redisSentinelHost = process.env.REDIS_SENTINEL_SERVICE_HOST;
var redisSentinelPort = process.env.REDIS_SENTINEL_SERVICE_PORT;
var options = {
'master_debug': false
};
var redisSentinelMasterDebug = process.env.REDIS_SENTINEL_MASTER_DEBUG;
if (typeof redisSentinelMasterDebug !== "undefined") {
if (redisSentinelMasterDebug === "true") {
options.master_debug = true;
}
}
console.log('redisSentinelHost ', redisSentinelHost, 'redisSentinelPort ', redisSentinelPort);
var RedisSentinel = require('redis-sentinel-client');
var sentinelClient = RedisSentinel.createClient(redisSentinelPort, redisSentinelHost, options);
console.log('sentinelClient ', sentinelClient);
return sentinelClient;
}
function writeTTL(err, data) {
console.log('I live for this long yet: ', data);
}
init();
谢谢
问题
在示例 1 中,您有以下行:
client.on('connect', runSample);
这一行在客户端连接时附加了一个函数运行。一切都很好。请注意,在客户端连接之前,该功能不会 运行。
在示例 2 中,同一行如下所示:
client.on('connect', runSample(client));
在这一行中,runSample
方法立即 执行。然后将此函数调用的结果(在本例中为 undefined
)传递给 client.on
。因此,如果我们在这里手动进行一些评估,javascript 归结为:
client.on('connect', undefined);
这失败了,因为你告诉 redis,"When you connect, nothing"。
简单的修复
解决此问题的最简单方法是使用闭包(吸收其作用域的函数)。这很可能是您想要做的。
client.on('connect', function () {
runSample(client)
});
更复杂的修复
如果你想让事情变得更复杂一点,你可以做一个这样的函数:
function buildRunner (client) {
return function runTests () {
var allStrings = '{abc:123}';
client.get(allStrings, function(err, reply) {
if (reply) {
console.log('Key is ' + reply.toString());
client.ttl(allStrings, writeTTL);
} else {
console.log('string key expired or not set before!');
// Set a value
client.set(allStrings, allStrings);
// Expire in 3 seconds
client.expire(allStrings, 3);
}
client.quit();
});
}
}
并像这样使用它:
client.on('connect', buildRunner(client));
请注意 buildRunner
将立即执行,就像以前一样。不同之处在于 buildRunner
returns 一个传递给 client.on
的函数。如果我们再次手动进行一些评估,我们会得到:
client.on('connect', function runTests () { ... })
// ^ runTests is holding on to an internal copy of `client` because
// `client` existed back when we built runTests
// up in `buildRunner`
在 redis sentinel 客户端程序中 - 第一个程序,当我保留要创建的 redis sentinel 对象时,它可以很好地设置 KEY 。但是当你观察第二个程序时,client.on('connect', runSample(client));我正在传递客户端对象 (redis sentinel client),它的连接参数 runSample。为此,我收到以下错误 ..
错误详情
https://github.com/DocuSignDev/node-redis-sentinel-client/blob/master/index.js
RedisSentinelClient.prototype.send_command undefined
/node_modules/redis-sentinel-client/index.js:293
return client.send_command.apply(client, arguments);
^
TypeError: Cannot read property 'send_command' of undefined
at RedisSentinelClient.send_command (/node_modules/redis-sentinel-client/index.js:293:16)
at RedisSentinelClient.(anonymous function).RedisSentinelClient.(anonymous function) (/node_modules/redis-sentinel-client/index.js:307:23)
at runSample (/msg/lb4.expire.onefile.2m.notworking.js:25:13)
at init (/msg/lb4.expire.onefile.2m.notworking.js:16:26)
at Object.<anonymous> (/msg/lb4.expire.onefile.2m.notworking.js:75:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
RajRajen:pubsub.local rajrajen$
第一个工作程序..
'use strict';
var client = getRedisSentinelObject();
client.on('error', function(err) {
console.log('Error ' + err);
});
client.on('connect', runSample);
function runSample() {
var allStrings = '{abc:123}';
client.get(allStrings, function(err, reply) {
if (reply) {
console.log('Key is ' + reply.toString());
client.ttl(allStrings, writeTTL);
} else {
console.log('string key expired or not set before!');
// Set a value
client.set(allStrings, allStrings);
// Expire in 3 seconds
client.expire(allStrings, 3);
}
client.quit();
});
}
function getRedisSentinelObject() {
var redisSentinelHost = process.env.REDIS_SENTINEL_SERVICE_HOST;
var redisSentinelPort = process.env.REDIS_SENTINEL_SERVICE_PORT;
var options = {
'master_debug': false
};
var redisSentinelMasterDebug = process.env.REDIS_SENTINEL_MASTER_DEBUG;
if (typeof redisSentinelMasterDebug !== "undefined") {
if (redisSentinelMasterDebug === "true") {
options.master_debug = true;
}
}
console.log('redisSentinelHost ', redisSentinelHost, 'redisSentinelPort ', redisSentinelPort);
var RedisSentinel = require('redis-sentinel-client');
var sentinelClient = RedisSentinel.createClient(redisSentinelPort, redisSentinelHost, options);
console.log('sentinelClient ', sentinelClient);
return sentinelClient;
}
function writeTTL(err, data) {
console.log('I live for this long yet: ', data);
}
第二个程序,不起作用
'use strict';
function init() {
var client = getRedisSentinelObject();
client.on('error', function(err) {
console.log('Error ' + err);
});
client.on('connect', runSample(client));
}
function runSample(client1) {
var allStrings = '{abc:123}';
client1.get(allStrings, function(err, reply) {
if (reply) {
console.log('Key is ' , reply.toString());
client1.ttl(allStrings, writeTTL);
} else {
console.log('string key expired or not set before!');
// Set a value
client1.set(allStrings, allStrings);
// Expire in 3 seconds
client1.expire(allStrings, 2);
}
// client1.quit();
});
}
function getRedisSentinelObject() {
var redisSentinelHost = process.env.REDIS_SENTINEL_SERVICE_HOST;
var redisSentinelPort = process.env.REDIS_SENTINEL_SERVICE_PORT;
var options = {
'master_debug': false
};
var redisSentinelMasterDebug = process.env.REDIS_SENTINEL_MASTER_DEBUG;
if (typeof redisSentinelMasterDebug !== "undefined") {
if (redisSentinelMasterDebug === "true") {
options.master_debug = true;
}
}
console.log('redisSentinelHost ', redisSentinelHost, 'redisSentinelPort ', redisSentinelPort);
var RedisSentinel = require('redis-sentinel-client');
var sentinelClient = RedisSentinel.createClient(redisSentinelPort, redisSentinelHost, options);
console.log('sentinelClient ', sentinelClient);
return sentinelClient;
}
function writeTTL(err, data) {
console.log('I live for this long yet: ', data);
}
init();
谢谢
问题
在示例 1 中,您有以下行:
client.on('connect', runSample);
这一行在客户端连接时附加了一个函数运行。一切都很好。请注意,在客户端连接之前,该功能不会 运行。
在示例 2 中,同一行如下所示:
client.on('connect', runSample(client));
在这一行中,runSample
方法立即 执行。然后将此函数调用的结果(在本例中为 undefined
)传递给 client.on
。因此,如果我们在这里手动进行一些评估,javascript 归结为:
client.on('connect', undefined);
这失败了,因为你告诉 redis,"When you connect, nothing"。
简单的修复
解决此问题的最简单方法是使用闭包(吸收其作用域的函数)。这很可能是您想要做的。
client.on('connect', function () {
runSample(client)
});
更复杂的修复
如果你想让事情变得更复杂一点,你可以做一个这样的函数:
function buildRunner (client) {
return function runTests () {
var allStrings = '{abc:123}';
client.get(allStrings, function(err, reply) {
if (reply) {
console.log('Key is ' + reply.toString());
client.ttl(allStrings, writeTTL);
} else {
console.log('string key expired or not set before!');
// Set a value
client.set(allStrings, allStrings);
// Expire in 3 seconds
client.expire(allStrings, 3);
}
client.quit();
});
}
}
并像这样使用它:
client.on('connect', buildRunner(client));
请注意 buildRunner
将立即执行,就像以前一样。不同之处在于 buildRunner
returns 一个传递给 client.on
的函数。如果我们再次手动进行一些评估,我们会得到:
client.on('connect', function runTests () { ... })
// ^ runTests is holding on to an internal copy of `client` because
// `client` existed back when we built runTests
// up in `buildRunner`