Skip to content

Commit ed6ea1a

Browse files
committed
doc: first version.
1 parent b7a82b9 commit ed6ea1a

2 files changed

Lines changed: 129 additions & 2 deletions

File tree

README.md

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,93 @@
1-
# lua-var-nginx-module
2-
fetch nginx variable by Luajit with FFI way which is fast and cheap
1+
lua-var-nginx-module
2+
====================
3+
4+
Fetchs Nginx variable by Luajit with FFI way which is fast and cheap.
5+
6+
```lua
7+
local function test1()
8+
local start_time = ngx.now()
9+
10+
local var = ngx.var
11+
local uri
12+
for i = 1, 10000 * 1000 do
13+
uri = var.uri
14+
if not uri then
15+
uri = uri .. "xxx"
16+
end
17+
end
18+
19+
ngx.update_time()
20+
ngx.say("ngx.var directly, used time: ", ngx.now() - start_time)
21+
end
22+
23+
local function test2()
24+
local start_time = ngx.now()
25+
26+
local ngxvar = require("resty.ngxvar").fetch
27+
local req = ngxvar("_request")
28+
local uri
29+
30+
for i = 1, 10000 * 1000 do
31+
uri = ngxvar("uri", req)
32+
if not uri then
33+
uri = uri .. "xxx"
34+
end
35+
end
36+
37+
ngx.update_time()
38+
ngx.say("ngxvar patch, used time: ", ngx.now() - start_time)
39+
end
40+
41+
test1() -- ngx.var directly, used time: 0.49900007247925
42+
test2() -- fetch with ngxvar, used time: 0.08299994468689
43+
```
44+
45+
Compares to `ngx.var`, performance has increased by more than five times. ^_^
46+
47+
48+
Table of Contents
49+
=================
50+
* [Install](#install)
51+
* [Methods](#methods)
52+
* [request](#request)
53+
* [fetch](#fetch)
54+
* [TODO](#todo)
55+
56+
Method
57+
======
58+
59+
### request
60+
61+
`syntax: req = ngxvar.request()`
62+
63+
Returns the request object of current request. We can cache it at your Lua code
64+
land if we try to fetch more than one variable in one request.
65+
66+
[Back to TOC](#table-of-contents)
67+
68+
### fetch
69+
70+
`syntax: val = ngxvar.fetch(name, req)`
71+
72+
Returns the Nginx variable value by name.
73+
74+
```nginx
75+
location /t {
76+
content_by_lua_block {
77+
local var = require("resty.ngxvar")
78+
local req = var.fetch("_request")
79+
80+
ngx.say(var.fetch("host", req))
81+
ngx.say(var.fetch("uri", req))
82+
}
83+
}
84+
```
85+
86+
[Back to TOC](#table-of-contents)
87+
88+
TODO
89+
====
90+
91+
* support more variables.
92+
93+
[Back to TOC](#table-of-contents)

benchmark/base.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local function test1()
2+
local start_time = ngx.now()
3+
4+
local var = ngx.var
5+
local uri
6+
for i = 1, 10000 * 1000 do
7+
uri = var.uri
8+
if not uri then
9+
uri = uri .. "xxx"
10+
end
11+
end
12+
13+
ngx.update_time()
14+
ngx.say("ngx.var directly, used time: ", ngx.now() - start_time)
15+
end
16+
17+
local function test2()
18+
local start_time = ngx.now()
19+
20+
local ngxvar = require("resty.ngxvar").fetch
21+
local req = ngxvar("_request")
22+
local uri
23+
24+
for i = 1, 10000 * 1000 do
25+
uri = ngxvar("uri", req)
26+
if not uri then
27+
uri = uri .. "xxx"
28+
end
29+
end
30+
31+
ngx.update_time()
32+
ngx.say("ngxvar patch, used time: ", ngx.now() - start_time)
33+
end
34+
35+
test1()
36+
test2()

0 commit comments

Comments
 (0)