在 Redis 中,我通过 CLI 运行 Lua 脚本,如下所示:-
$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2
因此,我的 Lua 脚本接受 4 个键和 2 个参数。
现在我想在 Node.js 中运行相同的脚本。
我正在使用 this用于在我的应用中导入 Redis 的库。
我没有找到任何示例来说明用于执行 Lua 脚本的 redisClient.eval(...)
函数的参数。
因此,我只是随便打一些可能有用的东西。但似乎没有任何效果。
我的 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
最佳答案
找到了一些解决方案:
解决方案 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);
});
关于node.js - 执行 redis eval 命令以在 nodeJS 中运行 Lua 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44676974/