Skip to content

Commit 062f208

Browse files
doujiang24agentzh
authored andcommitted
optimize: we now alway call tostring() upon args in redis query methods.
also reduced Lua string concatenations in redis query composition. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
1 parent 7a19cf9 commit 062f208

File tree

2 files changed

+90
-16
lines changed

2 files changed

+90
-16
lines changed

lib/resty/redis.lua

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,23 +248,23 @@ end
248248
local function _gen_req(args)
249249
local nargs = #args
250250

251-
local req = new_tab(nargs + 1, 0)
251+
local req = new_tab(nargs * 5 + 1, 0)
252252
req[1] = "*" .. nargs .. "\r\n"
253-
local nbits = 1
253+
local nbits = 2
254254

255255
for i = 1, nargs do
256256
local arg = args[i]
257-
nbits = nbits + 1
257+
if type(arg) ~= "string" then
258+
arg = tostring(arg)
259+
end
258260

259-
if not arg then
260-
req[nbits] = "$-1\r\n"
261+
req[nbits] = "$"
262+
req[nbits + 1] = #arg
263+
req[nbits + 2] = "\r\n"
264+
req[nbits + 3] = arg
265+
req[nbits + 4] = "\r\n"
261266

262-
else
263-
if type(arg) ~= "string" then
264-
arg = tostring(arg)
265-
end
266-
req[nbits] = "$" .. #arg .. "\r\n" .. arg .. "\r\n"
267-
end
267+
nbits = nbits + 5
268268
end
269269

270270
-- it is much faster to do string concatenation on the C land

t/sanity.t

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ __DATA__
4646
return
4747
end
4848
49-
res, err = red:set("dog", "an animal")
49+
local res, err = red:set("dog", "an animal")
5050
if not res then
5151
ngx.say("failed to set dog: ", err)
5252
return
@@ -239,7 +239,7 @@ get nokey: 0 (table)
239239
return
240240
end
241241
242-
res, err = red:set("connections", 10)
242+
local res, err = red:set("connections", 10)
243243
if not res then
244244
ngx.say("failed to set connections: ", err)
245245
return
@@ -344,7 +344,7 @@ connections: 1
344344
return
345345
end
346346
347-
res, err = red:incr("connections", 12)
347+
local res, err = red:incr("connections", 12)
348348
if not res then
349349
ngx.say("failed to set connections: ", res, ": ", err)
350350
return
@@ -593,7 +593,7 @@ reused times: 1
593593
return
594594
end
595595
596-
res, err = red:set("dog", "an animal")
596+
local res, err = red:set("dog", "an animal")
597597
if not res then
598598
ngx.say("failed to set dog: ", err)
599599
return
@@ -653,7 +653,7 @@ res: ["an animal",null,"an animal"]
653653
return
654654
end
655655
656-
res, err = red:hmset("animals", { dog = "bark", cat = "meow", cow = "moo" })
656+
local res, err = red:hmset("animals", { dog = "bark", cat = "meow", cow = "moo" })
657657
if not res then
658658
ngx.say("failed to set animals: ", err)
659659
return
@@ -700,3 +700,77 @@ cow: moo
700700
--- no_error_log
701701
[error]
702702

703+
704+
705+
=== TEST 13: boolean args
706+
--- http_config eval: $::HttpConfig
707+
--- config
708+
location /t {
709+
content_by_lua '
710+
local redis = require "resty.redis"
711+
local red = redis:new()
712+
713+
red:set_timeout(1000) -- 1 sec
714+
715+
local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
716+
if not ok then
717+
ngx.say("failed to connect: ", err)
718+
return
719+
end
720+
721+
ok, err = red:set("foo", true)
722+
if not ok then
723+
ngx.say("failed to set: ", err)
724+
return
725+
end
726+
727+
local res, err = red:get("foo")
728+
if not res then
729+
ngx.say("failed to get: ", err)
730+
return
731+
end
732+
733+
ngx.say("foo: ", res, ", type: ", type(res))
734+
735+
ok, err = red:set("foo", false)
736+
if not ok then
737+
ngx.say("failed to set: ", err)
738+
return
739+
end
740+
741+
local res, err = red:get("foo")
742+
if not res then
743+
ngx.say("failed to get: ", err)
744+
return
745+
end
746+
747+
ngx.say("foo: ", res, ", type: ", type(res))
748+
749+
ok, err = red:set("foo", nil)
750+
if not ok then
751+
ngx.say("failed to set: ", err)
752+
end
753+
754+
local res, err = red:get("foo")
755+
if not res then
756+
ngx.say("failed to get: ", err)
757+
return
758+
end
759+
760+
ngx.say("foo: ", res, ", type: ", type(res))
761+
762+
local ok, err = red:set_keepalive(10, 10)
763+
if not ok then
764+
ngx.say("failed to set_keepalive: ", err)
765+
end
766+
';
767+
}
768+
--- request
769+
GET /t
770+
--- response_body
771+
foo: true, type: string
772+
foo: false, type: string
773+
failed to set: ERR wrong number of arguments for 'set' command
774+
foo: false, type: string
775+
--- no_error_log
776+
[error]

0 commit comments

Comments
 (0)