pax_global_header00006660000000000000000000000064120502534550014513gustar00rootroot0000000000000052 comment=6d062bf1e11d3a9f2d2be2b1f2cda4dce644fbd5 lua-resty-redis-master/000077500000000000000000000000001205025345500154015ustar00rootroot00000000000000lua-resty-redis-master/.gitignore000066400000000000000000000000601205025345500173650ustar00rootroot00000000000000*.swp *.swo *~ go t/servroot/ reindex *.t_ tags lua-resty-redis-master/Makefile000066400000000000000000000006511205025345500170430ustar00rootroot00000000000000OPENRESTY_PREFIX=/usr/local/openresty-debug PREFIX ?= /usr/local LUA_INCLUDE_DIR ?= $(PREFIX)/include LUA_LIB_DIR ?= $(PREFIX)/lib/lua/$(LUA_VERSION) INSTALL ?= install .PHONY: all test install all: ; install: all $(INSTALL) -d $(DESTDIR)/$(LUA_LIB_DIR)/resty $(INSTALL) lib/resty/*.lua $(DESTDIR)/$(LUA_LIB_DIR)/resty test: all PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t lua-resty-redis-master/README.markdown000066400000000000000000000401351205025345500201050ustar00rootroot00000000000000Name ==== lua-resty-redis - Lua redis client driver for the ngx_lua based on the cosocket API Status ====== This library is considered production ready. Description =========== This Lua library is a Redis client driver for the ngx_lua nginx module: http://wiki.nginx.org/HttpLuaModule 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](https://github.com/chaoslawful/lua-nginx-module/tags) or [ngx_openresty 1.2.1.14](http://openresty.org/#Download) is required. Synopsis ======== lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;"; server { location /test { content_by_lua ' local redis = require "resty.redis" local red = redis:new() red:set_timeout(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") local ok, err = red:connect("127.0.0.1", 6379) if not ok then ngx.say("failed to connect: ", err) return end ok, err = red:set("dog", "an aniaml") 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 not res[1] 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 0 idle timeout local ok, err = red:set_keepalive(0, 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 '; } } Methods ======= All 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: http://redis.io/commands 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". All these command methods returns a single result in success and `nil` otherwise. In case of errors or failures, it will also return a second value which is a string describing the error. 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 `false` value *and* a string describing the error. A non-nil Redis "bulk reply" results in a Lua string as the return value. A nil bulk reply results in a `ngx.null` return value. 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 `{false, err}`. A nil multi-bulk reply returns in a `ngx.null` value. 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 --- `syntax: red, err = redis:new()` Creates a redis object. In case of failures, returns `nil` and a string describing the error. connect ------- `syntax: ok, err = red:connect(host, port)` `syntax: ok, err = red:connect("unix:/path/to/unix.sock")` 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. set_timeout ---------- `syntax: red:set_timeout(time)` Sets the timeout (in ms) protection for subsequent operations, including the `connect` method. set_keepalive ------------ `syntax: ok, err = red:set_keepalive(max_idle_timeout, pool_size)` Puts the current Redis connection 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 `1`. In case of errors, returns `nil` with a string describing the error. Only call this method in the place you would have called the `close` method instead. Calling this method will immediately turn the current redis object into the `closed` state. Any subsequent operations on the current objet will return the `closed` error. get_reused_times ---------------- `syntax: times, err = red:get_reused_times()` This method returns the (successfully) reused times for the current connection. In case of error, it returns `nil` and a string describing the error. If the current connection does not come from the built-in connection pool, then this method always returns `0`, that is, the connection has never been reused (yet). If the connection comes from the connection pool, then the return value is always non-zero. So this method can also be used to determine if the current connection comes from the pool. close ----- `syntax: ok, err = red:close()` Closes the current redis connection and returns the status. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. init_pipeline ------------- `syntax: red: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 `commit_pipeline` method is called or get cancelled by calling the `cancel_pipeline` method. 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. commit_pipeline --------------- `syntax: results, err = red: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 `nil` and a Lua string describing the error upon failures. cancel_pipeline --------------- `syntax: red:cancel_pipeline()` Quits the pipelining mode by discarding all existing cached Redis commands since the last call to the `init_pipeline` method. This method always succeeds. If the redis object is not in the Redis pipelining mode, then this method is a no-op. hmset ----- `syntax: red:hmset(myhash, field1, value1, field2, value2, ...)` `syntax: red:hmset(myhash, { field1 = value1, field2 = value2, ... })` 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 ------------- `syntax: hash = red:array_to_hash(array)` Auxiliary function that converts an array-like Lua table into a hash-like table. This method was first introduced in the `v0.11` release. read_reply ---------- `syntax: res, err = red:read_reply()` Reading a reply from the redis server. This method is mostly useful for the [Redis Pub/Sub API](http://redis.io/topics/pubsub/), for example, local cjson = require "cjson" local redis = require "resty.redis" local red = redis:new() local red2 = redis:new() red:set_timeout(1000) -- 1 sec red2:set_timeout(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 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: 1: subscribe: ["subscribe","dog",1] 2: publish: 1 1: receive: ["message","dog","Hello"] The following class methods are provieded: add_commands ------------ `syntax: hash = redis.add_commands(cmd_name1, cmd_name2, ...)` Adds new redis commands to the `resty.redis` class. Here is an example: local redis = require "resty.redis" redis.add_commands("foo", "bar") local red = redis:new() red:set_timeout(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 Transactions ================== This library supports the [Redis transactions](http://redis.io/topics/transactions/). Here is an example: local cjson = require "cjson" local redis = require "resty.redis" local red = redis:new() red:set_timeout(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 multi ans: "OK" set ans: "QUEUED" set ans: "QUEUED" exec ans: ["OK",[false,"ERR Operation against a key holding the wrong kind of value"]] Debugging ========= It is usually convenient to use the [lua-cjson](http://www.kyne.com.au/~mark/software/lua-cjson.php) library to encode the return values of the redis command methods to JSON. For example, local cjson = require "cjson" ... local res, err = red:mget("h1234", "h5678") if res then print("res: ", cjson.encode(res)) end Limitations =========== * This library cannot be used in code contexts like set_by_lua*, log_by_lua*, and header_filter_by_lua* where the ngx_lua cosocket API is not available. * The `resty.redis` object instance cannot be stored in a Lua variable at the Lua module level, because it will then be shared by all the concurrent requests handled by the same nginx worker process (see http://wiki.nginx.org/HttpLuaModule#Data_Sharing_within_an_Nginx_Worker ) and result in bad race conditions when concurrent requests are trying to use the same `resty.redis` instance. You should always initiate `resty.redis` objects in function local variables or in the `ngx.ctx` table. These places all have their own data copies for each request. Installation ============ If you are using the ngx_openresty bundle (http://openresty.org ), then you don't need to do anything because it already includes and enables lua-resty-redis by default. And you can just use it in your Lua code, as in local redis = require "resty.redis" ... If you're using your own nginx + ngx_lua build, then you need to configure the lua_package_path directive to add the path of your lua-resty-redis source tree to ngx_lua's LUA_PATH search path, as in # nginx.conf http { lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;"; ... } TODO ==== Community ========= English Mailing List -------------------- The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers. Chinese Mailing List -------------------- The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers. Bugs and Patches ================ Please report bugs or submit patches by 1. creating a ticket on the [GitHub Issue Tracker](http://github.com/agentzh/lua-resty-redis/issues), 1. or posting to the [OpenResty community](http://wiki.nginx.org/HttpLuaModule#Community). Author ====== Yichun "agentzh" Zhang (章亦春) Copyright and License ===================== This module is licensed under the BSD license. Copyright (C) 2012, by Yichun "agentzh" Zhang (章亦春) . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. See Also ======== * the ngx_lua module: http://wiki.nginx.org/HttpLuaModule * the redis wired protocol specification: http://redis.io/topics/protocol * the [lua-resty-memcached](https://github.com/agentzh/lua-resty-memcached) library * the [lua-resty-mysql](https://github.com/agentzh/lua-resty-mysql) library lua-resty-redis-master/lib/000077500000000000000000000000001205025345500161475ustar00rootroot00000000000000lua-resty-redis-master/lib/resty/000077500000000000000000000000001205025345500173155ustar00rootroot00000000000000lua-resty-redis-master/lib/resty/redis.lua000066400000000000000000000202371205025345500211320ustar00rootroot00000000000000-- Copyright (C) 2012 Yichun Zhang (agentzh) local sub = string.sub local tcp = ngx.socket.tcp local insert = table.insert local concat = table.concat local len = string.len local null = ngx.null local pairs = pairs local unpack = unpack local setmetatable = setmetatable local tonumber = tonumber local error = error module(...) _VERSION = '0.15' local commands = { "append", "auth", "bgrewriteaof", "bgsave", "blpop", "brpop", "brpoplpush", "config", "dbsize", "debug", "decr", "decrby", "del", "discard", "echo", "eval", "exec", "exists", "expire", "expireat", "flushall", "flushdb", "get", "getbit", "getrange", "getset", "hdel", "hexists", "hget", "hgetall", "hincrby", "hkeys", "hlen", "hmget", --[[ "hmset", ]] "hset", "hsetnx", "hvals", "incr", "incrby", "info", "keys", "lastsave", "lindex", "linsert", "llen", "lpop", "lpush", "lpushx", "lrange", "lrem", "lset", "ltrim", "mget", "monitor", "move", "mset", "msetnx", "multi", "object", "persist", "ping", "psubscribe", "publish", "punsubscribe", "quit", "randomkey", "rename", "renamenx", "rpop", "rpoplpush", "rpush", "rpushx", "sadd", "save", "scard", "script", "sdiff", "sdiffstore", "select", "set", "setbit", "setex", "setnx", "setrange", "shutdown", "sinter", "sinterstore", "sismember", "slaveof", "slowlog", "smembers", "smove", "sort", "spop", "srandmember", "srem", "strlen", "subscribe", "sunion", "sunionstore", "sync", "ttl", "type", "unsubscribe", "unwatch", "watch", "zadd", "zcard", "zcount", "zincrby", "zinterstore", "zrange", "zrangebyscore", "zrank", "zrem", "zremrangebyrank", "zremrangebyscore", "zrevrange", "zrevrangebyscore", "zrevrank", "zscore", "zunionstore", "evalsha" } local mt = { __index = _M } function new(self) local sock, err = tcp() if not sock then return nil, err end return setmetatable({ sock = sock }, mt) end function set_timeout(self, timeout) local sock = self.sock if not sock then return nil, "not initialized" end return sock:settimeout(timeout) end function connect(self, ...) local sock = self.sock if not sock then return nil, "not initialized" end return sock:connect(...) end function set_keepalive(self, ...) local sock = self.sock if not sock then return nil, "not initialized" end return sock:setkeepalive(...) end function get_reused_times(self) local sock = self.sock if not sock then return nil, "not initialized" end return sock:getreusedtimes() end function close(self) local sock = self.sock if not sock then return nil, "not initialized" end return sock:close() end local function _read_reply(sock) local line, err = sock:receive() if not line then return nil, err end local prefix = sub(line, 1, 1) if prefix == "$" then -- print("bulk reply") local size = tonumber(sub(line, 2)) if size < 0 then return null end local data, err = sock:receive(size) if not data then return nil, err end local dummy, err = sock:receive(2) -- ignore CRLF if not dummy then return nil, err end return data elseif prefix == "+" then -- print("status reply") return sub(line, 2) elseif prefix == "*" then local n = tonumber(sub(line, 2)) -- print("multi-bulk reply: ", n) if n < 0 then return null end local vals = {}; for i = 1, n do local res, err = _read_reply(sock) if res then insert(vals, res) elseif res == nil then return nil, err else -- be a valid redis error value insert(vals, {false, err}) end end return vals elseif prefix == ":" then -- print("integer reply") return tonumber(sub(line, 2)) elseif prefix == "-" then -- print("error reply: ", n) return false, sub(line, 2) else return nil, "unkown prefix: \"" .. prefix .. "\"" end end local function _gen_req(args) local req = {"*", #args, "\r\n"} for i = 1, #args do local arg = args[i] if not arg then insert(req, "$-1\r\n") else insert(req, "$") insert(req, len(arg)) insert(req, "\r\n") insert(req, arg) insert(req, "\r\n") end end -- it is faster to do string concatenation on the Lua land return concat(req, "") end local function _do_cmd(self, ...) local args = {...} local sock = self.sock if not sock then return nil, "not initialized" end local req = _gen_req(args) local reqs = self._reqs if reqs then insert(reqs, req) return end -- print("request: ", table.concat(req, "")) local bytes, err = sock:send(req) if not bytes then return nil, err end return _read_reply(sock) end function read_reply(self) local sock = self.sock if not sock then return nil, "not initialized" end return _read_reply(sock) end for i = 1, #commands do local cmd = commands[i] _M[cmd] = function (self, ...) return _do_cmd(self, cmd, ...) end end function hmset(self, hashname, ...) local args = {...} if #args == 1 then local t = args[1] local array = {} for k, v in pairs(t) do insert(array, k) insert(array, v) end -- print("key", hashname) return _do_cmd(self, "hmset", hashname, unpack(array)) end -- backwards compatibility return _do_cmd(self, "hmset", hashname, ...) end function init_pipeline(self) self._reqs = {} end function cancel_pipeline(self) self._reqs = nil end function commit_pipeline(self) local reqs = self._reqs if not reqs then return nil, "no pipeline" end self._reqs = nil local sock = self.sock if not sock then return nil, "not initialized" end local bytes, err = sock:send(reqs) if not bytes then return nil, err end local vals = {} for i = 1, #reqs do local res, err = _read_reply(sock) if res then insert(vals, res) elseif res == nil then return nil, err else -- be a valid redis error value insert(vals, {false, err}) end end return vals end function array_to_hash(self, t) local h = {} for i = 1, #t, 2 do h[t[i]] = t[i + 1] end return h end local class_mt = { -- to prevent use of casual module global variables __newindex = function (table, key, val) error('attempt to write to undeclared variable "' .. key .. '"') end } function add_commands(...) local cmds = {...} local newindex = class_mt.__newindex class_mt.__newindex = nil for i = 1, #cmds do local cmd = cmds[i] _M[cmd] = function (self, ...) return _do_cmd(self, cmd, ...) end end class_mt.__newindex = newindex end setmetatable(_M, class_mt) lua-resty-redis-master/t/000077500000000000000000000000001205025345500156445ustar00rootroot00000000000000lua-resty-redis-master/t/bugs.t000066400000000000000000000133761205025345500170030ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); my $HtmlDir = html_dir; our $HttpConfig = qq{ lua_package_path "$HtmlDir/?.lua;$pwd/lib/?.lua;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); log_level 'warn'; run_tests(); __DATA__ === TEST 1: github issue #108: ngx.locaiton.capture + redis.set_keepalive --- http_config eval: $::HttpConfig --- config location /r1 { default_type text/html; set $port $TEST_NGINX_REDIS_PORT; #lua_code_cache off; lua_need_request_body on; content_by_lua_file html/r1.lua; } location /r2 { default_type text/html; set $port $TEST_NGINX_REDIS_PORT; #lua_code_cache off; lua_need_request_body on; content_by_lua_file html/r2.lua; } location /anyurl { internal; proxy_pass http://127.0.0.1:$server_port/dummy; } location = /dummy { echo dummy; } --- user_files >>> r1.lua local redis = require "resty.redis" local red = redis:new() local ok, err = red:connect("127.0.0.1", ngx.var.port) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = red:flushall() if not ok then ngx.say("failed to flushall: ", err) return end ok, err = red:set_keepalive() if not ok then ngx.say("failed to set keepalive: ", err) return end local http_ress = ngx.location.capture("/r2") -- 1 ngx.say("ok") >>> r2.lua local redis = require "resty.redis" local red = redis:new() local ok, err = red:connect("127.0.0.1", ngx.var.port) --2 if not ok then ngx.say("failed to connect: ", err) return end local res = ngx.location.capture("/anyurl") --3 --- request GET /r1 --- response_body ok --- no_error_log [error] === TEST 2: exit(404) after I/O (ngx_lua github issue #110 https://github.com/chaoslawful/lua-nginx-module/issues/110 --- http_config eval: $::HttpConfig --- config error_page 400 /400.html; error_page 404 /404.html; location /foo { access_by_lua ' local redis = require "resty.redis" local red = redis:new() red:set_timeout(2000) -- 2 sec -- ngx.log(ngx.ERR, "hello"); -- or connect to a unix domain socket file listened -- by a redis server: -- local ok, err = red:connect("unix:/path/to/redis.sock") local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT) if not ok then ngx.log(ngx.ERR, "failed to connect: ", err) return end res, err = red:set("dog", "an animal") if not res then ngx.log(ngx.ERR, "failed to set dog: ", err) return end -- ngx.say("set dog: ", res) local res, err = red:get("dog") if err then ngx.log(ngx.ERR, "failed to get dog: ", err) return end if not res then ngx.log(ngx.ERR, "dog not found.") return end -- ngx.say("dog: ", res) -- red:close() local ok, err = red:set_keepalive(0, 100) if not ok then ngx.log(ngx.ERR, "failed to set keepalive: ", err) return end ngx.exit(404) '; echo Hello; } --- user_files >>> 400.html Bad request, dear... >>> 404.html Not found, dear... --- request GET /foo --- response_body Not found, dear... --- error_code: 404 --- no_error_log [error] === TEST 3: set and get an empty string --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" local red = redis:new() red:set_timeout(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") 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 res, err = red:set("dog", "") if not res then ngx.say("failed to set dog: ", err) return end ngx.say("set dog: ", res) for i = 1, 2 do local res, err = red:get("dog") if err then ngx.say("failed to get dog: ", err) return end if not res then ngx.say("dog not found.") return end ngx.say("dog: ", res) end red:close() '; } --- request GET /t --- response_body set dog: OK dog: dog: --- no_error_log [error] === TEST 4: ngx.exec() after red:get() --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:get("dog") if err then ngx.say("failed to get dog: ", err) return end ngx.exec("/hello") '; } location = /hello { echo hello world; } --- request GET /t --- response_body hello world --- no_error_log [error] lua-resty-redis-master/t/hmset.t000066400000000000000000000072101205025345500171510ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: hmset key-pairs --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:hmset("animals", "dog", "bark", "cat", "meow") if not res then ngx.say("failed to set animals: ", err) return end ngx.say("hmset animals: ", res) local res, err = red:hmget("animals", "dog", "cat") if not res then ngx.say("failed to get animals: ", err) return end ngx.say("hmget animals: ", res) red:close() '; } --- request GET /t --- response_body hmset animals: OK hmget animals: barkmeow --- no_error_log [error] === TEST 2: hmset lua tables --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local t = { dog = "bark", cat = "meow", cow = "moo" } local res, err = red:hmset("animals", t) if not res then ngx.say("failed to set animals: ", err) return end ngx.say("hmset animals: ", res) local res, err = red:hmget("animals", "dog", "cat", "cow") if not res then ngx.say("failed to get animals: ", err) return end ngx.say("hmget animals: ", res) red:close() '; } --- request GET /t --- response_body hmset animals: OK hmget animals: barkmeowmoo --- no_error_log [error] === TEST 3: hmset a single scalar --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:hmset("animals", "cat") if not res then ngx.say("failed to set animals: ", err) return end ngx.say("hmset animals: ", res) local res, err = red:hmget("animals", "cat") if not res then ngx.say("failed to get animals: ", err) return end ngx.say("hmget animals: ", res) red:close() '; } --- request GET /t --- response_body_like: 500 Internal Server Error --- error_code: 500 --- error_log table expected, got string lua-resty-redis-master/t/pipeline.t000066400000000000000000000142431205025345500176420ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: basic --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 for i = 1, 2 do red:init_pipeline() red:set("dog", "an animal") red:get("dog") red:set("dog", "hello") red:get("dog") local results = red:commit_pipeline() local cjson = require "cjson" ngx.say(cjson.encode(results)) end red:close() '; } --- request GET /t --- response_body ["OK","an animal","OK","hello"] ["OK","an animal","OK","hello"] --- no_error_log [error] === TEST 2: cancel automatically --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 red:init_pipeline() red:set("dog", "an animal") red:get("dog") for i = 1, 2 do red:init_pipeline() red:set("dog", "an animal") red:get("dog") red:set("dog", "hello") red:get("dog") local results = red:commit_pipeline() local cjson = require "cjson" ngx.say(cjson.encode(results)) end red:close() '; } --- request GET /t --- response_body ["OK","an animal","OK","hello"] ["OK","an animal","OK","hello"] --- no_error_log [error] === TEST 3: cancel explicitly --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 red:init_pipeline() red:set("dog", "an animal") red:get("dog") red:cancel_pipeline() local res, err = red:flushall() if not res then ngx.say("failed to flush all: ", err) return end ngx.say("flushall: ", res) for i = 1, 2 do red:init_pipeline() red:set("dog", "an animal") red:get("dog") red:set("dog", "hello") red:get("dog") local results = red:commit_pipeline() local cjson = require "cjson" ngx.say(cjson.encode(results)) end red:close() '; } --- request GET /t --- response_body flushall: OK ["OK","an animal","OK","hello"] ["OK","an animal","OK","hello"] --- no_error_log [error] === TEST 4: mixed --- http_config eval: $::HttpConfig --- config location /test { content_by_lua ' local redis = require "resty.redis" local red = redis:new() red:set_timeout(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 res, err = red:set("dog", "an aniaml") if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("set result: ", res) 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 not res[1] then ngx.say("failed to run command ", i, ": ", res[2]) else ngx.say("cmd ", i, ": ", res) end else -- process the scalar value ngx.say("cmd ", i, ": ", res) end end -- put it into the connection pool of size 100, -- with 0 idle timeout local ok, err = red:set_keepalive(0, 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 '; } --- request GET /test --- response_body set result: OK dog: an aniaml cmd 1: OK cmd 2: OK cmd 3: Marry cmd 4: Bob --- no_error_log [error] lua-resty-redis-master/t/pubsub.t000066400000000000000000000041011205025345500173250ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: single channel --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local cjson = require "cjson" local redis = require "resty.redis" local red = redis:new() local red2 = redis:new() red:set_timeout(1000) -- 1 sec red2:set_timeout(1000) -- 1 sec local ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT) if not ok then ngx.say("1: failed to connect: ", err) return end ok, err = red2:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT) if not ok then ngx.say("2: failed to connect: ", err) return end 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() '; } --- request GET /t --- response_body 1: subscribe: ["subscribe","dog",1] 2: publish: 1 1: receive: ["message","dog","Hello"] --- no_error_log [error] lua-resty-redis-master/t/sanity.t000066400000000000000000000400511205025345500173400ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: set and get --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 res, err = red:set("dog", "an animal") if not res then ngx.say("failed to set dog: ", err) return end ngx.say("set dog: ", res) for i = 1, 2 do local res, err = red:get("dog") if err then ngx.say("failed to get dog: ", err) return end if not res then ngx.say("dog not found.") return end ngx.say("dog: ", res) end red:close() '; } --- request GET /t --- response_body set dog: OK dog: an animal dog: an animal --- no_error_log [error] === TEST 2: flushall --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:flushall() if not res then ngx.say("failed to flushall: ", err) return end ngx.say("flushall: ", res) red:close() '; } --- request GET /t --- response_body flushall: OK --- no_error_log [error] === TEST 3: get nil bulk value --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:flushall() if not res then ngx.say("failed to flushall: ", err) return end ngx.say("flushall: ", res) res, err = red:get("not_found") if err then ngx.say("failed to get: ", err) return end if res == ngx.null then ngx.say("not_found not found.") return end ngx.say("get not_found: ", res) red:close() '; } --- request GET /t --- response_body flushall: OK not_found not found. --- no_error_log [error] === TEST 4: get nil list --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:flushall() if not res then ngx.say("failed to flushall: ", err) return end ngx.say("flushall: ", res) res, err = red:lrange("nokey", 0, 1) if err then ngx.say("failed to get: ", err) return end if res == ngx.null then ngx.say("nokey not found.") return end ngx.say("get nokey: ", #res, " (", type(res), ")") red:close() '; } --- request GET /t --- response_body flushall: OK get nokey: 0 (table) --- no_error_log [error] === TEST 5: incr and decr --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 res, err = red:set("connections", 10) if not res then ngx.say("failed to set connections: ", err) return end ngx.say("set connections: ", res) res, err = red:incr("connections") if not res then ngx.say("failed to set connections: ", err) return end ngx.say("incr connections: ", res) local res, err = red:get("connections") if err then ngx.say("failed to get connections: ", err) return end res, err = red:incr("connections") if not res then ngx.say("failed to incr connections: ", err) return end ngx.say("incr connections: ", res) res, err = red:decr("connections") if not res then ngx.say("failed to decr connections: ", err) return end ngx.say("decr connections: ", res) res, err = red:get("connections") if not res then ngx.say("connections not found.") return end ngx.say("connections: ", res) res, err = red:del("connections") if not res then ngx.say("failed to del connections: ", err) return end ngx.say("del connections: ", res) res, err = red:incr("connections") if not res then ngx.say("failed to set connections: ", err) return end ngx.say("incr connections: ", res) res, err = red:get("connections") if not res then ngx.say("connections not found.") return end ngx.say("connections: ", res) red:close() '; } --- request GET /t --- response_body set connections: OK incr connections: 11 incr connections: 12 decr connections: 11 connections: 11 del connections: 1 incr connections: 1 connections: 1 --- no_error_log [error] === TEST 6: bad incr command format --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 res, err = red:incr("connections", 12) if not res then ngx.say("failed to set connections: ", res, ": ", err) return end ngx.say("incr connections: ", res) red:close() '; } --- request GET /t --- response_body failed to set connections: false: ERR wrong number of arguments for 'incr' command --- no_error_log [error] === TEST 7: lpush and lrange --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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 local res, err = red:flushall() if not res then ngx.say("failed to flushall: ", err) return end ngx.say("flushall: ", res) local res, err = red:lpush("mylist", "world") if not res then ngx.say("failed to lpush: ", err) return end ngx.say("lpush result: ", res) res, err = red:lpush("mylist", "hello") if not res then ngx.say("failed to lpush: ", err) return end ngx.say("lpush result: ", res) res, err = red:lrange("mylist", 0, -1) if not res then ngx.say("failed to lrange: ", err) return end local cjson = require "cjson" ngx.say("lrange result: ", cjson.encode(res)) red:close() '; } --- request GET /t --- response_body flushall: OK lpush result: 1 lpush result: 2 lrange result: ["hello","world"] --- no_error_log [error] === TEST 8: blpop expires its own timeout --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" local red = redis:new() red:set_timeout(2500) -- 2.5 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 local res, err = red:flushall() if not res then ngx.say("failed to flushall: ", err) return end ngx.say("flushall: ", res) local res, err = red:blpop("key", 1) if err then ngx.say("failed to blpop: ", err) return end if res == ngx.null then ngx.say("no element popped.") return end local cjson = require "cjson" ngx.say("blpop result: ", cjson.encode(res)) red:close() '; } --- request GET /t --- response_body flushall: OK no element popped. --- no_error_log [error] --- timeout: 3 === TEST 9: blpop expires cosocket timeout --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" local red = redis:new() 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 local res, err = red:flushall() if not res then ngx.say("failed to flushall: ", err) return end ngx.say("flushall: ", res) red:set_timeout(200) -- 200 ms local res, err = red:blpop("key", 1) if err then ngx.say("failed to blpop: ", err) return end if not res then ngx.say("no element popped.") return end local cjson = require "cjson" ngx.say("blpop result: ", cjson.encode(res)) red:close() '; } --- request GET /t --- response_body flushall: OK failed to blpop: timeout --- error_log lua tcp socket read timed out === TEST 10: set keepalive and get reused times --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local redis = require "resty.redis" 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 local times = red:get_reused_times() ngx.say("reused times: ", times) local ok, err = red:set_keepalive() if not ok then ngx.say("failed to set keepalive: ", err) return end ok, err = red:connect("127.0.0.1", $TEST_NGINX_REDIS_PORT) if not ok then ngx.say("failed to connect: ", err) return end times = red:get_reused_times() ngx.say("reused times: ", times) '; } --- request GET /t --- response_body reused times: 0 reused times: 1 --- no_error_log [error] === TEST 11: mget --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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:flushall() if not ok then ngx.say("failed to flush all: ", err) return end res, err = red:set("dog", "an animal") if not res then ngx.say("failed to set dog: ", err) return end ngx.say("set dog: ", res) for i = 1, 2 do local res, err = red:mget("dog", "cat", "dog") if err then ngx.say("failed to get dog: ", err) return end if not res then ngx.say("dog not found.") return end local cjson = require "cjson" ngx.say("res: ", cjson.encode(res)) end red:close() '; } --- request GET /t --- response_body set dog: OK res: ["an animal",null,"an animal"] res: ["an animal",null,"an animal"] --- no_error_log [error] === TEST 12: hmget array_to_hash --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" 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:flushall() if not ok then ngx.say("failed to flush all: ", err) return end res, err = red:hmset("animals", { dog = "bark", cat = "meow", cow = "moo" }) if not res then ngx.say("failed to set animals: ", err) return end ngx.say("hmset animals: ", res) local res, err = red:hmget("animals", "dog", "cat", "cow") if not res then ngx.say("failed to get animals: ", err) return end ngx.say("hmget animals: ", res) local res, err = red:hgetall("animals") if err then ngx.say("failed to get animals: ", err) return end if not res then ngx.say("animals not found.") return end local h = red:array_to_hash(res) ngx.say("dog: ", h.dog) ngx.say("cat: ", h.cat) ngx.say("cow: ", h.cow) red:close() '; } --- request GET /t --- response_body hmset animals: OK hmget animals: barkmeowmoo dog: bark cat: meow cow: moo --- no_error_log [error] lua-resty-redis-master/t/transaction.t000066400000000000000000000064641205025345500203700ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: sanity --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local cjson = require "cjson" local redis = require "resty.redis" 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 local redis_key = "foo" 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:sort("log", "by", redis_key .. ":*->timestamp") if not ans then ngx.say("failed to run sort: ", err) return end ngx.say("sort ans: ", cjson.encode(ans)) ans, err = red:exec() ngx.say("exec ans: ", cjson.encode(ans)) local ok, err = red:set_keepalive(0, 1024) if not ok then ngx.say("failed to put the current redis connection into pool: ", err) return end '; } --- request GET /t --- response_body multi ans: "OK" sort ans: "QUEUED" exec ans: [{}] --- no_error_log [error] === TEST 2: redis cmd reference sample: redis does not halt on errors --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local cjson = require "cjson" local redis = require "resty.redis" 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 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() '; } --- request GET /t --- response_body multi ans: "OK" set ans: "QUEUED" set ans: "QUEUED" exec ans: ["OK",[false,"ERR Operation against a key holding the wrong kind of value"]] --- no_error_log [error] lua-resty-redis-master/t/user-cmds.t000066400000000000000000000025761205025345500177450ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; lua_package_cpath "/usr/local/openresty-debug/lualib/?.so;/usr/local/openresty/lualib/?.so;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_REDIS_PORT} ||= 6379; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: single channel --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local cjson = require "cjson" local redis = require "resty.redis" redis.add_commands("foo", "bar") 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 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 '; } --- request GET /t --- response_body failed to foo: ERR unknown command 'foo' failed to bar: ERR unknown command 'bar' --- no_error_log [error] lua-resty-redis-master/t/version.t000066400000000000000000000011451205025345500175170ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; no_long_string(); #no_diff(); run_tests(); __DATA__ === TEST 1: basic --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local redis = require "resty.redis" ngx.say(redis._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] lua-resty-redis-master/valgrind.suppress000066400000000000000000000232551205025345500210240ustar00rootroot00000000000000{ Memcheck:Cond fun:lj_str_new } { Memcheck:Param write(buf) fun:__write_nocancel fun:ngx_log_error_core fun:ngx_resolver_read_response } { Memcheck:Cond fun:ngx_sprintf_num fun:ngx_vslprintf fun:ngx_log_error_core fun:ngx_resolver_read_response fun:ngx_epoll_process_events fun:ngx_process_events_and_timers fun:ngx_single_process_cycle fun:main } { Memcheck:Addr1 fun:ngx_vslprintf fun:ngx_snprintf fun:ngx_sock_ntop fun:ngx_event_accept } { Memcheck:Param write(buf) fun:__write_nocancel fun:ngx_log_error_core fun:ngx_resolver_read_response fun:ngx_event_process_posted fun:ngx_process_events_and_timers fun:ngx_single_process_cycle fun:main } { Memcheck:Cond fun:ngx_sprintf_num fun:ngx_vslprintf fun:ngx_log_error_core fun:ngx_resolver_read_response fun:ngx_event_process_posted fun:ngx_process_events_and_timers fun:ngx_single_process_cycle fun:main } { exp-sgcheck:SorG fun:lj_str_new fun:lua_pushlstring } { Memcheck:Leak fun:malloc fun:ngx_alloc obj:* } { exp-sgcheck:SorG fun:lj_str_new fun:lua_pushlstring } { exp-sgcheck:SorG fun:ngx_http_lua_ndk_set_var_get } { exp-sgcheck:SorG fun:lj_str_new fun:lua_getfield } { exp-sgcheck:SorG fun:lj_str_new fun:lua_setfield } { exp-sgcheck:SorG fun:ngx_http_variables_init_vars fun:ngx_http_block } { exp-sgcheck:SorG fun:ngx_conf_parse } { exp-sgcheck:SorG fun:ngx_vslprintf fun:ngx_log_error_core } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_calloc fun:ngx_event_process_init } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_pcalloc } { Memcheck:Addr4 fun:lj_str_new fun:lua_setfield } { Memcheck:Addr4 fun:lj_str_new fun:lua_getfield } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:(below main) } { Memcheck:Param epoll_ctl(event) fun:epoll_ctl } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init } { Memcheck:Cond fun:ngx_conf_flush_files fun:ngx_single_process_cycle } { Memcheck:Cond fun:memcpy fun:ngx_vslprintf fun:ngx_log_error_core fun:ngx_http_charset_header_filter } { Memcheck:Leak fun:memalign fun:posix_memalign fun:ngx_memalign fun:ngx_pcalloc } { Memcheck:Addr4 fun:lj_str_new fun:lua_pushlstring } { Memcheck:Cond fun:lj_str_new fun:lj_str_fromnum } { Memcheck:Cond fun:lj_str_new fun:lua_pushlstring } { Memcheck:Addr4 fun:lj_str_new fun:lua_setfield fun:ngx_http_lua_cache_store_code } { Memcheck:Cond fun:lj_str_new fun:lua_getfield fun:ngx_http_lua_cache_load_code } { Memcheck:Cond fun:lj_str_new fun:lua_setfield fun:ngx_http_lua_cache_store_code } { Memcheck:Addr4 fun:lj_str_new fun:lua_getfield fun:ngx_http_lua_cache_load_code } { Memcheck:Param socketcall.setsockopt(optval) fun:setsockopt fun:drizzle_state_connect } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pool_cleanup_add } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pnalloc } { Memcheck:Cond fun:ngx_conf_flush_files fun:ngx_single_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pcalloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_palloc_large } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_create_pool } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_palloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_malloc fun:ngx_pnalloc } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_array_push fun:ngx_http_get_variable_index fun:ngx_http_memc_add_variable fun:ngx_http_memc_init fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init fun:ngx_single_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_crc32_table_init fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_event_process_init fun:ngx_worker_process_init fun:ngx_worker_process_cycle fun:ngx_spawn_process fun:ngx_start_worker_processes fun:ngx_master_process_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_hash_init fun:ngx_http_variables_init_vars fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_upstream_drizzle_create_srv_conf fun:ngx_http_upstream fun:ngx_conf_parse fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_hash_keys_array_init fun:ngx_http_variables_add_core_vars fun:ngx_http_core_preconfiguration fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_array_push fun:ngx_hash_add_key fun:ngx_http_add_variable fun:ngx_http_echo_add_variables fun:ngx_http_echo_handler_init fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_upstream_drizzle_create_srv_conf fun:ngx_http_core_server fun:ngx_conf_parse fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_upstream_drizzle_create_srv_conf fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_array_push fun:ngx_hash_add_key fun:ngx_http_variables_add_core_vars fun:ngx_http_core_preconfiguration fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_hash_init fun:ngx_http_upstream_init_main_conf fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_pcalloc fun:ngx_http_drizzle_keepalive_init fun:ngx_http_upstream_drizzle_init fun:ngx_http_upstream_init_main_conf fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_palloc_large fun:ngx_palloc fun:ngx_hash_init fun:ngx_http_variables_init_vars fun:ngx_http_block fun:ngx_conf_parse fun:ngx_init_cycle fun:main } { Memcheck:Leak fun:memalign fun:posix_memalign fun:ngx_memalign fun:ngx_create_pool } { Memcheck:Leak fun:memalign fun:posix_memalign fun:ngx_memalign fun:ngx_palloc_block fun:ngx_palloc }