A Redis protocol codec for Luvit
Simply install using lit directly:
lit install creationix/redis-client
Or add to your dependencies list.
exports.dependencies = {
"creationix/redis-client"
}
The redis client library wraps the raw codec in an easy to use coroutine based interface using luvit streams.
Send multiple strings to make a query and the response will come back pre-decoded.
local connect = require('redis-client')
coroutine.wrap(function ()
-- Connect to redis
local send = connect { host = "localhost", port = 6379 }
-- Send some commands
send("set", "name", "Tim")
local name = send("get", "name")
assert(name == "Tim")
-- Close the connection
send()
end)()
This codec is transport agnostic. I like to use it with the coro friendly libraries, but it can be used with the node style streams in luvit or even with a non-luvit lua project.
It is a simple encoder/decoder for talking RESP over a socket.
The encoder is a simple function that accepts a table of strings and encodes it as a RESP list. The decoder accepts a chunk of raw data string and tries to consume one message.
If there is not enough data, it simply returns nothing. If there is enough, it returns the parsed value as well as the leftover data.
local codec = require('redis-codec')
local encoded = codec.encode({"set", "name", "Tim"})
local message, extra = codec.decode("$5\r\nHello\r\n+More\r\n")