pax_global_header00006660000000000000000000000064152112655050014514gustar00rootroot0000000000000052 comment=98bf47d7a48105c2e5ad5437cda0922a9c02fef6 openresty-lua-resty-memcached-98bf47d/000077500000000000000000000000001521126550500200245ustar00rootroot00000000000000openresty-lua-resty-memcached-98bf47d/.gitattributes000066400000000000000000000000331521126550500227130ustar00rootroot00000000000000*.t linguist-language=Text openresty-lua-resty-memcached-98bf47d/.gitignore000066400000000000000000000000531521126550500220120ustar00rootroot00000000000000*.swp *.swo *~ go t/servroot/ reindex *.t_ openresty-lua-resty-memcached-98bf47d/.travis.yml000066400000000000000000000072051521126550500221410ustar00rootroot00000000000000sudo: required dist: focal branches: only: - "master" os: linux language: c compiler: - gcc addons: apt: packages: - memcached - time - psmisc - net-tools - libevent-dev cache: directories: - download-cache env: global: - JOBS=3 - NGX_BUILD_JOBS=$JOBS - LUAJIT_PREFIX=/opt/luajit21 - LUAJIT_LIB=$LUAJIT_PREFIX/lib - LUAJIT_INC=$LUAJIT_PREFIX/include/luajit-2.1 - LUA_INCLUDE_DIR=$LUAJIT_INC - LUA_CMODULE_DIR=/lib - OPENSSL_PREFIX=/usr/local/openresty/openssl3 - OPENSSL_LIB=$OPENSSL_PREFIX/lib - OPENSSL_INC=$OPENSSL_PREFIX/include - OPENSSL_VER=1.1.1l - LD_LIBRARY_PATH=$LUAJIT_LIB:$LD_LIBRARY_PATH - TEST_NGINX_SLEEP=0.006 - MEMCACHED_VERSION=1.6.10 matrix: - NGINX_VERSION=1.31.0 install: - wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add - - echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list - sudo apt-get update - sudo DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends openresty-pcre2 openresty-openssl3 openresty-pcre2-dev openresty-openssl3-dev - if [ ! -d download-cache ]; then mkdir download-cache; fi - wget https://memcached.org/files/memcached-${MEMCACHED_VERSION}.tar.gz - sudo apt-get install -qq -y axel - cpanm --sudo --notest Test::Nginx IPC::Run Test2::Util > build.log 2>&1 || (cat build.log && exit 1) - wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz - git clone https://github.com/openresty/openresty.git ../openresty - git clone https://github.com/openresty/nginx-devel-utils.git - git clone https://github.com/simpl/ngx_devel_kit.git ../ndk-nginx-module - git clone https://github.com/openresty/lua-nginx-module.git ../lua-nginx-module - git clone https://github.com/openresty/lua-resty-core.git ../lua-resty-core - git clone https://github.com/openresty/lua-resty-lrucache.git ../lua-resty-lrucache - git clone https://github.com/openresty/echo-nginx-module.git ../echo-nginx-module - git clone https://github.com/openresty/no-pool-nginx.git ../no-pool-nginx - git clone -b v2.1-agentzh https://github.com/openresty/luajit2.git - git clone https://github.com/openresty/mockeagain.git script: - tar -xf memcached-${MEMCACHED_VERSION}.tar.gz - cd memcached-${MEMCACHED_VERSION} && ./configure --enable-tls && make && sudo make install && cd .. - sudo /usr/local/bin/memcached -u nobody -d -o track_sizes -U 11211 -p 11211 -l 127.0.0.1 - sudo /usr/local/bin/memcached -u nobody -d -Z -o track_sizes -o ssl_chain_cert=./t/cert/server.crt -o ssl_key=./t/cert/server.key -p 11212 -l 127.0.0.1 - cd luajit2/ - make -j$JOBS CCDEBUG=-g Q= PREFIX=$LUAJIT_PREFIX CC=$CC XCFLAGS='-DLUA_USE_APICHECK -DLUA_USE_ASSERT' > build.log 2>&1 || (cat build.log && exit 1) - sudo make install PREFIX=$LUAJIT_PREFIX > build.log 2>&1 || (cat build.log && exit 1) - cd ../mockeagain/ && make CC=$CC -j$JOBS && cd .. - export PATH=$PWD/work/nginx/sbin:$PWD/nginx-devel-utils:/usr/local/bin:$PATH - export LD_PRELOAD=$PWD/mockeagain/mockeagain.so - export LD_LIBRARY_PATH=$PWD/mockeagain:$LD_LIBRARY_PATH - export TEST_NGINX_RESOLVER=8.8.4.4 - export NGX_BUILD_CC=$CC - ngx-build $NGINX_VERSION --without-pcre2 --with-ipv6 --with-http_realip_module --with-http_ssl_module --with-cc-opt="-I$OPENSSL_INC" --with-ld-opt="-L$OPENSSL_LIB -Wl,-rpath,$OPENSSL_LIB" --add-module=../ndk-nginx-module --add-module=../lua-nginx-module --add-module=../echo-nginx-module --with-debug > build.log 2>&1 || (cat build.log && exit 1) - nginx -V; memcached -V; - ldd `which nginx`|grep -E 'luajit|ssl|pcre' - prove -r t openresty-lua-resty-memcached-98bf47d/Makefile000066400000000000000000000006511521126550500214660ustar00rootroot00000000000000OPENRESTY_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 openresty-lua-resty-memcached-98bf47d/README.markdown000066400000000000000000000507211521126550500225320ustar00rootroot00000000000000Name ==== 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) * [sslhandshake](#sslhandshake) * [set](#set) * [set_timeout](#set_timeout) * [set_timeouts](#set_timeouts) * [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) * [init_pipeline](#init_pipeline) * [commit_pipeline](#commit_pipeline) * [cancel_pipeline](#cancel_pipeline) * [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) sslhandshake ------------ **syntax:** *session, err = memc:sslhandshake(reused_session?, server_name?, ssl_verify?, send_status_req?)* Does SSL/TLS handshake on the currently established connection. See the [tcpsock.sslhandshake](https://github.com/openresty/lua-nginx-module#tcpsocksslhandshake) API from OpenResty for more details. [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 and defaults to `0` (meaning never expires). The expiration time is in seconds. The `flags` parameter is optional and defaults to `0`. [Back to TOC](#table-of-contents) set_timeout ---------- `syntax: ok, err = memc:set_timeout(timeout)` 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_timeouts ---------- `syntax: ok, err = memc:set_timeouts(connect_timeout, send_timeout, read_timeout)` Sets the timeouts (in ms) for connect, send and read operations respectively. Returns 1 when successful and nil plus a string describing the error otherwise. 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 object 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 and defaults to `0` (meaning never expires). The expiration time is in seconds. 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 and defaults to `0` (meaning never expires). The expiration time is in seconds. 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 and defaults to `0` (meaning never expires). The expiration time is in seconds. 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 and defaults to `0` (meaning never expires). The expiration time is in seconds. The `flags` parameter is optional and 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 table 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) init_pipeline --------- `syntax: err = memc:init_pipeline(n?)` Enable the Memcache pipelining mode. All subsequent calls to Memcache command methods will automatically get buffer 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. The optional params `n` is buffer tables size. default value 4 [Back to TOC](#table-of-contents) commit_pipeline --------- `syntax: results, err = memc:commit_pipeline()` Quits the pipelining mode by committing all the cached Memcache 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 success return a lua table. failed return a lua string describing the error upon failures. [Back to TOC](#table-of-contents) cancel_pipeline --------- `syntax: memc:cancel_pipeline()` Quits the pipelining mode by discarding all existing buffer Memcache commands since the last call to the init_pipeline method. the method no return. always succeeds. [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 (章亦春) , OpenResty Inc. [Back to TOC](#table-of-contents) Copyright and License ===================== This module is licensed under the BSD license. Copyright (C) 2012-2017, by Yichun "agentzh" Zhang (章亦春) , OpenResty 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) openresty-lua-resty-memcached-98bf47d/dist.ini000066400000000000000000000004441521126550500214720ustar00rootroot00000000000000name=lua-resty-memcached abstract=Lua memcached client driver for the ngx_lua based on the cosocket API author=Yichun "agentzh" Zhang (agentzh) is_original=yes license=2bsd lib_dir=lib doc_dir=lib repo_link=https://github.com/openresty/lua-resty-memcached main_module=lib/resty/memcached.lua openresty-lua-resty-memcached-98bf47d/lib/000077500000000000000000000000001521126550500205725ustar00rootroot00000000000000openresty-lua-resty-memcached-98bf47d/lib/resty/000077500000000000000000000000001521126550500217405ustar00rootroot00000000000000openresty-lua-resty-memcached-98bf47d/lib/resty/memcached.lua000066400000000000000000000463111521126550500243560ustar00rootroot00000000000000-- Copyright (C) Yichun Zhang (agentzh), CloudFlare Inc. 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 tab_insert = table.insert local setmetatable = setmetatable local type = type local tab_clear = require "table.clear" local cmd_tab = {} local _M = { _VERSION = '0.17' } local mt = { __index = _M } local ok, new_tab = pcall(require, "table.new") if not ok or type(new_tab) ~= "function" then new_tab = function (narr, nrec) return {} end end local function _read_reply(sock, len) local line, err if len == nil then line, err = sock:receive() else line, err = sock:receive(len) end if not line then if err == "timeout" then sock:close() end return nil, err end return line, nil end 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 local memc = setmetatable({ sock = sock, escape_key = escape_key, unescape_key = unescape_key, }, mt) return memc end local function set_timeouts(self, connect, send, read) local sock = self.sock if not sock then return nil, "not initialized" end sock:settimeouts(connect, send, read) return 1 end _M.set_timeouts = set_timeouts function _M.set_timeout(self, timeout) return set_timeouts(self, timeout, timeout, timeout) end function _M.connect(self, ...) local sock = self.sock if not sock then return nil, "not initialized" end return sock:connect(...) end function _M.sslhandshake(self, ...) local sock = self.sock if not sock then return nil, "not initialized" end return sock:sslhandshake(...) 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 tab_clear(cmd_tab) tab_insert(cmd_tab, "get") for i = 1, nkeys do tab_insert(cmd_tab, " ") tab_insert(cmd_tab, escape_key(keys[i])) end tab_insert(cmd_tab, "\r\n") -- print("multi get cmd: ", cmd_tab) local bytes, err = sock:send(cmd_tab) 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 local function _get_reply(sock) local line, err = _read_reply(sock) if err then 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 = _read_reply(sock, len) if err then return nil, nil, err end local _, err = _read_reply(sock, 7) -- discard the trailing "\r\nEND\r\n" if err then return nil, nil, err end return data, flags 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 reqs = rawget(self, "_reqs") if reqs then local readers = rawget(self, "_readers") tab_insert(reqs, "get ") tab_insert(reqs, self.escape_key(key)) tab_insert(reqs, "\r\n") tab_insert(readers, _get_reply) return 1 end tab_clear(cmd_tab) tab_insert(cmd_tab, "get ") tab_insert(cmd_tab, self.escape_key(key)) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, nil, err end return _get_reply(sock) 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 tab_clear(cmd_tab) tab_insert(cmd_tab, "gets") for i = 1, nkeys do tab_insert(cmd_tab, " ") tab_insert(cmd_tab, escape_key(keys[i])) end tab_insert(cmd_tab, "\r\n") -- print("multi get cmd: ", cmd_tab) local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end local unescape_key = self.unescape_key local results = {} while true do local line, err = _read_reply(sock) if err then 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 = _read_reply(sock, len) if err then return nil, err end results[unescape_key(key)] = {data, flags, cas_uniq} data, err = _read_reply(sock, 2) -- discard the trailing CRLF if err then 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 tab_clear(cmd_tab) tab_insert(cmd_tab, "gets ") tab_insert(cmd_tab, self.escape_key(key)) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, nil, nil, err end local line, err = _read_reply(sock) if err then 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 = _read_reply(sock, len) if not data then return nil, nil, nil, err end line, err = _read_reply(sock, 7) -- discard the trailing "\r\nEND\r\n" if not line then 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_reply(sock) local data, err = _read_reply(sock) if err then return nil, err end if data == "STORED" then return 1 end return nil, data 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 reqs = rawget(self, "_reqs") if reqs then local readers = rawget(self, "_readers") tab_insert(reqs, cmd) tab_insert(reqs, " ") tab_insert(reqs, self.escape_key(key)) tab_insert(reqs, " ") tab_insert(reqs, flags) tab_insert(reqs, " ") tab_insert(reqs, exptime) tab_insert(reqs, " ") tab_insert(reqs, strlen(value)) tab_insert(reqs, "\r\n") tab_insert(reqs, value) tab_insert(reqs, "\r\n") tab_insert(readers, _store_reply) return 1 end tab_clear(cmd_tab) tab_insert(cmd_tab, cmd) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, self.escape_key(key)) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, flags) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, exptime) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, strlen(value)) tab_insert(cmd_tab, "\r\n") tab_insert(cmd_tab, value) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end return _store_reply(sock) 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 tab_clear(cmd_tab) tab_insert(cmd_tab, "cas ") tab_insert(cmd_tab, self.escape_key(key)) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, flags) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, exptime) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, strlen(value)) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, cas_uniq) tab_insert(cmd_tab, "\r\n") tab_insert(cmd_tab, value) tab_insert(cmd_tab, "\r\n") -- local cjson = require "cjson" -- print("request: ", cjson.encode(cmd_tab)) local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end local line, err = _read_reply(sock) if err then return nil, err end -- print("response: [", line, "]") if line == "STORED" then return 1 end return nil, line end local function _delete_reply(sock) local res, err = _read_reply(sock) if err then return nil, err end if res ~= 'DELETED' then return nil, res end return 1 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 reqs = rawget(self, "_reqs") if reqs then local readers = rawget(self, "_readers") tab_insert(reqs, "delete ") tab_insert(reqs, key) tab_insert(reqs, "\r\n") tab_insert(readers, _delete_reply) return 1 end tab_clear(cmd_tab) tab_insert(cmd_tab, "delete ") tab_insert(cmd_tab, key) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end return _delete_reply(sock) 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 tab_clear(cmd_tab) if time then tab_insert(cmd_tab, "flush_all ") tab_insert(cmd_tab, time) tab_insert(cmd_tab, "\r\n") else tab_clear(cmd_tab) tab_insert(cmd_tab, "flush_all\r\n") end local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end local res, err = _read_reply(sock) if err then return nil, err end if res ~= 'OK' then return nil, res end return 1 end local function _incr_decr_reply(sock) local line, err = _read_reply(sock) if err then return nil, err end if not match(line, '^%d+$') then return nil, line end return line end local function _incr_decr(self, cmd, key, value) local sock = self.sock if not sock then return nil, "not initialized" end local reqs = rawget(self, "_reqs") local readers = rawget(self, "_readers") if reqs then tab_insert(reqs, cmd) tab_insert(reqs, " ") tab_insert(reqs, self.escape_key(key)) tab_insert(reqs, " ") tab_insert(reqs, value) tab_insert(reqs, "\r\n") tab_insert(readers, _incr_decr_reply) return 1 end tab_clear(cmd_tab) tab_insert(cmd_tab, cmd) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, self.escape_key(key)) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, value) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end return _incr_decr_reply(sock) 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 local function _stats_reply(sock) local lines = {} local n = 0 while true do local line, err = _read_reply(sock) if err then 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.stats(self, args) local sock = self.sock if not sock then return nil, "not initialized" end local reqs = rawget(self, "_reqs") local readers = rawget(self, "_readers") if reqs then if args then tab_insert(reqs, "stats ") tab_insert(reqs, args) tab_insert(reqs, "\r\n") else tab_insert(reqs, "stats\r\n") end tab_insert(readers, _stats_reply) return 1 end local bytes, err if args then tab_clear(cmd_tab) tab_insert(cmd_tab, "stats ") tab_insert(cmd_tab, args) tab_insert(cmd_tab, "\r\n") bytes, err = sock:send(cmd_tab) else bytes, err = sock:send("stats\r\n") end if not bytes then return nil, err end return _stats_reply(sock) end local function _version_reply(sock) local line, err = _read_reply(sock) if err then return nil, err end local ver = match(line, "^VERSION (.+)$") if not ver then return nil, line end return ver 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 return _version_reply(sock) 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 local function _verbosity_reply(sock) local line, err = _read_reply(sock) if err then return nil, err end if line ~= 'OK' then return nil, line end return 1 end function _M.verbosity(self, level) local sock = self.sock if not sock then return nil, "not initialized" end local reqs = rawget(self, "_reqs") if reqs then local readers = rawget(self, "_readers") tab_insert(reqs, "verbosity ") tab_insert(reqs, level) tab_insert(reqs, "\r\n") tab_insert(readers, _verbosity_reply) return 1 end tab_clear(cmd_tab) tab_insert(cmd_tab, "verbosity ") tab_insert(cmd_tab, level) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end return _verbosity_reply(sock) end local function _touch_reply(sock) local line, err = _read_reply(sock) if err then 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.touch(self, key, exptime) local sock = self.sock if not sock then return nil, "not initialized" end local reqs = rawget(self, "_reqs") local readers = rawget(self, "_readers") if reqs then tab_insert(reqs, "touch ") tab_insert(reqs, self.escape_key(key)) tab_insert(reqs, " ") tab_insert(reqs, exptime) tab_insert(reqs, "\r\n") tab_insert(readers, _touch_reply) return 1 end tab_clear(cmd_tab) tab_insert(cmd_tab, "touch ") tab_insert(cmd_tab, self.escape_key(key)) tab_insert(cmd_tab, " ") tab_insert(cmd_tab, exptime) tab_insert(cmd_tab, "\r\n") local bytes, err = sock:send(cmd_tab) if not bytes then return nil, err end return _touch_reply(sock) end function _M.close(self) local sock = self.sock if not sock then return nil, "not initialized" end return sock:close() end function _M.init_pipeline(self, n) if self._reqs then return "already init pipeline" end if n and type(n) ~= 'number' then return "bad n arg: number expected, but got " .. type(n) end self._reqs = new_tab(n or 20, 0) self._readers = new_tab(n or 4, 0) return nil end function _M.cancel_pipeline(self) self._reqs = nil self._readers = nil end function _M.commit_pipeline(self) local reqs = rawget(self, "_reqs") local readers = rawget(self, "_readers") self._reqs = nil self._readers = nil if not reqs or not readers then return nil, "no pipeline" end local sock = self.sock if not sock then return nil, "not initialized" end if #readers == 0 then return nil, "no more cmds" end local bytes, err = sock:send(reqs) if not bytes then return nil, err end local results = {} for i, reader in ipairs(readers) do results[i] = { reader(sock) } end return results, nil end return _M openresty-lua-resty-memcached-98bf47d/t/000077500000000000000000000000001521126550500202675ustar00rootroot00000000000000openresty-lua-resty-memcached-98bf47d/t/cert/000077500000000000000000000000001521126550500212245ustar00rootroot00000000000000openresty-lua-resty-memcached-98bf47d/t/cert/ca.crt000066400000000000000000000024221521126550500223210ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIDkTCCAnmgAwIBAgIUJPrw/2C2SybyKqm5jyLpJjyF7F0wDQYJKoZIhvcNAQEL BQAwWDELMAkGA1UEBhMCY24xCzAJBgNVBAgMAmZqMQswCQYDVQQHDAJ4bTEOMAwG A1UECgwFb3JpbmMxDDAKBgNVBAsMA2RldjERMA8GA1UEAwwIcm9vdC5jb20wHhcN MjEwOTE3MDQwNzM3WhcNMzEwOTE1MDQwNzM3WjBYMQswCQYDVQQGEwJjbjELMAkG A1UECAwCZmoxCzAJBgNVBAcMAnhtMQ4wDAYDVQQKDAVvcmluYzEMMAoGA1UECwwD ZGV2MREwDwYDVQQDDAhyb290LmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC AQoCggEBALsqT2jdc7siXvzjoRELrXEBpsArb9yVv/5UQxMTfahl1D85LItFXM/y WHEBurB5G0ih8lfWiTkP5FKE3HIRXJ6FX0YViCUrz1Xt4mSzkiwpvvc0afcoYyF4 pSazn1B4uFdHonUdYOWUbvTnfg2vMYof0xXmGRuyFX+wEnVgUa13uwl2ksGFpg9K IHfnOgk6YeIlsqoC0eeEL3FO/AAQe0KQWBrvWgiCRXU7MHpLvc1qD60EsUpKuVNI U21JNMDUjFugzsDFuUX8+sHY0qgFx3/BTuzymcrInN3ZHZL3+ZEODmxllDH2piVf 01KOp8o7IaFtljBj2ukttVpJ7RsbmwMCAwEAAaNTMFEwHQYDVR0OBBYEFFhJHTu7 uPa5dRBN8TeKeiS0lvtQMB8GA1UdIwQYMBaAFFhJHTu7uPa5dRBN8TeKeiS0lvtQ MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAEfN5I+hnduA2G5D rv9oNldOtifEw8+i3kcvfm4d6HkrKpMwsVXTlg5uosUXeQE9oGpOc6/H90GfYLc5 Xae74DQ/xfJ7mIPi/XPEjD7c47tAA3qx5mpmyzQHgMUmWONCAdFsSaWDtmWyUv/7 0VG8E8ThuNHvMFDwvIqcJmM5Mjq8kEZy3DOIp02N9wOlY6ZH+KxUZBZ+1Nmf9HO3 TW0lb/atay5EJwnqMer4wD3vRy1vHLg4RQ0oo9I0qT1uVcrQoEqM3u8BX+IAqv6g zb1P/v8atSo5BFL9NUVyl9BGldIuvGpokrsOGixeasUybborAllkYxZVV/1oGcmk Z7WEpGg= -----END CERTIFICATE----- openresty-lua-resty-memcached-98bf47d/t/cert/ca.key000066400000000000000000000033271521126550500223260ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- Proc-Type: 4,ENCRYPTED DEK-Info: DES-EDE3-CBC,596A1E93036923B5 Z/gvrmUeqYIQnuB4qwm1oUKjFnO+DPSNMUsmc4FmleX5cHlMqfHNgt6tawqKKtS0 5vKrzBNAyjICEsjfWl01I8dB2wVxRuqC9hPF5Cbx//XPCcQXFeMBxMWPCZdyG9qV 8KXwvIsP/9J6b7amFeO7wPgISB9b4kOpvM3HrsrCfyVfQsTA7M526yZp/KH3JDt/ lTJipbVernZlo74rbjmjR2ake9dY3kX7UNXlsyx9+4cqpVd6U2PRrZ9GRxaCRkMr hu52COze7Kx7GXECU/f4Gn9vuZ4GvYYMyjm/lNWLMdZQuTSU4Uta59YxBsUS/uWU hHTwATCALvaqixrb/vErje7QkSjN2pJPSLHi5j13fqS4sTYMp3rRyy58CsJnvcJf rsFFQ6LansaAwO33yZzYCJoXH5TG/dvqGq19tx2N1WcAueCVywAcvn4sLWMvzRR2 R9xbeD1wUoKE1RX5xIPmEg6TR/CCgozysLrwPRGtmIX3mi49Wh9wl0vYN39Y8xkA RFdM/EGs1p9TnbGtkZcun5EDsvbTUaFkWOmYhxfvx/qVNCAIsPMT5UAEmNDlJt0R 5Vv3B7DY+tgsbqfo/PozWC8cKJZSdkeopoFFAB0i/CMmZFyadIsgluC+0UhiwWHN alujA5Do5osm8tHNevPvSrOa16cFTpxo0XlRNE1QbMU0jYLbDPCnaYUOKkfwF0lV jMV/R0ChoysAcuDmCBSCsgT/LN9CF4sYB2ak8mryotlYXzt+85GCMazAJGKLqTJM Q1ES3bXyfNTCb+xMeab4utlEOczH/GxqO12wPFwqdKkodVTtyNHjigyCK2uEBqqD js2X5Wau+kLqdhkNfoKI8uyr++u4A2KtzkZ0GqgcKtlD170+fXZ5vH3QueT9btik 11BCzNrCMjWEfv2uXLI1kxOhLxrS3cM+IKU44WWS0rGbsx3zT+snOisusu9gFujR pNGb7yOOgPkDnCDREtsbB/4q+Gbkm/4oFa9jHGmadyGE95ohczZm2ek9fjuJQdPM SN8kvSXbiSmdjR5WAugj1AGwlqHEmWfuCRDOvMEsWsVrbQzHPz1d7lQ0n+4GZMD5 DE1HyC4hhkXWFbrR18/hEzlj8minsad56sOsZnRukfqgC+UwmzLCQ0FeRxZ6YPLO G8QUEsV0Hkye24hTYDe1koXGCuGk+rzc3PiBFlxcCxHrP1LWCgDG43FI6pCFSaja +lZb3joEJI0bWBD3Qvu3VZa7bv/cmalXc3jlRLKIl0w8vktXC7sXfNAsg5b5/b/s ouad+mWHgCAyEk/3sQAiU7iH6CEY2uSJ+4zFRwJrf2FuMZFY1fqemjnSUNHs1TSk TmDCnMMd7HT04d2gBTCfStot5Ea9mLxrBuTERTDElg9wicqmjgPdIVStkeyfuroI vKCo22KVQKOyxuX+F9N0PjSoyLSXn/1b1Qd5fsWPQKemsO0T2RN2ylU/ESJXOWOF 68C+NYJ0PDzQvjRKa+BIdCS2S2upRDcZWkCPmqrKGS3A1tp2RwSJsa5ekIhKQDl+ qdtexk2csT4gUQ/M0A5xg0+L1HuT9wDnHUA26JcwZEOUBKUp4KYUwHrtacRPbJAp -----END RSA PRIVATE KEY----- openresty-lua-resty-memcached-98bf47d/t/cert/server.crt000066400000000000000000000046521521126550500232530ustar00rootroot00000000000000-----BEGIN CERTIFICATE----- MIIDNzCCAh8CFBm+gdvmlf3JRMOp/BEuhJ4mrPjcMA0GCSqGSIb3DQEBCwUAMFgx CzAJBgNVBAYTAmNuMQswCQYDVQQIDAJmajELMAkGA1UEBwwCeG0xDjAMBgNVBAoM BW9yaW5jMQwwCgYDVQQLDANkZXYxETAPBgNVBAMMCHJvb3QuY29tMB4XDTIxMDkx NzA0MDkzM1oXDTMxMDkxNTA0MDkzM1owWDELMAkGA1UEBhMCY24xCzAJBgNVBAgM AmZqMQswCQYDVQQHDAJ4bTEOMAwGA1UECgwFb3JpbmMxDDAKBgNVBAsMA2RldjER MA8GA1UEAwwIdGVzdC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB AQDNb1PwTcCe9tM2KJ/oFj72sQqGjyhPCGYRrEf3K2b7ZJ523EaYBcVpU3va/lqu QvbNoxEjssFNtXmlxL3wkptOLoNQ6ArsZs/J7HCWqqOwgNkGBQo53geTdVYyqdL/ Tb3DJAoH1n0qoo0qTjmgSjR6Hmj7HNf+9pkMLLUMtNUUO1mMeBXubJ+A0Kx9wjEw hlC8jhr0TlAJwXM9acTOeEbVQPPMTq7xETmqRGXnFTxBWcKawbJ78pmLUc0uv9Py UyuPYDjRtpPxwJSX63hk9gfuMVCmXl6JOfS2CtBZ2dRbbd72Wn8ld37wWZEX/M7M z09fOdyft0pTrXTf2f8Ne+FVAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAEMOO5c9 kkSVe+vwPEr3JnGm+aQSgX8APa/CoXt+BETuecgjFLSH1Sb2nRWosUBMeQJzY4zx AbuBzyFsOUbFIJuUsKJjghDlOkrtUvq675vJ7/m5xzKxzl3I5TMHUmJ3+Zg7aOFu lJNLqGN3z536xKUPyEJoM9y6yvQuiDxzNr8Nb4iEg475Ro4OfOsNEIU888A3aWJY 5BsU3rnY1kkWPs7smr57O7bw0k0XRyEteeiS0aJ750J6aLDQBd+aVenUvWUoUq2L 5PK+/xSN6f8vLW6P0DW2wgtAjAcX73BISeBNUy6xbNDMy5n6poswsJ+ZxtodC/45 Dv4MkBCZBv5HHog= -----END CERTIFICATE----- -----BEGIN CERTIFICATE----- MIIDkTCCAnmgAwIBAgIUJPrw/2C2SybyKqm5jyLpJjyF7F0wDQYJKoZIhvcNAQEL BQAwWDELMAkGA1UEBhMCY24xCzAJBgNVBAgMAmZqMQswCQYDVQQHDAJ4bTEOMAwG A1UECgwFb3JpbmMxDDAKBgNVBAsMA2RldjERMA8GA1UEAwwIcm9vdC5jb20wHhcN MjEwOTE3MDQwNzM3WhcNMzEwOTE1MDQwNzM3WjBYMQswCQYDVQQGEwJjbjELMAkG A1UECAwCZmoxCzAJBgNVBAcMAnhtMQ4wDAYDVQQKDAVvcmluYzEMMAoGA1UECwwD ZGV2MREwDwYDVQQDDAhyb290LmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCC AQoCggEBALsqT2jdc7siXvzjoRELrXEBpsArb9yVv/5UQxMTfahl1D85LItFXM/y WHEBurB5G0ih8lfWiTkP5FKE3HIRXJ6FX0YViCUrz1Xt4mSzkiwpvvc0afcoYyF4 pSazn1B4uFdHonUdYOWUbvTnfg2vMYof0xXmGRuyFX+wEnVgUa13uwl2ksGFpg9K IHfnOgk6YeIlsqoC0eeEL3FO/AAQe0KQWBrvWgiCRXU7MHpLvc1qD60EsUpKuVNI U21JNMDUjFugzsDFuUX8+sHY0qgFx3/BTuzymcrInN3ZHZL3+ZEODmxllDH2piVf 01KOp8o7IaFtljBj2ukttVpJ7RsbmwMCAwEAAaNTMFEwHQYDVR0OBBYEFFhJHTu7 uPa5dRBN8TeKeiS0lvtQMB8GA1UdIwQYMBaAFFhJHTu7uPa5dRBN8TeKeiS0lvtQ MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAEfN5I+hnduA2G5D rv9oNldOtifEw8+i3kcvfm4d6HkrKpMwsVXTlg5uosUXeQE9oGpOc6/H90GfYLc5 Xae74DQ/xfJ7mIPi/XPEjD7c47tAA3qx5mpmyzQHgMUmWONCAdFsSaWDtmWyUv/7 0VG8E8ThuNHvMFDwvIqcJmM5Mjq8kEZy3DOIp02N9wOlY6ZH+KxUZBZ+1Nmf9HO3 TW0lb/atay5EJwnqMer4wD3vRy1vHLg4RQ0oo9I0qT1uVcrQoEqM3u8BX+IAqv6g zb1P/v8atSo5BFL9NUVyl9BGldIuvGpokrsOGixeasUybborAllkYxZVV/1oGcmk Z7WEpGg= -----END CERTIFICATE----- openresty-lua-resty-memcached-98bf47d/t/cert/server.key000066400000000000000000000032171521126550500232470ustar00rootroot00000000000000-----BEGIN RSA PRIVATE KEY----- MIIEpAIBAAKCAQEAzW9T8E3AnvbTNiif6BY+9rEKho8oTwhmEaxH9ytm+2SedtxG mAXFaVN72v5arkL2zaMRI7LBTbV5pcS98JKbTi6DUOgK7GbPyexwlqqjsIDZBgUK Od4Hk3VWMqnS/029wyQKB9Z9KqKNKk45oEo0eh5o+xzX/vaZDCy1DLTVFDtZjHgV 7myfgNCsfcIxMIZQvI4a9E5QCcFzPWnEznhG1UDzzE6u8RE5qkRl5xU8QVnCmsGy e/KZi1HNLr/T8lMrj2A40baT8cCUl+t4ZPYH7jFQpl5eiTn0tgrQWdnUW23e9lp/ JXd+8FmRF/zOzM9PXzncn7dKU61039n/DXvhVQIDAQABAoIBAAIttw8qU+ubpHvD jIaSjdJh6EGrDPthlGuFjzHQ1QO/emq6RUlhbkmCqCKXZfDJJwLhQtJFGAWx8ftP DdCaTWlT1vKps0v0fymeB0ehwaCijSqiLQyWDTCEIwjggitQ8L08CZsxrBwioyqa r8PCZbIR1n0rPiMnDFJzuuvKkyPmxFu9OA5u99Njmp5sj1O4ZcLwNdIFz8PiWOBX BBtxoIACU+Gq26qIAoBZFzEa5CS+hJd/xkolx9PMz7wyvf9fHd6mQg+U/tdi5VMj YAGw3x58U+vwgaCNkXVgUHBmFJ8rsTS0kCl96JkzFbAQOUUWS1b0f4WB2yacSqPi 2f8LDjUCgYEA62Z8U6tHSMTK7SLwF3LG1NH7A2CmgxDHhb83vNYvyM+4muBun3qa 40tPEOnuVlXDV3pZWGr4evPnjJxlEXK4qDC5EE/N1i265pIg4SVNSGg4wdoFeuep w9CUPPPv959c9xAakwC7/xhVgHpXN1C/s/ZWDnlPceTmWcZezn9wVbsCgYEA32mL 23ATMcS0VxpLYLqIyp5WZ2wCfePwcF7s/GG6VOansm5dixVWpc0+bxG/q3zXH5wJ ZYOBrghDAq+ldjWGKlD6JIO66Rb/J+6yOghzxsI3BsfE+Vd0MUkACV/UH1AGdBex a/BmvyjM2shoUtufeQ/PXuhicjqtmQIYJelgLC8CgYEAzCXBRL6Rv6YLZsSZ+3vq u76vNiqnvZrbrj47FGWcY0biDk79IQmvMdep5j06GV2fX0oYGfoLLHZCJUusywj2 48X9eQv8svZN1JB+O0OiVl0se9GcVw55x/wn0wSq49EtSoaIxVwprf1oiVeQM8Sv vF6yGzNRlrCwpMLIjcUvoukCgYAqs1Ol11/zzTLiywXJMR3gDC9biQtY4G4eLCaD fnJMs+Ete2appKRxAC+ErxHBRxUe14X8PwxUJQQZyVSIqWZBRETrIbphvqF6EmdC LEdkn85An4IIXiy5FsjJAeXbLkJtgfHJPaOFudidoFxAKfXq/5iU/TmDt69iZUIU TOyyxQKBgQCZQX3Q0Z2XuRVeaH7BHWOYhUR55a2BJad8rjl/X+sThGvDbGF5nFcl h5vYHfLIWNG2MsVP/6CM5E2VovKenxqkszV6GJ+RsuI2BWUSNi1kitA2yjJaqh/f XcQlq+x4flLzGooXO5YUiXj9mtcpBFOEOW/O4CMnTMlNUYyx/bLJ+w== -----END RSA PRIVATE KEY----- openresty-lua-resty-memcached-98bf47d/t/mock.t000066400000000000000000000102241521126550500214040ustar00rootroot00000000000000# 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] openresty-lua-resty-memcached-98bf47d/t/sanity.t000066400000000000000000001676641521126550500220070ustar00rootroot00000000000000# 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 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 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 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 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 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) 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 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) 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 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) 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 existent 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_like eval qr/failed to connect: timeout|failed to connect: network is unreachable/ms --- error_log eval qr/lua tcp socket connect timed out|Network is unreachable/ms === 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] === TEST 39: pipeline execute multi commands --- http_config eval: $::HttpConfig --- config location /t { content_by_lua_block { 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 vals, err err = memc:init_pipeline() if err then ngx.say(err) return end memc:set("age", 10, 30, 4) memc:set("name", "value") memc:get("age") memc:get("name") memc:incr("age", 3) memc:add("name", "-haha") memc:delete("name") vals, err = memc:commit_pipeline() if err then ngx.say(err) return end for i,v in ipairs(vals) do ngx.say((vals[i][1] or "")..(vals[i][2] or "")..(vals[i][3] or "")) end vals, err = memc:delete("name") if err then ngx.say(err) end memc:close() } } --- request GET /t --- response_body 1 1 104 value0 13 NOT_STORED 1 NOT_FOUND --- no_error_log [error] === TEST 40: pipeline commands buffer empty --- http_config eval: $::HttpConfig --- config location /t { content_by_lua_block { 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 vals, err err = memc:init_pipeline() if err then ngx.say("init: ", err) return end vals, err = memc:commit_pipeline() if err then ngx.say("commit: ", err) return end memc:close() } } --- request GET /t --- response_body commit: no more cmds --- no_error_log [error] === TEST 41: pipeline repeat commit --- http_config eval: $::HttpConfig --- config location /t { content_by_lua_block { 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 vals, err err = memc:init_pipeline() if err then ngx.say("init: ", err) return end memc:set("touch_key", "touch_value") memc:touch("touch_key", 60) memc:stats("items") vals, err = memc:commit_pipeline() if err then ngx.say("commit: ", err) return end if vals[1][2] then ngx.say('err: ', vals[1][2]) else ngx.say('return: ', vals[1][1]) end if vals[2][2] then ngx.say('err: ', vals[2][2]) else ngx.say('return: ', vals[2][1]) end if vals[3][2] then ngx.say('err: ', vals[3][2]) else ngx.say('return: ', #vals[3][1]) end vals, err = memc:commit_pipeline() if err then ngx.say("commit: ", err) end memc:close() } } --- request GET /t --- response_body_like chop return: 1 return: 1 return: \d+ commit: no pipeline --- no_error_log [error] === TEST 42: pipeline repeat init --- http_config eval: $::HttpConfig --- config location /t { content_by_lua_block { 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 vals, err err = memc:init_pipeline() if err then ngx.say("init: ", err) return end memc:set("init_key", "init_value") memc:touch("init_key", 60) err = memc:init_pipeline() if err then ngx.say("init: ", err) return end memc:close() } } --- request GET /t --- response_body init: already init pipeline --- no_error_log [error] === TEST 43: pipeline cancel --- http_config eval: $::HttpConfig --- config location /t { content_by_lua_block { 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 vals, err err = memc:init_pipeline() if err then ngx.say("init: ", err) return end memc:set("cancel_key", "cancel_value") memc:touch("cancel_key", 60) memc:cancel_pipeline() _, err = memc:commit_pipeline() if err then ngx.say("init: ", err) return end memc:close() } } --- request GET /t --- response_body init: no pipeline --- no_error_log [error] === TEST 44: pipeline init buffer size error --- http_config eval: $::HttpConfig --- config location /t { content_by_lua_block { 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 vals, err err = memc:init_pipeline('x') if err then ngx.say("init: ", err) return end memc:close() } } --- request GET /t --- response_body init: bad n arg: number expected, but got string --- no_error_log [error] openresty-lua-resty-memcached-98bf47d/t/tableset.t000066400000000000000000000060511521126550500222610ustar00rootroot00000000000000# 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] openresty-lua-resty-memcached-98bf47d/t/tls.t000066400000000000000000000071011521126550500212550ustar00rootroot00000000000000# 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()); log_level('info'); 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} ||= 11212; 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(2000)) -- 2 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 session, err = memc:sslhandshake(nil, "test.com", false) if err ~= nil then ngx.say("failed to connect to memcached: ", 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: verify server certificate failed --- http_config eval: $::HttpConfig --- config location /t { content_by_lua ' local memcached = require "resty.memcached" local memc = memcached:new() assert(memc:set_timeout(2000)) -- 2 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 session, err = memc:sslhandshake(nil, "test.com", true) if err ~= nil then ngx.say("failed to connect to memcached: ", 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 failed to connect to memcached: 19: self-signed certificate in certificate chain --- error_log lua ssl certificate verify error: (19: self-signed certificate in certificate chain) openresty-lua-resty-memcached-98bf47d/t/touch.t000066400000000000000000000051731521126550500216040ustar00rootroot00000000000000# 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 openresty-lua-resty-memcached-98bf47d/t/version.t000066400000000000000000000011531521126550500221410ustar00rootroot00000000000000# 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] openresty-lua-resty-memcached-98bf47d/valgrind.suppress000066400000000000000000000164011521126550500234420ustar00rootroot00000000000000{ 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 } { Memcheck:Leak fun:malloc fun:ngx_alloc obj:* } { exp-sgcheck:SorG fun:ngx_http_lua_ndk_set_var_get } { 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: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:Param socketcall.setsockopt(optval) fun:setsockopt fun:drizzle_state_connect } { Memcheck:Leak fun:malloc fun:ngx_alloc fun:ngx_pool_cleanup_add } { Memcheck:Cond fun:ngx_conf_flush_files fun:ngx_single_process_cycle fun:main } { 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: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 } { Memcheck:Leak match-leak-kinds: definite fun:malloc fun:ngx_alloc fun:ngx_set_environment fun:ngx_single_process_cycle } { Memcheck:Leak match-leak-kinds: definite fun:malloc fun:ngx_alloc fun:ngx_set_environment fun:ngx_worker_process_init fun:ngx_worker_process_cycle }