Lua script context API - Cache sub-context
cache
Cache operations for storing and retrieving values from named in-memory caches.
Caches must be defined in the configuration using the caches clause
before they can be used from scripts. Each cache is a key-value store where keys
map to one or more values, with optional TTL-based expiration.
Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
get(cache, key) | cache name, key | value? | Get first cached value |
get_all(cache, key) | cache name, key | values? | Get all cached values |
set(cache, key, value, timeout?) | cache, key, value, optional TTL (ms) | - | Set cached value |
set_values(cache, key, values, timeout?) | cache, key, values array, optional TTL (ms) | - | Set multiple values |
append(cache, key, value, timeout?) | cache, key, value, optional TTL (ms) | - | Append to cached values |
append_values(cache, key, values, timeout?) | cache, key, values, optional TTL (ms) | - | Append multiple values |
remove(cache, key) | cache name, key | - | Remove cached values |
The optional timeout parameter specifies the TTL (time-to-live) in milliseconds.
If omitted, the cache's default timeout from its configuration is used.
Example
local context, previous = ...
local cache = context.cache
-- Store a value with 5 minute TTL (300000 ms)
cache:set("session_cache", "user123", "session_data", 300000)
-- Retrieve value
local data = cache:get("session_cache", "user123")
-- Remove when done
cache:remove("session_cache", "user123")
return previous