Commit 8c8852c5 authored by Marek Vavruša's avatar Marek Vavruša

daemon/bindings: syntactic sugar for cache

the cache can be setup declaratively with:

cache.size = 10 * MB

the cache storage backend can be changed with:

cache.storage = ‘lmdb://.'
parent 969b1610
...@@ -335,6 +335,26 @@ The cache in Knot DNS Resolver is persistent with LMDB backend, this means that ...@@ -335,6 +335,26 @@ The cache in Knot DNS Resolver is persistent with LMDB backend, this means that
the cached data on restart or crash to avoid cold-starts. The cache may be reused between cache the cached data on restart or crash to avoid cold-starts. The cache may be reused between cache
daemons or manipulated from other processes, making for example synchronised load-balanced recursors possible. daemons or manipulated from other processes, making for example synchronised load-balanced recursors possible.
.. envvar:: cache.size (number)
Get/set the cache maximum size in bytes. Note that this is only a hint to the backend,
which may or may not respect it. See :func:`cache.open()`.
.. code-block:: lua
print(cache.size)
cache.size = 100 * MB -- equivalent to `cache.open(100 * MB)`
.. envvar:: cache.storage (string)
Get or change the cache storage backend configuration, see :func:`cache.backends()` for
more information. If the new storage configuration is invalid, it is not set.
.. code-block:: lua
print(cache.storage)
cache.storage = 'lmdb://.'
.. function:: cache.backends() .. function:: cache.backends()
:return: map of backends :return: map of backends
......
...@@ -356,7 +356,9 @@ static int cache_open(lua_State *L) ...@@ -356,7 +356,9 @@ static int cache_open(lua_State *L)
/* Select cache storage backend */ /* Select cache storage backend */
struct engine *engine = engine_luaget(L); struct engine *engine = engine_luaget(L);
unsigned cache_size = lua_tonumber(L, 1);
const char *conf = n > 1 ? lua_tostring(L, 2) : NULL; const char *conf = n > 1 ? lua_tostring(L, 2) : NULL;
const char *uri = conf;
struct storage_api *storage = cache_select_storage(engine, &conf); struct storage_api *storage = cache_select_storage(engine, &conf);
if (!storage) { if (!storage) {
format_error(L, "unsupported cache backend"); format_error(L, "unsupported cache backend");
...@@ -369,7 +371,7 @@ static int cache_open(lua_State *L) ...@@ -369,7 +371,7 @@ static int cache_open(lua_State *L)
kr_cache_close(engine->resolver.cache); kr_cache_close(engine->resolver.cache);
} }
/* Reopen cache */ /* Reopen cache */
void *storage_opts = storage->opts_create(conf, lua_tointeger(L, 1)); void *storage_opts = storage->opts_create(conf, cache_size);
engine->resolver.cache = kr_cache_open(storage_opts, engine->pool); engine->resolver.cache = kr_cache_open(storage_opts, engine->pool);
free(storage_opts); free(storage_opts);
if (engine->resolver.cache == NULL) { if (engine->resolver.cache == NULL) {
...@@ -377,6 +379,15 @@ static int cache_open(lua_State *L) ...@@ -377,6 +379,15 @@ static int cache_open(lua_State *L)
lua_error(L); lua_error(L);
} }
/* Store current configuration */
lua_getglobal(L, "cache");
lua_pushstring(L, "size");
lua_pushnumber(L, cache_size);
lua_rawset(L, -3);
lua_pushstring(L, "storage");
lua_pushstring(L, uri);
lua_rawset(L, -3);
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
return 1; return 1;
} }
......
...@@ -106,7 +106,7 @@ void *namedb_lmdb_mkopts(const char *conf, size_t maxsize) ...@@ -106,7 +106,7 @@ void *namedb_lmdb_mkopts(const char *conf, size_t maxsize)
struct namedb_lmdb_opts *opts = malloc(sizeof(*opts)); struct namedb_lmdb_opts *opts = malloc(sizeof(*opts));
if (opts) { if (opts) {
memset(opts, 0, sizeof(*opts)); memset(opts, 0, sizeof(*opts));
opts->path = conf ? conf : "."; opts->path = (conf && strlen(conf)) ? conf : ".";
opts->mapsize = maxsize; opts->mapsize = maxsize;
} }
return opts; return opts;
......
...@@ -40,6 +40,16 @@ setmetatable(modules, { ...@@ -40,6 +40,16 @@ setmetatable(modules, {
end end
}) })
-- Syntactic sugar for cache
-- `cache.{size|storage} = value`
setmetatable(cache, {
__newindex = function (t,k,v)
if k == 'size' then t.open(v, rawget(t, 'storage'))
elseif k == 'storage' then t.open(rawget(t, 'size'), v)
else rawset(t, k, v) end
end
})
-- Register module in Lua environment -- Register module in Lua environment
function modules_register(module) function modules_register(module)
-- Syntactic sugar for get() and set() properties -- Syntactic sugar for get() and set() properties
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment