|
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) |
0 commit comments