Hello, currently the official doc says three ways (actually two) for server-wide data sharing :
If server-wide data sharing is required, then use one or more of the following approaches:
- Use the ngx.shared.DICT API provided by this module.
- Use only a single nginx worker and a single server (this is however not recommended when there is a multi core CPU or multiple CPUs in a single machine).
- Use data storage mechanisms such as
memcached
, redis
, MySQL
or PostgreSQL
. The OpenResty bundleassociated with this module comes with a set of companion Nginx modules and Lua libraries that provide interfaces with these data storage mechanisms.
On a 2-core server, with the following code, no matter how many cocurrent requests you make, the module `resty.view` will only be loaded once at `init_by_lua_block` and the response will always be 0.
I think maybe this is a way for server-wide data sharing which the official doc doesn't mention. But I'm not sure. Does any one use like this?
if you remove the init_by_lua_block code, you will find the response may be 0 or 1 which means the module is loaded twice (this is the expected result as the official doc says so)
resty.view
ngx.log(ngx.ERR,'loaded...')
return ngx.worker.id()
nginx.conf
worker_processes auto;
user root root;
events {
worker_connections 1024;
}
http {
init_by_lua_block {
require "resty.view"
}
server {
location / {
content_by_lua_block {
ngx.print(require"resty.view")
}
}
}
}