Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Lua module for parsing raw redis responses

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



This lua library implementes a thin and fast redis raw response parser
that constructs corresponding lua data strucutres, as well as a
function that constructs redis raw requests.

To maximize speed, this module is implemented in pure C.

Here's the usage of the parse_reply method:

    local parser = require('redis.parser')

    -- assuming the reply variable holds the (single) redis response
    --  to be parsed:
    local res, typ = parser.parse_reply(reply)

    if typ == parser.BAD_REPLY then
        -- res is the textual error message from the parser
    elseif typ == parser.INTEGER_REPLY then
        -- res is an integer, like 3, returned from the redis server
    elseif typ == parser.ERROR_REPLY then
        -- res is the error message from the redis2 server
    elseif typ == parser.STATUS_REPLY then
        -- res is the textual message from the redis server
    elseif typ == parser.BULK_REPLY then
        --- res is a string for the bulk data
    elseif typ == parser.MULTI_BULK_REPLY then
        -- res is a lua (array) table that holds the individual bulks

You can use the parse_replies method to parse multiple pipelined
redis responses, for example

    local parser = require('redis.parser')

    -- assuming the replies variable holds n redis responses
    --  to be parsed:
    local results = parser.parse_replies(replies, n)
    for i, result in ipairs(results) do
        local res = result[1]
        local typ = result[2]

        -- res and typ have exactly the same meaning as in
        --  the parse_reply method documented above

We also have a build_query function that helps construct raw redis requests from
simple lua values. It simply accepts a lua table, a list of parameters including
the command name.

Let's check out the complete list of redis 2.0 commands:

For the first command in that list, "APPEND   key value", we can just use

local req = parser.build_query({'APPEND', 'some-key', 'some-value'})

to construct a binary request in the return value. Because the redis
command is case insensitive, I usually just use 'append', the lower
case form, as the first element of that list, as in

    local req = parser.build_query({'set', 'foo', 'some value'})
        -- req is the raw TCP request ready to send to the remote redis server
        -- over the TCP connection

null values should be specified by "parser.null" (without quotes) rather
than Lua's "nil".

Boolean values will be converted to 1 or 0, for true and false, respectively.


This module is originally written for ngx_lua + ngx_redis2:

    # To use /usr/include/lua5.1/ and install to /usr/lib/lua/5.1:
    make install

    To use a different Lua other than /usr/include/lua5.1, use the following:

    make LUA_INC=/opt/lua/include

    and install this module to a different location than /usr/lib/lua/5.1:

    make INSTALL_PATH=/opt/lua-exts install

    Please edit the Makefile file to use a custom lua and/or install
    this module into a custom location.

    redis pipelining response parsing and query support

Something went wrong with that request. Please try again.