lua-resty-memcached-shdict - Powerful memcached client with a shdict caching layer and many other features
Experimental.
local shdict_memc = require "resty.memcached.shdict"
local function dlog(ctx, ...)
ngx.log(ngx.DEBUG, "my app: ", ...)
end
local function error_log(ctx, ...)
ngx.log(ngx.ERR, "my app: ", ...)
end
local function warn(ctx, ...)
ngx.log(ngx.WARN, "my app: ", ...)
end
local memc_fetch, memc_store =
shdict_memc.gen_memc_methods{
tag = "my memcached server tag",
debug_logger = dlog,
warn_logger = warn,
error_logger = error_log,
locks_shdict_name = "some_lua_shared_dict_name",
shdict_set = meta_shdict_set, -- generated by lua-resty-shdict-simple or
-- any other API compatible function factories
shdict_get = meta_shdict_get, -- ditto
disable_shdict = false, -- optional, default false
memc_host = "127.0.0.1",
memc_port = 11211,
memc_timeout = 200, -- in ms
memc_conn_pool_size = 5,
memc_fetch_retries = 2, -- optional, default 1
memc_fetch_retry_delay = 100, -- in ms, optional, default to 100 (ms)
memc_conn_max_idle_time = 10 * 1000, -- in ms, for in-pool connections,
-- optional, default to nil
memc_store_retries = 2, -- optional, default to 1
memc_store_retry_delay = 100, -- in ms, optional, default to 100 (ms)
store_ttl = 1, -- in seconds, optional, default to 0 (i.e., never expires)
}
-- on hot code paths:
local ctx = ngx.ctx
-- in case of failure, can return the stale data with an error message
local data, err = memc_fetch(ctx, key)
-- using the default ttl specified by "store_ttl"
local ok = memc_store(ctx, key, value)
ok = memc_store(ctx, key, value, ttl) -- overriding the default ttl
This library provides a powerful memcached client that deliver the following important features:
- Automatically using shm-based caching layer with OpenResty's lua_shared_dict.
This local shm cache layer can also be turned off by setting the
disable_shdict
totrue
. - Automatically using server-level (not worker-level) cache locks (based on lua-resty-lock) to avoid duplicate and concurrent queries to the memcached server for the same key when it is a cache miss in the local shm cache.
- Automatically handles and logs any errors while accessing the Memcached server or
lua_shared_dict
. - Automatically uses connection pools for all the Memcached queries.
- Automatically returns stale (or expired) data item in the local shm cache when a cache miss happens and some other light threads are already querying Memcached and updating the cache for the same key.
- Automatically retry querying Memcached when failures happen (like intermittent network issues).
This library is mostly used for cases that use Memcached or Memcached-compatible servers as the data storage (like Kyoto Tycoon).
Atop this library, the user can further add another per-worker caching layer by employing the lua-resty-lrucache library herself, and/or add asynchronous cache updating mechanism via ngx.timer.at.
- Unlike the lua-resty-memcached library, this client does not escape special characters in the key by default. So it is the caller's responsibility to avoid using special characters in the keys.
This library depends on the following Lua libraries:
- Add a test suite under
t/
.
Yichun "agentzh" Zhang (章亦春) [email protected], CloudFlare Inc.
This module is licensed under the BSD license.
Copyright (C) 2016, by CloudFlare Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
-
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.