小编Lua*_*yen的帖子

ngx lua:局部变量的范围,init在init_by_lua_block中

我是 nginx lua 的新手,并且从以前的开发人员那里得到了设置。试图通过文档来了解范围,但我很不确定。

现在就是这样

init_by_lua_block {
    my_module = require 'my_module'
    my_module.load_data()
}


location / {
    content_by_lua_block {
        my_module.use_data()
    }
}
Run Code Online (Sandbox Code Playgroud)

在 my_module 中

local _M = {}
local content = {}

function _M.use_data()
    -- access content variable
end

function _M.load_data()
    -- code to load json data into content variable
end

return _M
Run Code Online (Sandbox Code Playgroud)

所以我的理解是,内容是一个局部变量,因此它的生命周期在每个请求内。然而,它正在被初始化init_by_lua_block,并且被其他本地函数使用,这让我感到困惑。这是一个好的做法吗?这个内容变量的实际生命周期是多少?

非常感谢您的阅读。

lua nginx openresty lua-ngx-module

4
推荐指数
1
解决办法
6805
查看次数

标签 统计

lua ×1

lua-ngx-module ×1

nginx ×1

openresty ×1