执行redis eval命令在nodeJS中运行Lua脚本 [英] executing redis eval command to run Lua script in nodeJS
问题描述
在 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.
我正在使用 this 库在我的应用中导入 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
");
});
我的问题:如何使用 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
");
console.log(stdout);
});
这篇关于执行redis eval命令在nodeJS中运行Lua脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!