File: define_commands.lua

package info (click to toggle)
lua-redis 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 264 kB
  • ctags: 126
  • sloc: makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,158 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package.path = "../src/?.lua;src/?.lua;" .. package.path
pcall(require, "luarocks.require")

local redis = require 'redis'

local params = {
    host = '127.0.0.1',
    port = 6379,
}

-- commands defined in the redis.commands table are available at module
-- level and are used to populate each new client instance.
redis.commands.hset = redis.command('hset')

-- you can also specify a response callback to parse raw replies
redis.commands.hgetall = redis.command('hgetall', {
    response = function(reply, command, ...)
        local new_reply = { }
        for i = 1, #reply, 2 do new_reply[reply[i]] = reply[i + 1] end
        return new_reply
    end
})

local client = redis.connect(params)
client:select(15) -- for testing purposes

client:hset('user:1000', 'name', 'John Doe')
client:hset('user:1000', 'nickname', 'anonymous')
client:hset('user:1000', 'email', 'anything@anywhere.tld')

local user = client:hgetall('user:1000')
print(string.format('%s is also known as %s and his email address is %s.',
    user.name, user.nickname, user.email
))

--[[
OUTPUT:
John Doe is also known as anonymous and his email address is anything@anywhere.tld.
]]