在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):openresty/lua-resty-memcached开源软件地址(OpenSource Url):https://github.com/openresty/lua-resty-memcached开源编程语言(OpenSource Language):Lua 97.9%开源软件介绍(OpenSource Introduction):Namelua-resty-memcached - Lua memcached client driver for the ngx_lua based on the cosocket API Table of Contents
StatusThis library is considered production ready. DescriptionThis Lua library is a 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 or OpenResty 1.0.15.7 is required. Synopsis 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
';
}
} MethodsThe new
Creates a memcached object. In case of failures, returns It accepts an optional
memached:new{
key_transform = { ngx.escape_uri, ngx.unescape_uri }
} connect
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. sslhandshakesyntax: 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 API from OpenResty for more details. set
Inserts an entry into memcached unconditionally. If the key already exists, overrides it. The memc:set("dog", {"a ", {"kind of"}, " animal"}) is functionally equivalent to memc:set("dog", "a kind of animal") The The set_timeout
Sets the timeout (in ms) protection for subsequent operations, including the Returns 1 when successful and nil plus a string describing the error otherwise. set_timeouts
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
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 Only call this method in the place you would have called the get_reused_times
This method returns the (successfully) reused times for the current connection. In case of error, it returns If the current connection does not come from the built-in connection pool, then this method always returns close
Closes the current memcached connection and returns the status. In case of success, returns add
Inserts an entry into memcached if and only if the key does not exist. The memc:add("dog", {"a ", {"kind of"}, " animal"}) is functionally equivalent to memc:add("dog", "a kind of animal") The The In case of success, returns replace
Inserts an entry into memcached if and only if the key does exist. The memc:replace("dog", {"a ", {"kind of"}, " animal"}) is functionally equivalent to memc:replace("dog", "a kind of animal") The The In case of success, returns append
Appends the value to an entry with the same key that already exists in memcached. The memc:append("dog", {"a ", {"kind of"}, " animal"}) is functionally equivalent to memc:append("dog", "a kind of animal") The The In case of success, returns prepend
Prepends the value to an entry with the same key that already exists in memcached. The memc:prepend("dog", {"a ", {"kind of"}, " animal"}) is functionally equivalent to memc:prepend("dog", "a kind of animal") The The In case of success, returns get
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, If the entry is not found, then three 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 In case of errors, gets
Just like the This method is usually used together with the cas
Just like the The touch
Update the expiration time of an existing key. Returns This method was first introduced in the flush_all
Flushes (or invalidates) all the existing entries in the memcached server immediately (by default) or after the expiration
specified by the In case of success, returns delete
Deletes the key from memcached immediately. The key to be deleted must already exist in memcached. In case of success, returns incr
Increments the value of the specified key by the integer value specified in the Returns the new value after incrementation in success, and decr
Decrements the value of the specified key by the integer value specified in the Returns the new value after decrementation in success, and stats
Returns memcached server statistics information with an optional In case of success, this method returns a lua table holding all of the lines of the output; in case of failures, it returns If the version
Returns the server version number, like In case of error, it returns quit
Tells the server to close the current memcached connection. Returns Generally you can just directly call the verbosity
Sets the verbosity level used by the memcached server. The Returns init_pipeline
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 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. 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. Automatic Error LoggingBy default the underlying ngx_lua 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's lua_socket_log_errors directive, that is, lua_socket_log_errors off; Limitations
TODO
AuthorYichun "agentzh" Zhang (章亦春) [email protected], OpenResty Inc. Copyright and LicenseThis module is licensed under the BSD license. Copyright (C) 2012-2017, by Yichun "agentzh" Zhang (章亦春) [email protected], 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:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. See Also
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论