Skip to content

Commit b7a82b9

Browse files
committed
optimize: support to cache the current request object, make it faster.
1 parent be0aec8 commit b7a82b9

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

lib/resty/ngxvar.lua

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,12 @@ int ngx_http_lua_var_ffi_remote_addr(ngx_http_request_t *r,
1818

1919

2020
local var_patched = pcall(function() return C.ngx_http_lua_var_ffi_test() end)
21-
local _M = {
22-
_version = 0.1,
21+
local vars = {
2322
method = ngx.req.get_method,
2423
}
2524

2625

27-
function _M._request()
28-
local r = get_request()
29-
if not r then
30-
return false, "no request found"
31-
end
32-
33-
return r
34-
end
35-
36-
37-
function _M.uri(r)
26+
function vars.uri(r)
3827
r = r or get_request()
3928
if not r then
4029
return false, "no request found"
@@ -45,7 +34,7 @@ function _M.uri(r)
4534
end
4635

4736

48-
function _M.host(r)
37+
function vars.host(r)
4938
r = r or get_request()
5039
if not r then
5140
return false, "no request found"
@@ -56,12 +45,12 @@ function _M.host(r)
5645
end
5746

5847

59-
function _M.status()
48+
function vars.status()
6049
return ngx.status
6150
end
6251

6352

64-
function _M.remote_addr(r)
53+
function vars.remote_addr(r)
6554
r = r or get_request()
6655
if not r then
6756
return false, "no request found"
@@ -72,12 +61,30 @@ function _M.remote_addr(r)
7261
end
7362

7463

75-
return function (name, request)
76-
local method = _M[name]
64+
local _M = {
65+
_version = 0.1,
66+
}
67+
68+
69+
function _M.request()
70+
local r = get_request()
71+
if not r then
72+
return false, "no request found"
73+
end
74+
75+
return r
76+
end
77+
78+
79+
function _M.fetch(name, request)
80+
local method = vars[name]
7781

7882
if not var_patched or not method then
7983
return ngx_var[name]
8084
end
8185

8286
return method(request)
8387
end
88+
89+
90+
return _M

t/sanity.t

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ __DATA__
2323
location /t {
2424
content_by_lua_block {
2525
local var = require("resty.ngxvar")
26-
ngx.say(var("uri"))
26+
ngx.say(var.fetch("uri"))
2727
}
2828
}
2929
--- request
@@ -41,7 +41,7 @@ GET /t/test/bar
4141
location /t {
4242
content_by_lua_block {
4343
local var = require("resty.ngxvar")
44-
ngx.say(var("host"))
44+
ngx.say(var.fetch("host"))
4545
}
4646
}
4747
--- request
@@ -64,7 +64,8 @@ foo.com
6464
}
6565
log_by_lua_block {
6666
local var = require("resty.ngxvar")
67-
ngx.log(ngx.ERR, var("status"), " type: ", type(var("status")))
67+
ngx.log(ngx.ERR, var.fetch("status"),
68+
" type: ", type(var.fetch("status")))
6869
}
6970
}
7071
--- request
@@ -81,8 +82,8 @@ GET /t
8182
location /t {
8283
content_by_lua_block {
8384
local var = require("resty.ngxvar")
84-
local req = var("_request")
85-
ngx.say(var("host", req))
85+
local req = var.fetch("_request")
86+
ngx.say(var.fetch("host", req))
8687
}
8788
}
8889
--- request
@@ -102,7 +103,7 @@ foo.com
102103
location /t {
103104
content_by_lua_block {
104105
local var = require("resty.ngxvar")
105-
ngx.say(var("remote_addr"))
106+
ngx.say(var.fetch("remote_addr"))
106107
}
107108
}
108109
--- request

0 commit comments

Comments
 (0)