适用于 macOS 系统,依赖:
- openresty https://openresty.org/cn/installation.html
- redis
$ brew install openresty/brew/openresty
..$ mkdir hello-redis
$ cd hello-redis
$ opm --cwd get openresty/lua-resty-redis
$ vim hello.lua
<见下面>
$ resty hello.lua
.. 成功--
-- hello.lua
--
local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
ok, err = red:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", ok)
local res, err = red:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)