Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Lua module for parsing raw redis responses
Perl C

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
t
.gitignore
Makefile
README
ddebug.h
redis-parser.c

README

lua-redis-parser

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
    end

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
    end

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:

   http://redis.io/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.

Background

This module is originally written for ngx_lua + ngx_redis2:

    http://github.com/chaoslawful/lua-nginx-module

    https://github.com/agentzh/redis2-nginx-module

Installation
    Build requirements:
    - Lua (http://www.lua.org/)
    Or:
    - LuaJIT (http://www.luajit.org/)

    Gnu make and gcc is required to build this module.

    Linux/BSD/Solaris
        gmake CC=gcc
        gmake install CC=gcc

    Mac OS X
        make LDFLAGS='-bundle -undefined dynamic_lookup' CC=gcc
        make install

    If your Lua or LuaJIT is not installed into the system, specify
    its include directory like this:

        make LUA_INCLUDE_DIR=/opt/luajit/include/luajit-2.0

    You can specify a custom path for the installation target:
        make install LUA_LIB_DIR=/opt/lualib

    The DESTDIR variable is also supported, to ease RPM packaging.

TODO
    redis pipelining response parsing and query support

Something went wrong with that request. Please try again.