https://github.com/creationix/redis-luvit
A redis client for luvit
https://github.com/creationix/redis-luvit
Last synced: 3 months ago
JSON representation
A redis client for luvit
- Host: GitHub
- URL: https://github.com/creationix/redis-luvit
- Owner: creationix
- License: mit
- Created: 2015-11-11T10:27:21.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-03-04T05:22:44.000Z (over 9 years ago)
- Last Synced: 2025-04-10T02:55:39.353Z (about 1 year ago)
- Language: Lua
- Size: 13.7 KB
- Stars: 12
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-luvit
A [Redis][] protocol codec for [Luvit][]
## Installing
Simply install using lit directly:
```sh
lit install creationix/redis-client
```
Or add to your dependencies list.
```lua
exports.dependencies = {
"creationix/redis-client"
}
```
## Usage
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.
```lua
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)()
```
## Using the raw codec directly
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.
```lua
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")
```
[Redis]: http://redis.io/
[Luvit]: https://luvit.io/
[RESP]: http://redis.io/topics/protocol