简体   繁体   English

如何在Redis中使用Lua?

[英]How to use Lua with Redis?

I am using Lua+nginx with OpenResty bundle. 我正在将Lua + nginx与OpenResty捆绑包一起使用。 But problem comes when I try to connect Lua script with Redis, I'm not able to make successful connection. 但是,当我尝试将Lua脚本与Redis连接时,问题就来了,我无法成功建立连接。 I have gone through a lot of links and blogs, But in the end it always fails. 我已经浏览了很多链接和博客,但最后总是失败。 This is the snippet of code which I am trying. 这是我正在尝试的代码片段。

   server {
       location /test {
           content_by_lua '
               local redis = require "resty.redis" // **Problem in code "Not able to require "resty.redis""**

               local red = redis:new()

               red:set_timeout(1000) -- 1 sec

               -- or connect to a unix domain socket file listened
               -- by a redis server:
               --     local ok, err = red:connect("unix:/path/to/redis.sock")

               local ok, err = red:connect("127.0.0.1", 6379)
               if not ok then
                   ngx.say("failed to connect: ", err)
                   return
               end
  }
}

Assuming "not able to require" means you are getting module 'resty.redis' not found message with a list of paths, the error indicates that you are missing the module. 假设“无法要求”表示您正在获取module 'resty.redis' not found消息,其中包含路径列表,则该错误表明您缺少该模块。 You need to check the paths that are listed and make sure that resty/redis.lua is in one of those folders. 您需要检查列出的路径,并确保resty/redis.lua在这些文件夹之一中。 You will find that file in lua-resty-redis-<version> folder in your OpenResty installation. 您将在OpenResty安装的lua-resty-redis-<version>文件夹中找到该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM