]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lua cache.* fixes
authorVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 8 Feb 2017 12:13:57 +0000 (13:13 +0100)
committerVladimír Čunát <vladimir.cunat@nic.cz>
Wed, 8 Feb 2017 13:10:40 +0000 (14:10 +0100)
- docs: fix cache.current_* since long ago d5272b4
- don't allow "cache.foo = 'bar'" for abitrary foo
- restore cache['nic.cz'] after b31bad2ccf while not breaking completion
- #cache won't work on lua 5.1, so remove it

daemon/README.rst
daemon/lua/sandbox.lua

index c6365f1baa02cdaa3bb269daea79ef851f8ea99e..8d62ab64868ebefbf3aea8dc6f6b720444bc50ab 100644 (file)
@@ -711,30 +711,44 @@ The daemon provides an interface for dynamic loading of :ref:`daemon modules <mo
 Cache configuration
 ^^^^^^^^^^^^^^^^^^^
 
-The cache in Knot DNS Resolver is persistent with LMDB backend, this means that the daemon doesn't lose
+The default cache in Knot DNS Resolver is persistent with LMDB backend, this means that the daemon doesn't lose
 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.
 
 .. envvar:: cache.size (number)
 
-   Get/set the cache maximum size in bytes. Note that this is only a hint to the backend,
+   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.current_size (number)
+
+   Get the maximum size in bytes.
+
+   .. code-block:: lua
+
+       print(cache.current_size)
+
 .. envvar:: cache.storage (string)
 
-   Get or change the cache storage backend configuration, see :func:`cache.backends()` for
+   Set 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://.'
 
+.. envvar:: cache.current_storage (string)
+
+   Get the storage backend configuration.
+
+   .. code-block:: lua
+
+       print(cache.storage)
+
 .. function:: cache.backends()
 
    :return: map of backends
index feb57e33d3fe1bf426ded006e14d5845a31a5481..f76533521bb8dcd5849a4448cabc5d3efe57072c 100644 (file)
@@ -106,30 +106,26 @@ setmetatable(modules, {
 })
 
 -- Syntactic sugar for cache
--- `#cache -> cache.count()`
 -- `cache[x] -> cache.get(x)`
 -- `cache.{size|storage} = value`
 setmetatable(cache, {
-       __len = function (t)
-               return t.count()
-       end,
        __index = function (t, k)
-               if type(k) == 'number' then
-                       return rawget(t, k) or (rawget(t, 'current_size') and t.get(k))
-               end
+               local res = rawget(t, k)
+               if res and not rawget(t, 'current_size') then return res end
+               -- Beware: t.get returns empty table on failure to find.
+               -- That would be confusing here (breaking kresc), so return nil instead.
+               res = t.get(k)
+               if res and next(res) ~= nil then return res else return nil end
        end,
        __newindex = function (t,k,v)
                -- Defaults
-               if type(k) == number then
-                       local storage = rawget(t, 'current_storage')
-                       if not storage then storage = 'lmdb://' end
-                       local size = rawget(t, 'current_size')
-                       if not size then size = 10*MB end
-               end
+               local storage = rawget(t, 'current_storage')
+               if not storage then storage = 'lmdb://' end
+               local size = rawget(t, 'current_size')
+               if not size then size = 10*MB end
                -- Declarative interface for cache
                if     k == 'size'    then t.open(v, storage)
-               elseif k == 'storage' then t.open(size, v)
-               else   rawset(t, k, v) end
+               elseif k == 'storage' then t.open(size, v) end
        end
 })