Skip to content

Instantly share code, notes, and snippets.

@smallfish
Last active August 17, 2020 03:44
Show Gist options
  • Select an option

  • Save smallfish/fcb41941023681e8af4678bcb72b49b1 to your computer and use it in GitHub Desktop.

Select an option

Save smallfish/fcb41941023681e8af4678bcb72b49b1 to your computer and use it in GitHub Desktop.

安装依赖

适用于 macOS 系统,依赖:

$ 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment