node.js - 执行 redis eval 命令以在 nodeJS 中运行 Lua 脚本

标签 node.js lua redis

在 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/

相关文章:

lua - local _, x = ... 在lua中做什么?

algorithm - 函数按预期执行,但在循环结束时返回 nil 值

redis - 如何使用 Chef JSON 设置 redis 和 sidekiq 配置

mysql - node.js和mysql连接池不导出

javascript - 如何动态地将项目添加到接受对象列表的函数中

node.js - 即使使用 Procfile 和特定的 Web 进程,Heroku 也总是从 "npm"开始

python-3.x - 我想用python抓取一个网站,但是我遇到了麻烦。 requests library 是可以的,但是 400 与 Scrapy,下面的代码

node.js - amqp vs amqplib - 哪个 Node.js amqp 客户端库更好?

c - Lua注册的C函数调用另一个

Redis 作为自填充缓存