RESP (REdis Serialization Protocol) parser module.
NOTE: this module is under heavy development.
install from LuaRocks
$ luarocks install lua-resp
local resp = require('resp')
resp.EAGAIN
: Not enough data available.resp.EILSEQ
: Found illegal byte sequence.
resp.ARR
: arrays.resp.BLK
: bulkstrings.resp.ERR
: errors.resp.INT
: integers.resp.STR
: simplestrings.
encode messages.
Parameters
...
: messages of the following data types;nil
string
number
boolean
table (non-sparse array only)
Returns
msg:string
: serialized message.err:string
: error message.
encode messages as array.
usually, this api use to encode a command message.
Parameters and Returns
same as resp.encode API
decode serialized message strings.
Parameters
str
: serialized message string.head
: decode start position. (default0
)
Returns
consumed:number
: greater than 0 on sucess, or Status Constants.msg:string, number or array
: decoded message.typ:number
: Message Type Constants.
local inspect = require'util'.inspect
local resp = require("resp")
local msg = resp.encode( 'HMSET', 'myhash', 'hello', '"world"' )
-- encoded to following string;
-- *4\r\n$5\r\nHMSET\r\n$6\r\nmyhash\r\n$5\r\nhello\r\n$7\r\n"world"\r\n
local consumed, data, typ = resp.decode( msg )
-- consumed equal to 51
-- typ equal to resp.ARR
-- decoded to following table;
-- { [1] = "HMSET",
-- [2] = "myhash",
-- [3] = "hello",
-- [4] = "\"world\"",
-- len = 4 }
-- decode multiple-message
local mmsg = table.concat({msg, msg, msg})
consumed, data = resp.decode( mmsg )
while consumed > 0 do
mmsg = string.sub( mmsg, consumed + 1 )
consumed, data = resp.decode( mmsg )
end
-- use with head optional argument
mmsg = table.concat({msg, msg, msg})
consumed, data = resp.decode( mmsg )
while consumed > 0 do
consumed, data = resp.decode( mmsg, consumed )
end