在Redis中,我通过CLI运行Lua脚本,如下所示: -
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
所以,我的Lua脚本接受4个键和2个参数。
现在我想在Node.js中运行相同的脚本。
我正在使用this库在我的应用中导入Redis。
我没有找到任何关于redisClient.eval(...)
函数执行Lua脚本的参数的例子。
因此,我只是随机打一些可行的东西。但似乎没有任何效果。
我的app.js是这样的:
var redis = require("redis")
var client = redis.createClient();
// my hit and trial guess
client.eval(["script_file.lua", 1 "score" 0 10 , "meeting_type:email" meeting_status:close], function(err, res){
console.log("something happened\n");
});
我的问题:如何使用node.js执行以下命令,以便它返回与通过CLI(命令行界面)执行时相同的操作。
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
答案 0 :(得分:1)
找到一些解决方案:
解决方案1 )
var redis = require('redis')
var client = redis.createClient()
var fs = require('fs')
client.eval(fs.readFileSync('./debug_script.lua'), 4, key1, key2, key3, key4, arg1, arg2, function(err, res) {
console.log(res);
});
注意:4
(eval的第二个参数)表示要在脚本中传递的键数。
解决方案2 )创建子进程并运行CLI命令。
var redis = require("redis");
var client = redis.createClient();
var exec = require('child_process').exec;
var cmd = 'redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2';
exec(cmd, function(error, stdout, stderr) {
// command output is in stdout
console.log("something happened \n");
console.log(stdout);
});