Skip to content

Commit 18ccda0

Browse files
authored
feat: add a surface to support Redis module (openresty#211)
1 parent 931e4b0 commit 18ccda0

File tree

5 files changed

+141
-4
lines changed

5 files changed

+141
-4
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ addons:
1313
- luarocks
1414

1515
services:
16-
- redis
16+
- docker
1717

1818
cache:
1919
directories:
@@ -74,5 +74,6 @@ script:
7474
- ngx-build $NGINX_VERSION --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-cc-opt="-I$OPENSSL_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB" --add-module=../echo-nginx-module --add-module=../lua-nginx-module --add-module=../stream-lua-nginx-module --with-stream --with-stream_ssl_module --with-debug > build.log 2>&1 || (cat build.log && exit 1)
7575
- nginx -V
7676
- ldd `which nginx`|grep -E 'luajit|ssl|pcre'
77+
- docker run -d -p 6379:6379 --rm --name redis-redisbloom redislabs/rebloom:latest
7778
- TEST_SUBSYSTEM=http prove -r t
7879
- TEST_SUBSYSTEM=stream prove -r t

README.markdown

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,42 @@ Then the output will be
569569

570570
[Back to TOC](#table-of-contents)
571571

572+
Redis Module
573+
==================
574+
575+
This library supports the Redis module. Here is an example with RedisBloom module:
576+
577+
```lua
578+
local cjson = require "cjson"
579+
local redis = require "resty.redis"
580+
-- register the module prefix "bf" for RedisBloom
581+
redis.register_module_prefix("bf")
582+
583+
local red = redis:new()
584+
585+
local ok, err = red:connect("127.0.0.1", 6379)
586+
if not ok then
587+
ngx.say("failed to connect: ", err)
588+
return
589+
end
590+
591+
-- call BF.ADD command with the prefix 'bf'
592+
res, err = red:bf():add("dog", 1)
593+
if not res then
594+
ngx.say(err)
595+
return
596+
end
597+
ngx.say("receive: ", cjson.encode(res))
598+
599+
-- call BF.EXISTS command
600+
res, err = red:bf():exists("dog")
601+
if not res then
602+
ngx.say(err)
603+
return
604+
end
605+
ngx.say("receive: ", cjson.encode(res))
606+
```
607+
572608
Load Balancing and Failover
573609
===========================
574610

lib/resty/redis.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ function _M.new(self)
7474
end
7575

7676

77+
function _M.register_module_prefix(mod)
78+
_M[mod] = function(self)
79+
self._module_prefix = mod
80+
return self
81+
end
82+
end
83+
84+
7785
function _M.set_timeout(self, timeout)
7886
local sock = rawget(self, "_sock")
7987
if not sock then
@@ -446,12 +454,23 @@ function _M.read_reply(self)
446454
end
447455

448456

457+
local function do_cmd(self, cmd, ...)
458+
local module_prefix = rawget(self, "_module_prefix")
459+
if module_prefix then
460+
self._module_prefix = nil
461+
return _do_cmd(self, module_prefix .. "." .. cmd, ...)
462+
end
463+
464+
return _do_cmd(self, cmd, ...)
465+
end
466+
467+
449468
for i = 1, #common_cmds do
450469
local cmd = common_cmds[i]
451470

452471
_M[cmd] =
453472
function (self, ...)
454-
return _do_cmd(self, cmd, ...)
473+
return do_cmd(self, cmd, ...)
455474
end
456475
end
457476

@@ -663,7 +682,7 @@ end
663682
setmetatable(_M, {__index = function(self, cmd)
664683
local method =
665684
function (self, ...)
666-
return _do_cmd(self, cmd, ...)
685+
return do_cmd(self, cmd, ...)
667686
end
668687

669688
-- cache the lazily generated method in our

t/count.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ __DATA__
2222
ngx.say("size: ", n)
2323
';
2424
--- response_body
25-
size: 55
25+
size: 56
2626
--- no_error_log
2727
[error]

t/module.t

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# vim:set ft= ts=4 sw=4 et:
2+
3+
use t::Test;
4+
5+
repeat_each(2);
6+
7+
plan tests => repeat_each() * (3 * blocks());
8+
9+
run_tests();
10+
11+
__DATA__
12+
13+
=== TEST 1: sanity
14+
--- global_config eval: $::GlobalConfig
15+
--- server_config
16+
content_by_lua_block {
17+
local cjson = require "cjson"
18+
local redis = require "resty.redis"
19+
redis.register_module_prefix("bf")
20+
redis.register_module_prefix("test")
21+
22+
local red = redis:new()
23+
24+
local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT)
25+
if not ok then
26+
ngx.say("failed to connect: ", err)
27+
return
28+
end
29+
30+
local res, err = red:del("module_1")
31+
if not res then
32+
ngx.say(err)
33+
return
34+
end
35+
36+
res, err = red:bf():add("module_1", 1)
37+
if not res then
38+
ngx.say(err)
39+
return
40+
end
41+
ngx.say("receive: ", cjson.encode(res))
42+
43+
res, err = red:bf():exists("module_1", 1)
44+
if not res then
45+
ngx.say(err)
46+
return
47+
end
48+
ngx.say("receive: ", cjson.encode(res))
49+
50+
-- call normal command
51+
res, err = red:del("module_1")
52+
if not res then
53+
ngx.say(err)
54+
return
55+
end
56+
ngx.say("receive: ", cjson.encode(res))
57+
58+
-- call cached 'exists' again
59+
res, err = red:exists("module_1")
60+
if not res then
61+
ngx.say(err)
62+
return
63+
end
64+
ngx.say("receive: ", cjson.encode(res))
65+
66+
-- call pre-created 'get' method
67+
res, err = red:test():get()
68+
if not res then
69+
ngx.say(err)
70+
end
71+
72+
red:close()
73+
}
74+
--- response_body_like
75+
receive: 1
76+
receive: 1
77+
receive: 1
78+
receive: 0
79+
ERR unknown command `test.get`.+
80+
--- no_error_log
81+
[error]

0 commit comments

Comments
 (0)