问题描述
在Redis中,我通过CLI运行Lua脚本,如下所示:-
In Redis I run a Lua script through CLI like this:-
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
因此,我的Lua脚本接受4个键和2个参数.
So, my Lua script accepts 4 keys and 2 arguments.
现在,我想在Node.js中运行相同的脚本.
Now I want to run the same script in Node.js.
我正在使用此库在我的应用程序中导入Redis.
I am using this library for importing Redis in my app.
我没有找到任何示例来说明用于执行Lua脚本的redisClient.eval(...)
函数的参数.
I didn't find any example which tells about the arguments of redisClient.eval(...)
function for executing the Lua script.
因此,我只是碰到一些可能起作用的东西.但是似乎没有任何作用.
Thus I am just hitting something random that might work. But nothing seems to work.
我的app.js像这样:
My app.js goes like this:
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(命令行界面)执行时相同的结果.
My question: How to execute below command using node.js, so that it returns the same thing as it does when executed through CLI(command-line-interface).
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
推荐答案
找到了一些解决方案:
解决方案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的第二个参数)表示要在脚本中传递的键数.
Note: 4
(second argument of eval) represents the number of keys to be passed in the script.
解决方案2 )创建一个子进程并运行CLI命令.
Solution 2) Creates a child process and run CLI command.
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);
});
这篇关于执行redis eval命令以在nodeJS中运行Lua脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!