OpenResty:是否可以在不使用指令的情况下在 Lua 中创建一个 ngx.shared.DICT?

OpenResty: Is it possible to create a ngx.shared.DICT in Lua without using a directive?

我目前正在为现有的 OpenResty application. It uses some shared dictionaries, which are created in the nginx.conf file via the lua_shared_dict 指令编写一些测试。

我可以为它编写自己的模拟实现,但我想知道是否可以通过编程方式创建一个 ngx.shared.DICT 对象?

是否可以在 Lua 内创建它,还是自己创建模拟实现更好?


背景:我目前的测试设置非常简单。我使用 busted as a test framework and run it from the command line with the resty binary. The idea comes from this article.

我没有找到创建共享词典的编程方式,所以我最终模拟了它:

ngx.shared.someDict = {}
ngx.shared.someDict.get = function(self, key)
  return ngx.shared.someDict[key]
end
ngx.shared.someDict.set = function(self, key, val)
  ngx.shared.someDict[key] = val
end

ngx.shared.DICT 是 ngx_shm_zone_t,nginx 使用它来在进程之间共享内存。跨越 nginx guide ,配置解析时分配的共享内存,因此可能无法在 lua 代码中初始化。特别是,ngx.shared.DICT 用于在进程之间共享内存。当 worker 初始化时,无法通过 mmap 调用分配与父进程共享的内存。