Home > Backend Development > PHP Tutorial > A method for openresty to read redis into shared memory during the init_by_lua_file stage

A method for openresty to read redis into shared memory during the init_by_lua_file stage

WBOY
Release: 2016-07-28 08:27:38
Original
2230 people have browsed it

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template