I have written some lua script for my node.js project. but some of my lua scripts has same code in it. let me explain first.
my first script returns all the data from given key from redis.
script1.lua
local data = {};
local keyslist = redis.call('keys', 'day:*');
local key, redisData;
for iCtr = 1, #keyslist do
key = string.gsub(keyslist[iCtr], 'day:','');
redisData = redis.call('hmget', keyslist[iCtr], 'users');
table.insert(data, {date=key, users=redisData[1]});
end
return cjson.encode(data);
my second script returns top 2 records from the same key from redis.
script2.lua
local data = {};
local keyslist = redis.call('keys', 'day:*');
local key, redisData;
for iCtr = 1, #keyslist do
if iCtr < 3
key = string.gsub(keyslist[iCtr], 'day:','');
redisData = redis.call('hmget', keyslist[iCtr], 'users');
table.insert(data, {date=key, users=redisData[1]});
end
end
return cjson.encode(data);
Now want to call script1.lua from script2.lua like as follows.
script2.lua (Want like as follow)
local file = assert(loadfile("script1.lua"));
return file(2) -- return only top 2 records where needed.
-- some forLoop logic will be change as per about need.
I had tried above code, but it through following error
Script attempted to access unexisting global variable 'loadfile'
Sorry for my poor explanation.