Name
lua-resty-redis
- Lua redis client for the ngx_lua based on openresty/lua-resty-redis.
Usage
The redis
operation method is basically the same as the official redis
library.
It just saves the steps of setting timeout time, creating connection, setting keepalive
connection pool, closing connection, etc.
It greatly facilitates our operation.
Redis basic commands
local redis = require "redis_wrap"
-- Redis configuration parameter list, if you don't want to enable keepalive,
-- you need to remove the two elements of keepalive in the table below,
-- and then add a new one: keepalive = "off".
local redis_opts = {
db_index = 1,
timeout = 2000, -- 2 seconds.
keepalive_max_idle_timeout = 10000, -- 10 seconds.
keepalive_pool_size = 20,
}
-- Get the redis object.
local red = redis:new(redis_opts)
-- set
local ok, err = red:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
-- get
local res, err = red:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
Pipeline requests
local redis = require "redis_wrap"
local redis_opts = {
db_index = 1,
timeout = 2000, -- 2 seconds.
keepalive_max_idle_timeout = 10000, -- 10 seconds.
keepalive_pool_size = 20,
}
local red = redis:new(redis_opts)
-- Parameters can be omitted, adding parameters is for efficient processing when the number of command entries is known.
red:init_pipeline(4)
red:set("dog", "111")
red:set("cat", "222")
red:get("cat")
red:get("dog")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit the pipelined requests: ", err)
return
end
References
https://github.com/openresty/lua-resty-redis
https://moonbingbing.gitbooks.io/openresty-best-practices/content/redis/out_package.html