lua-resty-redis-cluster-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 coro-hiredis-nginx-module -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,coro-hiredis-nginx-module" -y lua-resty-redis-cluster-fast-1.21.4
使用 dnf 作为包管理器的 Fedora 等操作系统,执行以下命令进行私有库的安装。
sudo dnf install --disablerepo="*" --enablerepo="coro-nginx-module,coro-hiredis-nginx-module" -y lua-resty-redis-cluster-fast-1.21.4
使用 apt 作为包管理器的 Ubuntu/Debian 等操作系统,执行以下命令进行私有库的安装。
sudo apt-get install -y lua-resty-redis-cluster-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,coro-hiredis-nginx-module"  -y lua-resty-redis-cluster-fast-1.25.3
使用 dnf 作为包管理器的 Fedora 等操作系统,执行以下命令进行私有库的安装。
sudo dnf install --disablerepo="*" --enablerepo="coro-nginx-module,coro-hiredis-nginx-module" -y lua-resty-redis-cluster-fast-1.25.3
使用 apt 作为包管理器的 Ubuntu/Debian 等操作系统,执行以下命令进行私有库的安装。
sudo apt-get install -y lua-resty-redis-cluster-fast-1.25.3
安装 OpenResty
比如,对于 yum 作为包管理器的系统,想要安装 1.25.3 的 OpenResty:
$ yum list --showduplicates openresty
Last metadata expiration check: 0:07:22 ago on Sat 13 Sep 2025 07:15:05 AM CST.
Available Packages
openresty.x86_64                                                      1.21.4.4-1.el9                                                      openresty
openresty.x86_64                                                      1.25.3.2-1.el9                                                      openresty
openresty.x86_64                                                      1.27.1.2-1.el9                                                      openresty
$ sudo yum install openresty-1.25.3.2-1.el9
比如,对于 dnf 作为包管理器的系统,想要安装 1.25.3 的 OpenResty:
$ dnf list --showduplicates openresty
Last metadata expiration check: 0:07:22 ago on Sat 13 Sep 2025 07:15:05 AM CST.
Available Packages
openresty.x86_64                                                      1.21.4.4-1.el9                                                      openresty
openresty.x86_64                                                      1.25.3.2-1.el9                                                      openresty
openresty.x86_64                                                      1.27.1.2-1.el9                                                      openresty
$ sudo dnf install openresty-1.25.3.2-1.el9
对于使用 apt 作为包的理器的系统,安装 openresty 1.25.3 的 OpenResty:
$ apt-cache madison openresty
 openresty | 1.27.1.2-1~noble1 | https://openresty.org/package/ubuntu noble/main amd64 Packages
 openresty | 1.27.1.1-1~noble1 | https://openresty.org/package/ubuntu noble/main amd64 Packages
 openresty | 1.25.3.2-2~noble1 | https://openresty.org/package/ubuntu noble/main amd64 Packages
$ sudo apt install openresty=1.25.3.2-2~noble1
使用
Nginx 配置
在使用前需要在配置文件 nginx.conf 中需要添加以下这些配置项,用以加载 lua-resty-redis-cluster-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 接口
除了 connect 和 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.cluster.fast"
                    local red = redis:new()
                    red:set_timeouts(1000, 1000, 1000) -- 1 sec
                    -- connect via ip address directly
                    local ok, err = red:connect("127.0.0.1", 6379, {
                        cluster = "cluster",
                        other_nodes = {
                            { "127.0.0.1", 6380 },
                            { "127.0.0.1", 6381 },
                            { "127.0.0.1", 6382 },
                        },
                        no_slaves = true
                    })
                    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)
                    local ok, err = red:set_keepalive()
                    if not ok then
                        ngx.say("failed to set keepalive: ", err)
                        return
                    end
                }
            }
        }
    }
connect 和 set_keepalive 接口的说明如下:
connect
TODO:目前不支持 unix domain socket 和 SSL.
syntax: ok, err = red:connect(host, port, options_table)
连接到 Redis 入口节点所监听的主机和端口。在实际连接到远程后端之前,该方法将始终查找连接池中由该方法的先前调用创建的匹配的空闲连接。
options_table 参数是一个 Lua table,包括这些参数:
cluster为正在使用的 Redis 集群连接池指定一个自定义名称,这是一个必填字段。
pool_size指定连接池的大小,默认是 128。
other_nodes指定可以连接的其他 Redis 节点,当与入口节点的连接失败时,它将尝试连接到其他节点。
other_nodes参数是一个 Lua 表,用于保存多个节点的配置信息,例如:{ { "127.0.0.1", 6379 }, { "127.0.0.1", 6380 }, { "127.0.0.1", 6381 }, }no_slaves是否禁止从 Redis slave 节点读取数据,默认为
false。
set_keepalive
syntax: ok, err = red:set_keepalive()
为了兼容开源的
lua-resty-redis库的set_keepalive(max_idle_timeout, pool_size)方法,我们仍然允许传入两个参数,但我们会忽略传入的参数。
将当前 Redis 连接放入到 cosocket 连接池中。
只在你要调用 close 方法的地方调用此方法。调用此方法将立即把当前的 Redis 对象变成 closed 状态。除 connect() 外,对当前对象的任何后续操作都将返回 closed 错误。