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
針對 OpenResty 1.27.1.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.27.1
使用 dnf
作為包管理器的 Fedora
等作業系統,執行以下命令進行私有庫的安裝。
sudo dnf install --disablerepo="*" --enablerepo="coro-nginx-module,lua-resty-redis-fast" -y lua-resty-redis-fast-1.27.1
使用 apt
作為包管理器的 Ubuntu/Debian
等作業系統,執行以下命令進行私有庫的安裝。
sudo apt-get install -y lua-resty-redis-fast-1.27.1
安裝對應版本的 OpenResty
安裝對應版本的 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-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
}
}
}
}