Cache
Available since Nauthilus version 1.8.9.
The nauthilus_cache module provides a process-wide in-memory cache that can be accessed from any Lua script
(feature, backend, filter, action). Use it to store and reuse data across requests, aggregate values for batched
processing, or implement simple counters. Entries are thread-safe and can optionally expire via TTL.
dynamic_loader("nauthilus_cache")
local nauthilus_cache = require("nauthilus_cache")
Notes
- The cache is in-process and shared across all Lua requests in the same Nauthilus instance.
- Values are stored as simple Lua types or tables (numbers, strings, booleans, tables). Functions are not supported.
- Optional TTL can be provided per entry (in seconds). A TTL of 0 or nil means "no expiry".
- There is a background janitor cleaning up expired entries, plus lazy cleanup on read.
Functions
nauthilus_cache.cache_set
Stores a value under a key with optional TTL (in seconds).
Syntax
-- ttl_seconds is optional (nil or 0 = no expiry)
local result, error = nauthilus_cache.cache_set(key, value, ttl_seconds)
Parameters
key(string): Cache keyvalue(any table/primitive): Value to store (string/number/boolean/table)ttl_seconds(number, optional): Lifetime in seconds.nilor0means no expiration
Returns
result(string): "OK" on successerror(nil|string): Currently nil on success; reserved for future error use
Example
dynamic_loader("nauthilus_cache")
local cache = require("nauthilus_cache")
cache.cache_set("greeting", "hello", 60) -- expires after 60s
cache.cache_set("config", { retry = 3 }) -- no expiry
nauthilus_cache.cache_get
Retrieves a stored value if present and not expired.
Syntax
local value = nauthilus_cache.cache_get(key)
Parameters
key(string): Cache key
Returns
value(any|nil): The stored value, ornilif not present or expired
Example
local v = cache.cache_get("greeting")
if v then
-- use value
end
nauthilus_cache.cache_delete
Deletes a key from the cache.
Syntax
local removed = nauthilus_cache.cache_delete(key)
Parameters
key(string): Cache key to delete
Returns
removed(boolean):trueif the key existed and was removed; otherwisefalse
Example
local ok = cache.cache_delete("greeting")
nauthilus_cache.cache_exists
Checks whether a non-expired entry exists for the key.
Syntax
local present = nauthilus_cache.cache_exists(key)
Parameters
key(string): Cache key
Returns
present(boolean):trueif present and not expired; otherwisefalse
nauthilus_cache.cache_update
Atomically updates the value at a key by calling your provided Lua function with the current value. The function must be synchronous (no yields). The return value of your function becomes the new cached value.
Syntax
local new_value = nauthilus_cache.cache_update(key, updater_fn)
Parameters
key(string): Cache keyupdater_fn(function): Called asupdater_fn(old_value)and must return the new value to store
Returns
new_value(any): The value returned byupdater_fn, after storing it
Example
-- Increment a counter
local n = nauthilus_cache.cache_update("login_attempts", function(old)
old = old or 0
return old + 1
end)
nauthilus_cache.cache_keys
Returns a list (array table) of all non-expired keys.
Syntax
local keys = nauthilus_cache.cache_keys()
Returns
keys(table): Array of strings