Prerequisite:
activating and activating and activating ngx_lua to ngx_lua to ngx_lua to ngx to ngx_lua to being using out of out out of Redis data can only be read in the rewrite or access phase. How to read it in the initialization phase?
Solution:
Use the method of calling the shell command in the init stage, which is equivalent to executing a shell command in the init stage, reading the redis data, and parsing the read text data, that is, you can read the redis data to Example in nginx:
1 There is a hash in redis, the stored data is:
hmset test_hash f1 v1 f2 v2 f3 v3
2 The code read in init_by_lua_file is:
local cmd = "redis-cli hgetall test_hash"
local f = io.popen(cmd)
local content = f:read("*a")
f:close()The memory in the above content is the data in redis.
It should be noted that the content here is a string, which needs to be re-parsed into a dictionary by yourself. It can be stored in a variable or in shared memory.
The above introduces a method for openresty to read redis into shared memory in the init_by_lua_file stage, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.