lua-resty-redis-fast

配置二进制安装包仓库

首先我们需要配置二进制安装包的仓库,按照以下命令进行配置。(命令中的 CLIENT_TOKEN 需要替换成订阅邮件中的有效 Token)

curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com.cn/scripts/get-xray-priv-lib-repo.sh

sudo bash get-xray-priv-lib-repo.sh -l coro-nginx-module -t CLIENT_TOKEN

sudo bash get-xray-priv-lib-repo.sh -l lua-resty-redis-fast -t CLIENT_TOKEN

安装

针对 OpenResty-1.21.4.x

使用 yum 作为包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 等操作系统,执行以下命令进行私有库的安装。

sudo yum install --disablerepo="*" --enablerepo="coro-nginx-module,lua-resty-redis-fast" -y lua-resty-redis-fast-1.21.4

使用 dnf 作为包管理器的 Fedora 等操作系统,执行以下命令进行私有库的安装。

sudo dnf install --disablerepo="*" --enablerepo="coro-nginx-module,lua-resty-redis-fast" -y lua-resty-redis-fast-1.21.4

使用 apt 作为包管理器的 Ubuntu/Debian 等操作系统,执行以下命令进行私有库的安装。

sudo apt-get install -y lua-resty-redis-fast-1.21.4

针对 OpenResty 1.25.3.x

使用 yum 作为包管理器的 CentOS/RockyLinux/Amazon Linux/Alibaba Cloud Linux/Tecent Linux 等操作系统,执行以下命令进行私有库的安装。

sudo yum install --disablerepo="*" --enablerepo="coro-nginx-module,lua-resty-redis-fast"  -y lua-resty-redis-fast-1.25.3

使用 dnf 作为包管理器的 Fedora 等操作系统,执行以下命令进行私有库的安装。

sudo dnf install --disablerepo="*" --enablerepo="coro-nginx-module,lua-resty-redis-fast" -y lua-resty-redis-fast-1.25.3

使用 apt 作为包管理器的 Ubuntu/Debian 等操作系统,执行以下命令进行私有库的安装。

sudo apt-get install -y lua-resty-redis-fast-1.25.3

升级 OpenResty

如果此前已经安装过了 openresty,请执行以下更新命令:

sudo yum upgrade -y openresty

使用

Nginx 配置

在使用前需要在配置文件 nginx.conf 中需要添加以下这些配置项,用以加载 lua-resty-redis-fast 相关的动态模块。

    ...

    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_hiredis_module.so";

    http {
        coro_preload /usr/local/openresty-plus/hiredis/lib/libhiredis.so;

        coro_stack_size  32768;
        ...
    }

Lua 接口

除了 set_keepalive 接口发生了变化,该库的其他接口和开源的 lua-resty-redis 保持一致,以下是一个简单的例子:

    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_module.so";
    load_module "/usr/local/openresty-coro-nginx-module/lib/ngx_http_coro_hiredis_module.so";

    http {
        coro_preload /usr/local/openresty-plus/hiredis/lib/libhiredis.so;
        coro_stack_size  32768;

        server {
            location /test {
                # need to specify the resolver to resolve the hostname
                resolver 8.8.8.8;

                content_by_lua_block {
                    local redis = require "resty.redis.fast"
                    local red = redis:new()

                    red:set_timeout(1000) -- 1 sec

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

                    ok, err = red:select(1)
                    if not ok then
                        ngx.say("failed to select: ", err)
                        return
                    end

                    local res, err = red:set("dog", "an animal")
                    if not res then
                        ngx.say("failed to set dog: ", err)
                        return
                    end

                    local ok, err = red:set_keepalive()
                    if not ok then
                        ngx.say("failed to set keepalive: ", err)
                        return
                    end
                }
            }
        }
    }