Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis - Lua tables as return values - why is this not working

When I run this code through redis EVAL it return no results. Any idea why this is not working?

redis-cli EVAL "$(cat bug.lua)" 0

bug.lua

local retv = {}
retv["test"] = 1000

return retv

If I initialize the table that value alone gets printed.

$ cat bug.lua 
--!/usr/bin/env lua


local retv = {"This", "is", "a", "bug" }
retv["test"] = 1000

return retv

$ redis-cli EVAL "$(cat bug.lua)" 2 a b
1) "This"
2) "is"
3) "a"
4) "bug"
like image 579
vivekv Avatar asked Jun 19 '14 04:06

vivekv


1 Answers

Answer from @deltheil is valid.

Remember though that you can use cjson library to pack tables and pass them to consumers.

Your lua file:

local retv = {"This", "is", "a", "bug" }
retv["test"] = 1000

return cjson.encode(retv)

Command:

redis-cli EVAL "$(cat bug.lua)" 0

Result:

"{\"1\":\"This\",\"2\":\"is\",\"3\":\"a\",\"4\":\"bug\",\"test\":1000}"
like image 54
Anvaka Avatar answered Oct 16 '22 01:10

Anvaka