1. 在 Nginx 配置文件里使用 Lua 操作 MySQL 需要使用 lua-resty-redis 模块 : https://github.com/openresty/lua-resty-redis

  2. 在 Nginx 配置文件中引入 lua-resty-redis

     # nginx.conf
     http {
         lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";
         ...
     }
    
  3. lua-resty-redis API : https://github.com/openresty/lua-resty-redis#methods

  4. 操作数据库 Demo

     # nginx.conf
     location /test-redis {
         default_type "text/plain";
    
         content_by_lua_block {
             local md_redis = require("resty.redis")
             local redis = md_redis:new()
    
             redis:set_timeout(1000) -- 1 sec
    
             -- or connect to a unix domain socket file listened by a redis server:
             -- local ok, err = redis:connect("unix:/path/to/redis.sock")
    
             local ok, err = redis:connect("10.253.1.252", 6399)
             if not ok then
                 ngx.say("connect -- failed to connect: ", err)
                 return
             end
    
             ok, err = redis:set("dog", "an animal.")
             if not ok then
                 ngx.say("set -- failed to set a dog: ", err)
                 return
             end
    
             ngx.say("set a dog -- result: ", ok)
    
             local res, err = redis:get("dog")
             if not res then
                 ngx.say("get -- failed to get dog: ", err)
             end
    
             if res == ngx.null then
                 ngx.say("get -- dog not found,")
                 return
             end
    
             ngx.say("get -- dog: ", res)
    
             redis:init_pipeline()
             redis:set("cat", "Marry")
             redis:set("horse", "Bob")
             redis:set("person", "测试redis")
             redis:get("cat")
             redis:get("horse")
             redis:get("person")
             local results, err = redis:commit_pipeline()
             if not results then
                 ngx.say("commit -- failed to commit the piplined requests: ", err)
                 return
             end
    
             for i, res in ipairs(results) do
                 ngx.say("type(res) ---- ", type(res))
    
                 if type(res) == "table" then
                     if res[1] == false then
                         ngx.say("ipairs -- failed to run command ", i, ": ", res[2])
                     else
                         -- process the table value
                         ngx.say("ipairs -- res[i]:", res[i])
                     end
                 else
                     -- process the scalar value
                     ngx.say("ipairs -- key: ", i, " -- value: ", res)
                 end
             end
    
             -- put it into the connection pool of size 100, with 10 seconds max idle time
             local ok, err = redis:set_keepalive(10000, 100)
             if not ok then
                 ngx.say("close -- failed to set keepalive: ", err)
                 return
             end
    
             -- or just close the connection right away:
             -- local ok, err = redis:close()
             -- if not ok then
             --     ngx.say("close -- failed to close: ", err)
             --     return
             -- end
         }
     }
    

results matching ""

    No results matching ""