在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):openresty/lua-resty-redis开源软件地址(OpenSource Url):https://github.com/openresty/lua-resty-redis开源编程语言(OpenSource Language):Lua 88.4%开源软件介绍(OpenSource Introduction):Namelua-resty-redis - Lua redis client driver for the ngx_lua based on the cosocket API Table of Contents
StatusThis library is considered production ready. DescriptionThis Lua library is a Redis client driver for the ngx_lua nginx module: https://github.com/openresty/lua-nginx-module/#readme This Lua library takes advantage of ngx_lua's cosocket API, which ensures 100% nonblocking behavior. Note that at least ngx_lua 0.5.14 or OpenResty 1.2.1.14 is required. Synopsis # you do not need the following line if you are using
# the OpenResty bundle:
lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";
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"
local red = redis:new()
red:set_timeouts(1000, 1000, 1000) -- 1 sec
-- or connect to a unix domain socket file listened
-- by a redis server:
-- local ok, err = red:connect("unix:/path/to/redis.sock")
-- connect via ip address directly
local ok, err = red:connect("127.0.0.1", 6379)
-- or connect via hostname, need to specify resolver just like above
local ok, err = red:connect("redis.openresty.com", 6379)
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)
red:init_pipeline()
red:set("cat", "Marry")
red:set("horse", "Bob")
red:get("cat")
red:get("horse")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit the pipelined requests: ", err)
return
end
for i, res in ipairs(results) do
if type(res) == "table" then
if res[1] == false then
ngx.say("failed to run command ", i, ": ", res[2])
else
-- process the table value
end
else
-- process the scalar value
end
end
-- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
-- or just close the connection right away:
-- local ok, err = red:close()
-- if not ok then
-- ngx.say("failed to close: ", err)
-- return
-- end
}
}
} MethodsAll of the Redis commands have their own methods with the same name except all in lower case. You can find the complete list of Redis commands here: You need to check out this Redis command reference to see what Redis command accepts what arguments. The Redis command arguments can be directly fed into the corresponding method call. For example, the "GET" redis command accepts a single key argument, then you can just call the "get" method like this: local res, err = red:get("key") Similarly, the "LRANGE" redis command accepts threee arguments, then you should call the "lrange" method like this: local res, err = red:lrange("nokey", 0, 1) For example, "SET", "GET", "LRANGE", and "BLPOP" commands correspond to the methods "set", "get", "lrange", and "blpop". Here are some more examples: -- HMGET myhash field1 field2 nofield
local res, err = red:hmget("myhash", "field1", "field2", "nofield") -- HMSET myhash field1 "Hello" field2 "World"
local res, err = red:hmset("myhash", "field1", "Hello", "field2", "World") All these command methods returns a single result in success and A Redis "status reply" results in a string typed return value with the "+" prefix stripped. A Redis "integer reply" results in a Lua number typed return value. A Redis "error reply" results in a A non-nil Redis "bulk reply" results in a Lua string as the return value. A nil bulk reply results in a A non-nil Redis "multi-bulk reply" results in a Lua table holding all the composing values (if any). If any of the composing value is a valid redis error value, then it will be a two element table A nil multi-bulk reply returns in a See http://redis.io/topics/protocol for details regarding various Redis reply types. In addition to all those redis command methods, the following methods are also provided: new
Creates a redis object. In case of failures, returns connect
Attempts to connect to the remote host and port that the redis server is listening to or a local unix domain socket file listened by the redis server. Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method. The optional
set_timeout
Sets the timeout (in ms) protection for subsequent operations, including the Since version set_timeouts
Respectively sets the connect, send, and read timeout thresholds (in ms), for subsequent socket operations. Setting timeout thresholds with this method offers more granularity than set_timeout. As such, it is preferred to use set_timeouts over set_timeout. This method was added in the set_keepalive
Puts the current Redis connection immediately into the ngx_lua cosocket connection pool. You can specify the max idle timeout (in ms) when the connection is in the pool and the maximal size of the pool every nginx worker process. In case of success, returns Only call this method in the place you would have called the get_reused_times
This method returns the (successfully) reused times for the current connection. In case of error, it returns If the current connection does not come from the built-in connection pool, then this method always returns close
Closes the current redis connection and returns the status. In case of success, returns init_pipeline
Enable the redis pipelining mode. All subsequent calls to Redis command methods will automatically get cached and will send to the server in one run when the This method always succeeds. If the redis object is already in the Redis pipelining mode, then calling this method will discard existing cached Redis queries. The optional commit_pipeline
Quits the pipelining mode by committing all the cached Redis queries to the remote server in a single run. All the replies for these queries will be collected automatically and are returned as if a big multi-bulk reply at the highest level. This method returns cancel_pipeline
Quits the pipelining mode by discarding all existing cached Redis commands since the last call to the This method always succeeds. If the redis object is not in the Redis pipelining mode, then this method is a no-op. hmset
Special wrapper for the Redis "hmset" command. When there are only three arguments (including the "red" object itself), then the last argument must be a Lua table holding all the field/value pairs. array_to_hash
Auxiliary function that converts an array-like Lua table into a hash-like table. This method was first introduced in the read_reply
Reading a reply from the redis server. This method is mostly useful for the Redis Pub/Sub API, for example, local cjson = require "cjson"
local redis = require "resty.redis"
local red = redis:new()
local red2 = redis:new()
red:set_timeouts(1000, 1000, 1000) -- 1 sec
red2:set_timeouts(1000, 1000, 1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("1: failed to connect: ", err)
return
end
ok, err = red2:connect("127.0.0.1", 6379)
if not ok then
ngx.say("2: failed to connect: ", err)
return
end
local res, err = red:subscribe("dog")
if not res then
ngx.say("1: failed to subscribe: ", err)
return
end
ngx.say("1: subscribe: ", cjson.encode(res))
res, err = red2:publish("dog", "Hello")
if not res then
ngx.say("2: failed to publish: ", err)
return
end
ngx.say("2: publish: ", cjson.encode(res))
res, err = red:read_reply()
if not res then
ngx.say("1: failed to read reply: ", err)
return
end
ngx.say("1: receive: ", cjson.encode(res))
red:close()
red2:close() Running this example gives the output like this:
The following class methods are provieded: add_commands
WARNING this method is now deprecated since we already do automatic Lua method generation for any redis commands the user attempts to use and thus we no longer need this. Adds new redis commands to the local redis = require "resty.redis"
redis.add_commands("foo", "bar")
local red = redis:new()
red:set_timeouts(1000, 1000, 1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, err = red:foo("a")
if not res then
ngx.say("failed to foo: ", err)
end
res, err = red:bar()
if not res then
ngx.say("failed to bar: ", err)
end Redis AuthenticationRedis uses the There is nothing special for this command as compared to other Redis
commands like local redis = require "resty.redis"
local red = redis:new()
red:set_timeouts(1000, 1000, 1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, err = red:auth("foobared")
if not res then
ngx.say("failed to authenticate: ", err)
return
end where we assume that the Redis server is configured with the
password
If the password specified is wrong, then the sample above will output the following to the HTTP client:
Redis TransactionsThis library supports the Redis transactions. Here is an example: local cjson = require "cjson"
local redis = require "resty.redis"
local red = redis:new()
red:set_timeouts(1000, 1000, 1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local ok, err = red:multi()
if not ok then
ngx.say("failed to run multi: ", err)
return
end
ngx.say("multi ans: ", cjson.encode(ok))
local ans, err = red:set("a", "abc")
if not ans then
ngx.say("failed to run sort: ", err)
return
end
ngx.say("set ans: ", cjson.encode(ans))
local ans, err = red:lpop("a")
if not ans then
ngx.say("failed to run sort: ", err)
return
end
ngx.say("set ans: ", cjson.encode(ans))
ans, err = red:exec()
ngx.say("exec ans: ", cjson.encode(ans))
red:close() Then the output will be
Redis ModuleThis library supports the Redis module. Here is an example with RedisBloom module: local cjson = require "cjson"
local redis = require
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论