https://github.com/windvalley/lua-resty-redis
Lua redis client for the ngx_lua based on openresty/lua-resty-redis.
https://github.com/windvalley/lua-resty-redis
lua openresty redis
Last synced: 4 months ago
JSON representation
Lua redis client for the ngx_lua based on openresty/lua-resty-redis.
- Host: GitHub
- URL: https://github.com/windvalley/lua-resty-redis
- Owner: windvalley
- License: mit
- Created: 2019-11-11T09:33:57.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-20T02:38:18.000Z (over 5 years ago)
- Last Synced: 2025-03-04T08:45:47.111Z (about 1 year ago)
- Topics: lua, openresty, redis
- Language: Lua
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```lua
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
```lua
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