pax_global_header00006660000000000000000000000064126522351200014510gustar00rootroot0000000000000052 comment=2564138c76e3307711e8d0eb14e8f07cba318097 lua-resty-memcached-0.14/000077500000000000000000000000001265223512000152655ustar00rootroot00000000000000lua-resty-memcached-0.14/.gitattributes000066400000000000000000000000331265223512000201540ustar00rootroot00000000000000*.t linguist-language=Text lua-resty-memcached-0.14/.gitignore000066400000000000000000000000531265223512000172530ustar00rootroot00000000000000*.swp *.swo *~ go t/servroot/ reindex *.t_ lua-resty-memcached-0.14/Makefile000066400000000000000000000006511265223512000167270ustar00rootroot00000000000000OPENRESTY_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-memcached-0.14/README.markdown000066400000000000000000000443271265223512000200000ustar00rootroot00000000000000Name ==== lua-resty-memcached - Lua memcached client driver for the ngx_lua based on the cosocket API Table of Contents ================= * [Name](#name) * [Status](#status) * [Description](#description) * [Synopsis](#synopsis) * [Methods](#methods) * [new](#new) * [connect](#connect) * [set](#set) * [set_timeout](#set_timeout) * [set_keepalive](#set_keepalive) * [get_reused_times](#get_reused_times) * [close](#close) * [add](#add) * [replace](#replace) * [append](#append) * [prepend](#prepend) * [get](#get) * [gets](#gets) * [cas](#cas) * [touch](#touch) * [flush_all](#flush_all) * [delete](#delete) * [incr](#incr) * [decr](#decr) * [stats](#stats) * [version](#version) * [quit](#quit) * [verbosity](#verbosity) * [Automatic Error Logging](#automatic-error-logging) * [Limitations](#limitations) * [TODO](#todo) * [Author](#author) * [Copyright and License](#copyright-and-license) * [See Also](#see-also) Status ====== This library is considered production ready. Description =========== This Lua library is a memcached 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.0rc29](https://github.com/chaoslawful/lua-nginx-module/tags) or [OpenResty 1.0.15.7](http://openresty.org/#Download) is required. Synopsis ======== ```lua lua_package_path "/path/to/lua-resty-memcached/lib/?.lua;;"; server { location /test { content_by_lua ' local memcached = require "resty.memcached" local memc, err = memcached:new() if not memc then ngx.say("failed to instantiate memc: ", err) return end memc:set_timeout(1000) -- 1 sec -- or connect to a unix domain socket file listened -- by a memcached server: -- local ok, err = memc:connect("unix:/path/to/memc.sock") local ok, err = memc:connect("127.0.0.1", 11211) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local res, flags, err = memc: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) -- put it into the connection pool of size 100, -- with 10 seconds max idle timeout local ok, err = memc:set_keepalive(10000, 100) if not ok then ngx.say("cannot set keepalive: ", err) return end -- or just close the connection right away: -- local ok, err = memc:close() -- if not ok then -- ngx.say("failed to close: ", err) -- return -- end '; } } ``` [Back to TOC](#table-of-contents) Methods ======= The `key` argument provided in the following methods will be automatically escaped according to the URI escaping rules before sending to the memcached server. [Back to TOC](#table-of-contents) new --- `syntax: memc, err = memcached:new(opts?)` Creates a memcached object. In case of failures, returns `nil` and a string describing the error. It accepts an optional `opts` table argument. The following options are supported: * `key_transform` an array table containing two functions for escaping and unescaping the memcached keys, respectively. By default, the memcached keys will be escaped and unescaped as URI components, that is ```lua memached:new{ key_transform = { ngx.escape_uri, ngx.unescape_uri } } ``` [Back to TOC](#table-of-contents) connect ------- `syntax: ok, err = memc:connect(host, port)` `syntax: ok, err = memc:connect("unix:/path/to/unix.sock")` Attempts to connect to the remote host and port that the memcached server is listening to or a local unix domain socket file listened by the memcached 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. [Back to TOC](#table-of-contents) set --- `syntax: ok, err = memc:set(key, value, exptime, flags)` Inserts an entry into memcached unconditionally. If the key already exists, overrides it. The `value` argument could also be a Lua table holding multiple Lua strings that are supposed to be concatenated as a whole (without any delimiters). For example, ```lua memc:set("dog", {"a ", {"kind of"}, " animal"}) ``` is functionally equivalent to ```lua memc:set("dog", "a kind of animal") ``` The `exptime` parameter is optional, defaults to `0`. The `flags` parameter is optional, defaults to `0`. [Back to TOC](#table-of-contents) set_timeout ---------- `syntax: ok, err = memc:set_timeout(time)` Sets the timeout (in ms) protection for subsequent operations, including the `connect` method. Returns 1 when successful and nil plus a string describing the error otherwise. [Back to TOC](#table-of-contents) set_keepalive ------------ `syntax: ok, err = memc:set_keepalive(max_idle_timeout, pool_size)` Puts the current memcached 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 `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 memcached object into the `closed` state. Any subsequent operations other than `connect()` on the current objet will return the `closed` error. [Back to TOC](#table-of-contents) get_reused_times ---------------- `syntax: times, err = memc: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. [Back to TOC](#table-of-contents) close ----- `syntax: ok, err = memc:close()` Closes the current memcached connection and returns the status. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) add --- `syntax: ok, err = memc:add(key, value, exptime, flags)` Inserts an entry into memcached if and only if the key does not exist. The `value` argument could also be a Lua table holding multiple Lua strings that are supposed to be concatenated as a whole (without any delimiters). For example, ```lua memc:add("dog", {"a ", {"kind of"}, " animal"}) ``` is functionally equivalent to ```lua memc:add("dog", "a kind of animal") ``` The `exptime` parameter is optional, defaults to `0`. The `flags` parameter is optional, defaults to `0`. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) replace ------- `syntax: ok, err = memc:replace(key, value, exptime, flags)` Inserts an entry into memcached if and only if the key does exist. The `value` argument could also be a Lua table holding multiple Lua strings that are supposed to be concatenated as a whole (without any delimiters). For example, ```lua memc:replace("dog", {"a ", {"kind of"}, " animal"}) ``` is functionally equivalent to ```lua memc:replace("dog", "a kind of animal") ``` The `exptime` parameter is optional, defaults to `0`. The `flags` parameter is optional, defaults to `0`. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) append ------ `syntax: ok, err = memc:append(key, value, exptime, flags)` Appends the value to an entry with the same key that already exists in memcached. The `value` argument could also be a Lua table holding multiple Lua strings that are supposed to be concatenated as a whole (without any delimiters). For example, ```lua memc:append("dog", {"a ", {"kind of"}, " animal"}) ``` is functionally equivalent to ```lua memc:append("dog", "a kind of animal") ``` The `exptime` parameter is optional, defaults to `0`. The `flags` parameter is optional, defaults to `0`. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) prepend ------- `syntax: ok, err = memc:prepend(key, value, exptime, flags)` Prepends the value to an entry with the same key that already exists in memcached. The `value` argument could also be a Lua table holding multiple Lua strings that are supposed to be concatenated as a whole (without any delimiters). For example, ```lua memc:prepend("dog", {"a ", {"kind of"}, " animal"}) ``` is functionally equivalent to ```lua memc:prepend("dog", "a kind of animal") ``` The `exptime` parameter is optional, defaults to `0`. The `flags` parameter is optional, defaults to `0`. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) get --- `syntax: value, flags, err = memc:get(key)` `syntax: results, err = memc:get(keys)` Get a single entry or multiple entries in the memcached server via a single key or a talbe of keys. Let us first discuss the case When the key is a single string. The key's value and associated flags value will be returned if the entry is found and no error happens. In case of errors, `nil` values will be turned for `value` and `flags` and a 3rd (string) value will also be returned for describing the error. If the entry is not found, then three `nil` values will be returned. Then let us discuss the case when the a Lua table of multiple keys are provided. In this case, a Lua table holding the key-result pairs will be always returned in case of success. Each value corresponding each key in the table is also a table holding two values, the key's value and the key's flags. If a key does not exist, then there is no responding entries in the `results` table. In case of errors, `nil` will be returned, and the second return value will be a string describing the error. [Back to TOC](#table-of-contents) gets ---- `syntax: value, flags, cas_unique, err = memc:gets(key)` `syntax: results, err = memc:gets(keys)` Just like the `get` method, but will also return the CAS unique value associated with the entry in addition to the key's value and flags. This method is usually used together with the `cas` method. [Back to TOC](#table-of-contents) cas --- `syntax: ok, err = memc:cas(key, value, cas_unique, exptime?, flags?)` Just like the `set` method but does a check and set operation, which means "store this data but only if no one else has updated since I last fetched it." The `cas_unique` argument can be obtained from the `gets` method. [Back to TOC](#table-of-contents) touch --- `syntax: ok, err = memc:touch(key, exptime)` Update the expiration time of an existing key. Returns `1` for success or `nil` with a string describing the error otherwise. This method was first introduced in the `v0.11` release. [Back to TOC](#table-of-contents) flush_all --------- `syntax: ok, err = memc:flush_all(time?)` Flushes (or invalidates) all the existing entries in the memcached server immediately (by default) or after the expiration specified by the `time` argument (in seconds). In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) delete ------ `syntax: ok, err = memc:delete(key)` Deletes the key from memcached immediately. The key to be deleted must already exist in memcached. In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) incr ---- `syntax: new_value, err = memc:incr(key, delta)` Increments the value of the specified key by the integer value specified in the `delta` argument. Returns the new value after incrementation in success, and `nil` with a string describing the error in case of failures. [Back to TOC](#table-of-contents) decr ---- `syntax: new_value, err = memc:decr(key, value)` Decrements the value of the specified key by the integer value specified in the `delta` argument. Returns the new value after decrementation in success, and `nil` with a string describing the error in case of failures. [Back to TOC](#table-of-contents) stats ----- `syntax: lines, err = memc:stats(args?)` Returns memcached server statistics information with an optional `args` argument. In case of success, this method returns a lua table holding all of the lines of the output; in case of failures, it returns `nil` with a string describing the error. If the `args` argument is omitted, general server statistics is returned. Possible `args` argument values are `items`, `sizes`, `slabs`, among others. [Back to TOC](#table-of-contents) version ------- `syntax: version, err = memc:version(args?)` Returns the server version number, like `1.2.8`. In case of error, it returns `nil` with a string describing the error. [Back to TOC](#table-of-contents) quit ---- `syntax: ok, err = memc:quit()` Tells the server to close the current memcached connection. Returns `1` in case of success and `nil` other wise. In case of failures, another string value will also be returned to describe the error. Generally you can just directly call the `close` method to achieve the same effect. [Back to TOC](#table-of-contents) verbosity --------- `syntax: ok, err = memc:verbosity(level)` Sets the verbosity level used by the memcached server. The `level` argument should be given integers only. Returns `1` in case of success and `nil` other wise. In case of failures, another string value will also be returned to describe the error. [Back to TOC](#table-of-contents) Automatic Error Logging ======================= By default the underlying [ngx_lua](http://wiki.nginx.org/HttpLuaModule) module does error logging when socket errors happen. If you are already doing proper error handling in your own Lua code, then you are recommended to disable this automatic error logging by turning off [ngx_lua](http://wiki.nginx.org/HttpLuaModule)'s [lua_socket_log_errors](http://wiki.nginx.org/HttpLuaModule#lua_socket_log_errors) directive, that is, ```nginx lua_socket_log_errors off; ``` [Back to TOC](#table-of-contents) 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.memcached` 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.memcached` instance. You should always initiate `resty.memcached` objects in function local variables or in the `ngx.ctx` table. These places all have their own data copies for each request. [Back to TOC](#table-of-contents) TODO ==== * implement the memcached pipelining API. * implement the UDP part of the memcached ascii protocol. [Back to TOC](#table-of-contents) Author ====== Yichun "agentzh" Zhang (章亦春) , CloudFlare Inc. [Back to TOC](#table-of-contents) Copyright and License ===================== This module is licensed under the BSD license. Copyright (C) 2012-2016, by Yichun "agentzh" Zhang (章亦春) , CloudFlare Inc. 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. [Back to TOC](#table-of-contents) See Also ======== * the ngx_lua module: http://wiki.nginx.org/HttpLuaModule * the memcached wired protocol specification: http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt * the [lua-resty-redis](https://github.com/agentzh/lua-resty-redis) library. * the [lua-resty-mysql](https://github.com/agentzh/lua-resty-mysql) library. [Back to TOC](#table-of-contents) lua-resty-memcached-0.14/lib/000077500000000000000000000000001265223512000160335ustar00rootroot00000000000000lua-resty-memcached-0.14/lib/resty/000077500000000000000000000000001265223512000172015ustar00rootroot00000000000000lua-resty-memcached-0.14/lib/resty/memcached.lua000066400000000000000000000343201265223512000216140ustar00rootroot00000000000000-- Copyright (C) Yichun Zhang (agentzh), CloudFlare Inc. local sub = string.sub local escape_uri = ngx.escape_uri local unescape_uri = ngx.unescape_uri local match = string.match local tcp = ngx.socket.tcp local strlen = string.len local concat = table.concat local setmetatable = setmetatable local type = type local error = error local _M = { _VERSION = '0.13' } local mt = { __index = _M } function _M.new(self, opts) local sock, err = tcp() if not sock then return nil, err end local escape_key = escape_uri local unescape_key = unescape_uri if opts then local key_transform = opts.key_transform if key_transform then escape_key = key_transform[1] unescape_key = key_transform[2] if not escape_key or not unescape_key then return nil, "expecting key_transform = { escape, unescape } table" end end end return setmetatable({ sock = sock, escape_key = escape_key, unescape_key = unescape_key, }, mt) end function _M.set_timeout(self, timeout) local sock = self.sock if not sock then return nil, "not initialized" end sock:settimeout(timeout) return 1 end function _M.connect(self, ...) local sock = self.sock if not sock then return nil, "not initialized" end return sock:connect(...) end local function _multi_get(self, keys) local sock = self.sock if not sock then return nil, "not initialized" end local nkeys = #keys if nkeys == 0 then return {}, nil end local escape_key = self.escape_key local cmd = {"get"} local n = 1 for i = 1, nkeys do cmd[n + 1] = " " cmd[n + 2] = escape_key(keys[i]) n = n + 2 end cmd[n + 1] = "\r\n" -- print("multi get cmd: ", cmd) local bytes, err = sock:send(cmd) if not bytes then return nil, err end local unescape_key = self.unescape_key local results = {} while true do local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end if line == 'END' then break end local key, flags, len = match(line, '^VALUE (%S+) (%d+) (%d+)$') -- print("key: ", key, "len: ", len, ", flags: ", flags) if not key then return nil, line end local data, err = sock:receive(len) if not data then if err == "timeout" then sock:close() end return nil, err end results[unescape_key(key)] = {data, flags} data, err = sock:receive(2) -- discard the trailing CRLF if not data then if err == "timeout" then sock:close() end return nil, err end end return results end function _M.get(self, key) if type(key) == "table" then return _multi_get(self, key) end local sock = self.sock if not sock then return nil, nil, "not initialized" end local bytes, err = sock:send("get " .. self.escape_key(key) .. "\r\n") if not bytes then return nil, nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, nil, err end if line == 'END' then return nil, nil, nil end local flags, len = match(line, '^VALUE %S+ (%d+) (%d+)$') if not flags then return nil, nil, line end -- print("len: ", len, ", flags: ", flags) local data, err = sock:receive(len) if not data then if err == "timeout" then sock:close() end return nil, nil, err end line, err = sock:receive(7) -- discard the trailing "\r\nEND\r\n" if not line then if err == "timeout" then sock:close() end return nil, nil, err end return data, flags end local function _multi_gets(self, keys) local sock = self.sock if not sock then return nil, "not initialized" end local nkeys = #keys if nkeys == 0 then return {}, nil end local escape_key = self.escape_key local cmd = {"gets"} local n = 1 for i = 1, nkeys do cmd[n + 1] = " " cmd[n + 2] = escape_key(keys[i]) n = n + 2 end cmd[n + 1] = "\r\n" -- print("multi get cmd: ", cmd) local bytes, err = sock:send(cmd) if not bytes then return nil, err end local unescape_key = self.unescape_key local results = {} while true do local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end if line == 'END' then break end local key, flags, len, cas_uniq = match(line, '^VALUE (%S+) (%d+) (%d+) (%d+)$') -- print("key: ", key, "len: ", len, ", flags: ", flags) if not key then return nil, line end local data, err = sock:receive(len) if not data then if err == "timeout" then sock:close() end return nil, err end results[unescape_key(key)] = {data, flags, cas_uniq} data, err = sock:receive(2) -- discard the trailing CRLF if not data then if err == "timeout" then sock:close() end return nil, err end end return results end function _M.gets(self, key) if type(key) == "table" then return _multi_gets(self, key) end local sock = self.sock if not sock then return nil, nil, nil, "not initialized" end local bytes, err = sock:send("gets " .. self.escape_key(key) .. "\r\n") if not bytes then return nil, nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, nil, nil, err end if line == 'END' then return nil, nil, nil, nil end local flags, len, cas_uniq = match(line, '^VALUE %S+ (%d+) (%d+) (%d+)$') if not flags then return nil, nil, nil, line end -- print("len: ", len, ", flags: ", flags) local data, err = sock:receive(len) if not data then if err == "timeout" then sock:close() end return nil, nil, nil, err end line, err = sock:receive(7) -- discard the trailing "\r\nEND\r\n" if not line then if err == "timeout" then sock:close() end return nil, nil, nil, err end return data, flags, cas_uniq end local function _expand_table(value) local segs = {} local nelems = #value local nsegs = 0 for i = 1, nelems do local seg = value[i] nsegs = nsegs + 1 if type(seg) == "table" then segs[nsegs] = _expand_table(seg) else segs[nsegs] = seg end end return concat(segs) end local function _store(self, cmd, key, value, exptime, flags) if not exptime then exptime = 0 end if not flags then flags = 0 end local sock = self.sock if not sock then return nil, "not initialized" end if type(value) == "table" then value = _expand_table(value) end local req = cmd .. " " .. self.escape_key(key) .. " " .. flags .. " " .. exptime .. " " .. strlen(value) .. "\r\n" .. value .. "\r\n" local bytes, err = sock:send(req) if not bytes then return nil, err end local data, err = sock:receive() if not data then if err == "timeout" then sock:close() end return nil, err end if data == "STORED" then return 1 end return nil, data end function _M.set(self, ...) return _store(self, "set", ...) end function _M.add(self, ...) return _store(self, "add", ...) end function _M.replace(self, ...) return _store(self, "replace", ...) end function _M.append(self, ...) return _store(self, "append", ...) end function _M.prepend(self, ...) return _store(self, "prepend", ...) end function _M.cas(self, key, value, cas_uniq, exptime, flags) if not exptime then exptime = 0 end if not flags then flags = 0 end local sock = self.sock if not sock then return nil, "not initialized" end local req = "cas " .. self.escape_key(key) .. " " .. flags .. " " .. exptime .. " " .. strlen(value) .. " " .. cas_uniq .. "\r\n" .. value .. "\r\n" -- local cjson = require "cjson" -- print("request: ", cjson.encode(req)) local bytes, err = sock:send(req) if not bytes then return nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end -- print("response: [", line, "]") if line == "STORED" then return 1 end return nil, line end function _M.delete(self, key) local sock = self.sock if not sock then return nil, "not initialized" end key = self.escape_key(key) local req = "delete " .. key .. "\r\n" local bytes, err = sock:send(req) if not bytes then return nil, err end local res, err = sock:receive() if not res then if err == "timeout" then sock:close() end return nil, err end if res ~= 'DELETED' then return nil, res end return 1 end function _M.set_keepalive(self, ...) local sock = self.sock if not sock then return nil, "not initialized" end return sock:setkeepalive(...) end function _M.get_reused_times(self) local sock = self.sock if not sock then return nil, "not initialized" end return sock:getreusedtimes() end function _M.flush_all(self, time) local sock = self.sock if not sock then return nil, "not initialized" end local req if time then req = "flush_all " .. time .. "\r\n" else req = "flush_all\r\n" end local bytes, err = sock:send(req) if not bytes then return nil, err end local res, err = sock:receive() if not res then if err == "timeout" then sock:close() end return nil, err end if res ~= 'OK' then return nil, res end return 1 end local function _incr_decr(self, cmd, key, value) local sock = self.sock if not sock then return nil, "not initialized" end local req = cmd .. " " .. self.escape_key(key) .. " " .. value .. "\r\n" local bytes, err = sock:send(req) if not bytes then return nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end if not match(line, '^%d+$') then return nil, line end return line end function _M.incr(self, key, value) return _incr_decr(self, "incr", key, value) end function _M.decr(self, key, value) return _incr_decr(self, "decr", key, value) end function _M.stats(self, args) local sock = self.sock if not sock then return nil, "not initialized" end local req if args then req = "stats " .. args .. "\r\n" else req = "stats\r\n" end local bytes, err = sock:send(req) if not bytes then return nil, err end local lines = {} local n = 0 while true do local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end if line == 'END' then return lines, nil end if not match(line, "ERROR") then n = n + 1 lines[n] = line else return nil, line end end -- cannot reach here... return lines end function _M.version(self) local sock = self.sock if not sock then return nil, "not initialized" end local bytes, err = sock:send("version\r\n") if not bytes then return nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end local ver = match(line, "^VERSION (.+)$") if not ver then return nil, ver end return ver end function _M.quit(self) local sock = self.sock if not sock then return nil, "not initialized" end local bytes, err = sock:send("quit\r\n") if not bytes then return nil, err end return 1 end function _M.verbosity(self, level) local sock = self.sock if not sock then return nil, "not initialized" end local bytes, err = sock:send("verbosity " .. level .. "\r\n") if not bytes then return nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end if line ~= 'OK' then return nil, line end return 1 end function _M.touch(self, key, exptime) local sock = self.sock if not sock then return nil, "not initialized" end local bytes, err = sock:send("touch " .. self.escape_key(key) .. " " .. exptime .. "\r\n") if not bytes then return nil, err end local line, err = sock:receive() if not line then if err == "timeout" then sock:close() end return nil, err end -- moxi server from couchbase returned stored after touching if line == "TOUCHED" or line =="STORED" then return 1 end return nil, line end function _M.close(self) local sock = self.sock if not sock then return nil, "not initialized" end return sock:close() end return _M lua-resty-memcached-0.14/t/000077500000000000000000000000001265223512000155305ustar00rootroot00000000000000lua-resty-memcached-0.14/t/mock.t000066400000000000000000000102241265223512000166450ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket::Lua; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (4 * blocks() + 4); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; no_long_string(); run_tests(); __DATA__ === TEST 1: fail to flush --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", 1921); if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end ngx.say("flush: ", ok); memc:close() '; } --- request GET /t --- tcp_listen: 1921 --- tcp_query_len: 11 --- tcp_query eval "flush_all\r\n" --- tcp_reply eval "SOME ERROR\r\n" --- response_body failed to flush all: SOME ERROR --- no_error_log [error] === TEST 2: continue using the obj when read timeout happens --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() local ok, err = memc:connect("127.0.0.1", 1921); if not ok then ngx.say("failed to connect: ", err) return end memc:set_timeout(100) -- 0.1 sec for i = 1, 2 do local data, flags, err = memc:get("foo") if not data and err then ngx.say("failed to get: ", err) else ngx.say("get: ", data); end ngx.sleep(0.1) end memc:close() '; } --- request GET /t --- tcp_listen: 1921 --- tcp_query_len: 9 --- tcp_query eval "get foo\r\n" --- tcp_reply eval "VALUE foo 0 5\r\nhello\r\nEND\r\n" --- tcp_reply_delay: 150ms --- response_body failed to get: timeout failed to get: closed --- error_log lua tcp socket read timed out === TEST 3: gets multi getting error responses --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", 1921); if not ok then ngx.say("failed to connect: ", err) return end local res, err = memc:gets({"dog", "cat"}) if not res then ngx.say("failed to gets: ", err) return end ngx.say("gets: ", table.concat(res, ", ")); memc:close() '; } --- request GET /t --- tcp_listen: 1921 --- tcp_query_len: 14 --- tcp_query eval "gets dog cat\r\n" --- tcp_reply eval "SERVER_ERROR\r\n" --- response_body failed to gets: SERVER_ERROR --- no_error_log [error] === TEST 4: get multi getting error responses --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", 1921); if not ok then ngx.say("failed to connect: ", err) return end local res, err = memc:get({"dog", "cat"}) if not res then ngx.say("failed to get: ", err) return end ngx.say("get: ", table.concat(res, ", ")); memc:close() '; } --- request GET /t --- tcp_listen: 1921 --- tcp_query_len: 13 --- tcp_query eval "get dog cat\r\n" --- tcp_reply eval "SERVER_ERROR\r\n" --- response_body failed to get: SERVER_ERROR --- no_error_log [error] lua-resty-memcached-0.14/t/sanity.t000066400000000000000000001521551265223512000172350ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket::Lua; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (3 * blocks() - 1); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; }; $ENV{TEST_NGINX_RESOLVER} = '8.8.8.8'; $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; no_long_string(); run_tests(); __DATA__ === TEST 1: basic --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() assert(memc:set_timeout(1000)) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end for i = 1, 2 do local res, flags, err = memc: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, " (flags: ", flags, ")") end memc:close() '; } --- request GET /t --- response_body dog: 32 (flags: 0) dog: 32 (flags: 0) --- no_error_log [error] === TEST 2: add an exsitent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:add("dog", 56) if not ok then ngx.say("failed to add dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body failed to add dog: NOT_STORED dog: 32 --- no_error_log [error] === TEST 3: add a nonexistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:add("dog", 56) if not ok then ngx.say("failed to add dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body dog: 56 --- no_error_log [error] === TEST 4: set an exsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:set("dog", 56) if not ok then ngx.say("failed to set dog: ", err) return end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body dog: 56 --- no_error_log [error] === TEST 5: replace an exsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:replace("dog", 56) if not ok then ngx.say("failed to replace dog: ", err) return end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body dog: 56 --- no_error_log [error] === TEST 6: replace a nonexsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:replace("dog", 56) if not ok then ngx.say("failed to replace dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body failed to replace dog: NOT_STORED dog not found --- no_error_log [error] === TEST 7: prepend to a nonexsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:prepend("dog", 56) if not ok then ngx.say("failed to prepend to dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body failed to prepend to dog: NOT_STORED dog not found --- no_error_log [error] === TEST 8: prepend to an exsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) end local ok, err = memc:prepend("dog", 56) if not ok then ngx.say("failed to prepend to dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body dog: 5632 --- no_error_log [error] === TEST 9: append to a nonexsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:append("dog", 56) if not ok then ngx.say("failed to append to dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body failed to append to dog: NOT_STORED dog not found --- no_error_log [error] === TEST 10: append to an exsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) end local ok, err = memc:append("dog", 56) if not ok then ngx.say("failed to append to dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body dog: 3256 --- no_error_log [error] === TEST 11: delete an exsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) end local ok, err = memc:delete("dog") if not ok then ngx.say("failed to delete dog: ", err) end local res, flags, err = memc: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) local res, flags, err = memc:add("dog", 772) if err then ngx.say("failed to add dog: ", err) return end memc:close() '; } --- request GET /t --- response_body dog not found --- no_error_log [error] === TEST 12: delete a nonexsistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:delete("dog") if not ok then ngx.say("failed to delete dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body failed to delete dog: NOT_FOUND dog not found --- no_error_log [error] === TEST 13: delete an exsistent key with delay --- SKIP --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:delete("dog", 1) if not ok then ngx.say("failed to delete dog: ", err) end local ok, err = memc:add("dog", 76) if not ok then ngx.say("failed to add dog: ", err) end local ok, err = memc:replace("dog", 53) if not ok then ngx.say("failed to replace dog: ", err) end local res, flags, err = memc: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) memc:close() '; } --- request GET /t --- response_body failed to add dog: NOT_STORED failed to replace dog: NOT_STORED dog not found --- no_error_log [error] === TEST 14: flags --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32, 0, 526) if not ok then ngx.say("failed to set dog: ", err) return end local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body dog: 32 (flags: 526) --- no_error_log [error] === TEST 15: set with exptime --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local res, flags, err = memc:get("dog") if err then ngx.say("failed to get dog: ", err) return end ngx.location.capture("/sleep"); local ok, err = memc:set("dog", 32, 1, 526) if not ok then ngx.say("failed to set dog: ", err) return end if not res then ngx.say("dog not found") return end ngx.say("dog: ", res, " (flags: ", flags, ")") memc:close() '; } location /sleep { echo_sleep 1.1; } --- request GET /t --- response_body dog not found --- no_error_log [error] === TEST 16: flush with a delay --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:flush_all(3) if not ok then ngx.say("failed to flush all: ", err) return end local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body dog: 32 (flags: 0) --- no_error_log [error] === TEST 17: incr an existent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local value, err = memc:incr("dog", 2) if not value then ngx.say("failed to incr dog: ", err) return end ngx.say("dog is now: ", value) local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body dog is now: 34 dog: 34 (flags: 0) --- no_error_log [error] === TEST 18: incr a nonexistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local value, err = memc:incr("dog", 2) if not value then ngx.say("failed to incr dog: ", err) return end ngx.say("dog is now: ", value) local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body failed to incr dog: NOT_FOUND --- no_error_log [error] === TEST 19: decr an existent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local value, err = memc:decr("dog", 3) if not value then ngx.say("failed to decr dog: ", err) return end ngx.say("dog is now: ", value) local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body dog is now: 29 dog: 29 (flags: 0) --- no_error_log [error] === TEST 20: decr a nonexistent key --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local value, err = memc:decr("dog", 2) if not value then ngx.say("failed to decr dog: ", err) return end ngx.say("dog is now: ", value) local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body failed to decr dog: NOT_FOUND --- no_error_log [error] === TEST 21: general stats --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local lines, err = memc:stats() if not lines then ngx.say("failed to stats: ", err) return end ngx.say("stats:\\n", table.concat(lines, "\\n")) memc:close() '; } --- request GET /t --- response_body_like chop ^stats: STAT pid \d+ (?:STAT [^\n]+\n)*$ --- no_error_log [error] === TEST 22: stats items --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local lines, err = memc:stats("items") if not lines then ngx.say("failed to stats items: ", err) return end ngx.say("stats:\\n", table.concat(lines, "\\n")) memc:close() '; } --- request GET /t --- response_body_like chop ^stats: (?:STAT items:[^\n]+\n)*$ --- no_error_log [error] === TEST 23: stats sizes --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local lines, err = memc:stats("sizes") if not lines then ngx.say("failed to stats sizes: ", err) return end ngx.say("stats:\\n", table.concat(lines, "\\n")) memc:close() '; } --- request GET /t --- response_body_like chop ^stats: (?:STAT \d+ \d+\n)*$ --- no_error_log [error] === TEST 24: version --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ver, err = memc:version() if not ver then ngx.say("failed to get version: ", err) return end ngx.say("version: ", ver) memc:close() '; } --- request GET /t --- response_body_like chop ^version: \d+(?:\.\d+)*$ --- no_error_log [error] === TEST 25: quit --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:quit() if not ok then ngx.say("failed to quit: ", err) return end local ver, err = memc:version() if not ver then ngx.say("failed to get version: ", err) return end local ok, err = memc:close() if not ok then ngx.say("failed to close: ", err) return end ngx.say("closed successfully") '; } --- request GET /t --- response_body_like chop ^failed to get version: (closed|timeout|broken pipe|connection reset by peer)$ === TEST 26: verbosity --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:verbosity(2) if not ok then ngx.say("failed to quit: ", err) return end ngx.say("successfully set verbosity to level 2") local ver, err = memc:version() if not ver then ngx.say("failed to get version: ", err) return end local ok, err = memc:close() if not ok then ngx.say("failed to close: ", err) return end '; } --- request GET /t --- response_body successfully set verbosity to level 2 --- no_error_log [error] === TEST 27: multi get --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:set("cat", "hello\\nworld\\n") if not ok then ngx.say("failed to set dog: ", err) return end for i = 1, 2 do local results, err = memc:get({"dog", "blah", "cat"}) if err then ngx.say("failed to get keys: ", err) return end if not results then ngx.say("results empty") return end ngx.say("dog: ", results.dog and table.concat(results.dog, " ") or "not found") ngx.say("cat: ", results.cat and table.concat(results.cat, " ") or "not found") ngx.say("blah: ", results.blah and table.concat(results.blah, " ") or "not found") end local ok, err = memc:close() if not ok then ngx.say("failed to close: ", err) return end '; } --- request GET /t --- response_body dog: 32 0 cat: hello world 0 blah: not found dog: 32 0 cat: hello world 0 blah: not found --- no_error_log [error] === TEST 28: multi get (special chars in keys) --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog A", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:set("cat B", "hello\\nworld\\n") if not ok then ngx.say("failed to set dog: ", err) return end local results, err = memc:get({"dog A", "blah", "cat B"}) if err then ngx.say("failed to get dog: ", err) return end if not results then ngx.say("results empty") return end ngx.say("dog A: ", results["dog A"] and table.concat(results["dog A"], " ") or "not found") ngx.say("cat B: ", results["cat B"] and table.concat(results["cat B"], " ") or "not found") ngx.say("blah: ", results.blah and table.concat(results.blah, " ") or "not found") local ok, err = memc:close() if not ok then ngx.say("failed to close: ", err) return end '; } --- request GET /t --- response_body dog A: 32 0 cat B: hello world 0 blah: not found --- no_error_log [error] === TEST 29: connect timeout --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(100) -- 100 ms local ok, err = memc:connect("www.taobao.com", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local res, flags, err = memc: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, " (flags: ", flags, ")") memc:close() '; } --- request GET /t --- response_body failed to connect: timeout --- error_log lua tcp socket connect timed out === TEST 30: set keepalive and get reused times --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local times = memc:get_reused_times() ngx.say("reused times: ", times) local ok, err = memc:set_keepalive() if not ok then ngx.say("failed to set keepalive: ", err) return end ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end times = memc:get_reused_times() ngx.say("reused times: ", times) '; } --- request GET /t --- response_body reused times: 0 reused times: 1 --- no_error_log [error] === TEST 31: gets (single key, found) --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local res, flags, cas_uniq, err = memc:gets("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, " (flags: ", flags, ", cas_uniq: ", cas_uniq, ")") '; } --- request GET /t --- response_body_like chop ^dog: 32 \(flags: 0, cas_uniq: \d+\)$ --- no_error_log [error] === TEST 32: gets (single key, not found) --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local res, flags, cas_uniq, err = memc:gets("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, " (flags: ", flags, ", cas_uniq: ", cas_uniq, ")") '; } --- request GET /t --- response_body_like chop dog not found --- no_error_log [error] === TEST 33: gets (multiple key) --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local ok, err = memc:set("cat", "hello\\nworld\\n") if not ok then ngx.say("failed to set dog: ", err) return end local results, err = memc:gets({"dog", "blah", "cat"}) if err then ngx.say("failed to get keys: ", err) return end if not results then ngx.say("results empty") return end if results.dog then ngx.say("dog: ", table.concat(results.dog, " ")) else ngx.say("dog not found") end if results.blah then ngx.say("blah: ", table.concat(results.blah, " ")) else ngx.say("blah not found") end if results.cat then ngx.say("cat: ", table.concat(results.cat, " ")) else ngx.say("cat not found") end local ok, err = memc:close() if not ok then ngx.say("failed to close: ", err) return end '; } --- request GET /t --- response_body_like chop ^dog: 32 0 \d+ blah not found cat: hello world 0 \d+$ --- no_error_log [error] === TEST 34: gets (single key) + cas --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local value, flags, cas_uniq, err = memc:gets("dog") if err then ngx.say("failed to get dog: ", err) return end ngx.say("dog: ", value, " (flags: ", flags, ", cas_uniq: ", cas_uniq, ")") local ok, err = memc:cas("dog", "hello world", cas_uniq, 0, 78) if not ok then ngx.say("failed to cas: ", err) return end ngx.say("cas succeeded") local value, flags, err = memc:get("dog") if err then ngx.say("failed to get dog: ", err) return end ngx.say("dog: ", value, " (flags: ", flags, ")") '; } --- request GET /t --- response_body_like chop ^dog: 32 \(flags: 0, cas_uniq: \d+\) cas succeeded dog: hello world \(flags: 78\)$ --- no_error_log [error] === TEST 35: gets (multi key) + cas --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local results, err = memc:gets({"dog"}) if err then ngx.say("failed to get dog: ", err) return end local value, flags, cas_uniq, err = unpack(results.dog) ngx.say("dog: ", value, " (flags: ", flags, ", cas_uniq: ", cas_uniq, ")") local ok, err = memc:cas("dog", "hello world", cas_uniq, 0, 78) if not ok then ngx.say("failed to cas: ", err) return end ngx.say("cas succeeded") local value, flags, err = memc:get("dog") if err then ngx.say("failed to get dog: ", err) return end ngx.say("dog: ", value, " (flags: ", flags, ")") '; } --- request GET /t --- response_body_like chop ^dog: 32 \(flags: 0, cas_uniq: \d+\) cas succeeded dog: hello world \(flags: 78\)$ --- no_error_log [error] === TEST 36: gets (single key) + cas --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", 32) if not ok then ngx.say("failed to set dog: ", err) return end local value, flags, cas_uniq, err = memc:gets("dog") if err then ngx.say("failed to get dog: ", err) return end ok, err = memc:set("dog", 117) if not ok then ngx.say("failed to set dog: ", err) return end ngx.say("dog: ", value, " (flags: ", flags, ", cas_uniq: ", cas_uniq, ")") local ok, err = memc:cas("dog", "hello world", cas_uniq, 0, 78) if not ok then ngx.say("failed to cas: ", err) return end ngx.say("cas succeeded") local value, flags, err = memc:get("dog") if err then ngx.say("failed to get dog: ", err) return end ngx.say("dog: ", value, " (flags: ", flags, ")") '; } --- request GET /t --- response_body_like chop ^dog: 32 \(flags: 0, cas_uniq: \d+\) failed to cas: EXISTS$ --- no_error_log [error] === TEST 37: change escape method --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' function identity(str) return str end local memcached = require "resty.memcached" local memc = memcached:new{ key_transform = { identity, identity }} local key = "dog&cat" memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set(key, 32) if not ok then ngx.say("failed to set dog: ", err) return end for i = 1, 2 do local res, flags, err = memc:get(key) if err then ngx.say("failed to get", key, ": ", err) return end if not res then ngx.say(key, " not found") return end ngx.say(key, ": ", res, " (flags: ", flags, ")") end memc:close() '; } --- request GET /t --- response_body dog&cat: 32 (flags: 0) dog&cat: 32 (flags: 0) --- no_error_log [error] === TEST 38: gets (multiple key) + change only unescape key --- http_config eval: $::HttpConfig --- config resolver $TEST_NGINX_RESOLVER; location /t { content_by_lua ' function identity(str) return str end local memcached = require "resty.memcached" local memc = memcached:new{key_transform = {ngx.escape_uri, identity}} local key = "dog&cat" memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set(key, 32) if not ok then ngx.say("failed to set ", key, ": ", err) return end local ok, err = memc:set("cat", "hello\\nworld\\n") if not ok then ngx.say("failed to set dog: ", err) return end local results, err = memc:gets({key, "blah", "cat"}) if err then ngx.say("failed to get keys: ", err) return end if not results then ngx.say("results empty") return end if results[key] then ngx.say(key, ": ", table.concat(results[key], " ")) else ngx.say(key, " not found") end -- encode key for second run key = ngx.escape_uri(key) if results[key] then ngx.say(key, ": ", table.concat(results[key], " ")) else ngx.say(key, " not found") end if results.blah then ngx.say("blah: ", table.concat(results.blah, " ")) else ngx.say("blah not found") end if results.cat then ngx.say("cat: ", table.concat(results.cat, " ")) else ngx.say("cat not found") end local ok, err = memc:close() if not ok then ngx.say("failed to close: ", err) return end '; } --- request GET /t --- response_body_like chop ^dog&cat not found dog%26cat: 32 0 \d+ blah not found cat: hello world 0 \d+$ --- no_error_log [error] lua-resty-memcached-0.14/t/tableset.t000066400000000000000000000060521265223512000175230ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket::Lua; 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'; $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; no_long_string(); run_tests(); __DATA__ === TEST 1: set with table --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", { "c", "a", "t" }) if not ok then ngx.say("failed to set dog: ", err) return end for i = 1, 2 do local res, flags, err = memc: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, " (flags: ", flags, ")") end memc:close() '; } --- request GET /t --- response_body dog: cat (flags: 0) dog: cat (flags: 0) --- no_error_log [error] === TEST 2: set with nested table --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set("dog", { "c", "a", { "t"} }) if not ok then ngx.say("failed to set dog: ", err) return end for i = 1, 2 do local res, flags, err = memc: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, " (flags: ", flags, ")") end memc:close() '; } --- request GET /t --- response_body dog: cat (flags: 0) dog: cat (flags: 0) --- no_error_log [error] lua-resty-memcached-0.14/t/touch.t000066400000000000000000000051741265223512000170460ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket::Lua; use Cwd qw(cwd); repeat_each(2); plan tests => repeat_each() * (2 * blocks()); my $pwd = cwd(); our $HttpConfig = qq{ lua_package_path "$pwd/lib/?.lua;;"; }; $ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211; no_long_string(); no_diff(); run_tests(); __DATA__ === TEST 1: basic --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local key = "dog&cat&rabbit" local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:set(key, "value", 1) if not ok then ngx.say("failed to set ", key, ": ", err) return end local ok, err = memc:touch(key, 120) -- 120sec if not ok then ngx.say("failed to touch ", key, ": ", err) return end ngx.say("touch: ", ok) ngx.sleep(1.1) local val, err = memc:get(key) if not val then ngx.say("failed to get ", key, ": ", err) return end ngx.say("get key: ", val) '; } --- request GET /t --- response_body touch: 1 get key: value === TEST 2: not exists --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local key = "dog&cat&rabbit" local memcached = require "resty.memcached" local memc = memcached:new() memc:set_timeout(1000) -- 1 sec local ok, err = memc:connect("127.0.0.1", $TEST_NGINX_MEMCACHED_PORT) if not ok then ngx.say("failed to connect: ", err) return end local ok, err = memc:flush_all() if not ok then ngx.say("failed to flush all: ", err) return end local ok, err = memc:touch(key, 120) --120sec if not ok then ngx.say("failed to touch ", key, ": ", err) return end ngx.say("touch: ", ok) '; } --- request GET /t --- response_body failed to touch dog&cat&rabbit: NOT_FOUND lua-resty-memcached-0.14/t/version.t000066400000000000000000000011541265223512000174030ustar00rootroot00000000000000# vim:set ft= ts=4 sw=4 et: use Test::Nginx::Socket::Lua; 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 memc = require "resty.memcached" ngx.say(memc._VERSION) '; } --- request GET /t --- response_body_like chop ^\d+\.\d+$ --- no_error_log [error] lua-resty-memcached-0.14/valgrind.suppress000066400000000000000000000236341265223512000207110ustar00rootroot00000000000000{ 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 } { Memcheck:Cond fun:index fun:expand_dynamic_string_token fun:_dl_map_object fun:map_doit fun:_dl_catch_error fun:do_preload fun:dl_main fun:_dl_sysdep_start fun:_dl_start }