A method for Openresty to read Redis to shared memory in the init_by_lua_file phase

Source: Internet
Author: User
Premise:

For openresty or Ngx_lua please refer to: http://openresty.org/en/

Problem:

When using Openresty to read data within Redis, Redis is not connected in init_by_lua_file, so sometimes it is only possible to read Redis data in the rewrite or access phase, and what is the way to read it during the initialization phase?

Workaround:

The method of invoking the shell command in the init phase is equivalent to executing a shell command in the init phase, reading the Redis data, parsing the read text data, which can be achieved by reading the Redis data into Nginx

Example:

1 There is a hash in Redis that stores data as:

Hmset Test_hash F1 v1 F2 v2 F3 v3

The code read in 2 Init_by_lua_file is:

Local cmd = "redis-cli hgetall test_hash"

local F = io.popen (cmd)

Local content = F:read ("*a")

F:close ()

Memory in the content above is the data in Redis.

Note that the content here is a string that needs to be re-parsed into a dictionary, can be stored in a variable, or can exist in shared memory.

The above describes a openresty in the init_by_lua_file phase of the method of reading Redis to shared memory, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.