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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
=encoding utf-8
=head1 NAME
lua-redis-parser - Redis reply parser and request constructor library for Lua
=head1 Version
This document describes lua-redis-parser L<v0.13|https://github.com/agentzh/lua-redis-parser/tags> released on 8 April 2017.
=head1 Description
This lua library implements 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.
This library is usually used by Lua code running atop L<lua-nginx-module|http://github.com/chaoslawful/lua-nginx-module> to access
redis backends though L<redis2-nginx-module|http://github.com/agentzh/redis2-nginx-module>.
=head1 Functions
The C<parser> variable used below is referring to the variable holding the return value of C<require "redis.parser">. In other words, we assume you have written the following code first:
local parser = require "redis.parser"
=head2 parse_reply
B<syntax:> I<res, typ = parser.parse_reply(raw_reply)>
Parses the single (or the first) raw redis reply from the C<raw_reply> string and returns the Lua data structure C<res>, as well as the reply type C<typ>.
Here is an example:
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
=head2 parse_replies
B<syntax:> I<results = parser.parse_replies(raw_replies)>
Similar to the L<parse_reply> method, but parse multiple pipelined redis replies in the C<raw_replies> string argument. Returns a table of all the parsed results where each result is an array-like table consists of two elements, C<res> and C<typ>, which have exactly the same meaning as the return values of the L<parse_reply> function.
For instance,
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
=head2 typename
B<syntax:> I<str = parser.typename(typ)>
Returns the textual representation of the reply type values returned by the L<parse_reply> and L<parse_replies> functions. Here's the correspondence:
parser.typename(parser.BAD_REPLY) == "bad reply"
parser.typename(parser.INTEGER_REPLY) == "integer reply"
parser.typename(parser.ERROR_REPLY) == "error reply"
parser.typename(parser.STATUS_REPLY) == "status reply"
parser.typename(parser.BULK_REPLY) == "bulk reply"
parser.typename(parser.MULTI_BULK_REPLY) == "multi-bulk reply"
=head2 build_query
B<syntax:> I<raw_request = parser.build_query(args)>
Constructs a raw Redis request from simple Lua values. It simply accepts a Lua array-like table, a list of parameters including
the command name.
Please check out the complete list of redis 2.0 commands,
E<lt>http://redis.io/commandsE<gt>
The first command in that list, "APPEND key value", for example, we can just use
local parser = require "redis.parser"
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 parser = require "redis.parser"
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 C<parser.null> rather than Lua's C<nil> value.
Boolean values will be converted to C<1> or C<0>, for C<true> and C<false>, respectively.
=head1 Constants
=head2 BAD_REPLY
B<syntax:> I<typ = parser.BAD_REPLY>
=head2 INTEGER_REPLY
B<syntax:> I<typ = parser.INTEGER_REPLY>
=head2 ERROR_REPLY
B<syntax:> I<typ = parser.ERROR_REPLY>
=head2 STATUS_REPLY
B<syntax:> I<typ = parser.STATUS_REPLY>
=head2 BULK_REPLY
B<syntax:> I<typ = parser.BULK_REPLY>
=head2 MULTI_BULK_REPLY
B<syntax:> I<typ = parser.MULTI_BULK_REPLY>
=head2 null
B<syntax:> I<val = parser.null>
=head1 Background
This module is originally written for L<lua-nginx-module|http://github.com/chaoslawful/lua-nginx-module> and L<redis2-nginx-module|http://github.com/agentzh/redis2-nginx-module>:
=head1 Report Bugs
Although a lot of effort has been put into testing and code tuning, there must be some serious bugs lurking somewhere in this module. So whenever you are bitten by any quirks, please don't hesitate to
=over
=item 1.
create a ticket on the L<issue tracking interface|http://github.com/agentzh/lua-redis-parser/issues> provided by GitHub,
=item 2.
or send a bug report or even patches to C<agentzh@gmail.com>.
=back
=head1 Source Repository
Available on GitHub at L<agentzhE<sol>lua-redis-parser|http://github.com/agentzh/lua-redis-parser>.
=head1 Installation
=head2 Build requirements
=over
=item *
Lua (http://www.lua.org/)
=item *
or LuaJIT (http://www.luajit.org/)
=item *
Latest source tarball of this library downloaded from E<lt>https://github.com/agentzh/lua-redis-parser/tagsE<gt>
=back
Gnu make and gcc is required to build this module.
=head2 Linux/BSD/Solaris
gmake CC=gcc
gmake install CC=gcc
=head2 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 C<DESTDIR> variable is also supported, to ease RPM packaging.
This library is included and enabled by default in the L<OpenResty bundle|http://openresty.org>.
=head1 Author
=over
=item *
Yichun "agentzh" Zhang (章亦春) E<lt>agentzh@gmail.comE<gt>
=back
=head1 Copyright & License
This module is licenced under the BSD license.
Copyright (C) 2009-2017, by Yichun "agentzh" Zhang (章亦春) E<lt>agentzh@gmail.comE<gt>, OpenResty Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
=over
=item *
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
=back
=over
=item *
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
=back
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=head1 SEE ALSO
=over
=item *
Use case: L<Dynamic Routing Based On Redis|http://openresty.org/#DynamicRoutingBasedOnRedis>
=item *
L<lua-nginx-module|http://github.com/chaoslawful/lua-nginx-module>
=item *
L<redis2-nginx-module|http://github.com/agentzh/redis2-nginx-module>
=item *
L<Redis official site|http://redis.io/>
=back
|