executing redis eval command to run Lua script in nodeJS

前端 未结 1 1394
醉梦人生
醉梦人生 2021-01-16 16:26

In Redis I run a Lua script through CLI like this:-

$ redis-cli --eval debug_script.lua key1 key2 key3 key4 , arg1 arg2

So, my Lua script a

相关标签:
1条回答
  • 2021-01-16 16:35

    Found some solutions:

    Solution 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);
    });
    

    Note: 4 (second argument of eval) represents the number of keys to be passed in the script.

    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);
        });
    
    0 讨论(0)
提交回复
热议问题