A simple in-memory response cache plugin using ETS.
Caches agent run results keyed by user content, with configurable TTL and maximum cache size with LRU-style eviction.
Configuration
# Default — 5-minute TTL, hash of user message as key
ADK.Plugin.register({ADK.Plugin.Cache, []})
# Custom — 1-minute TTL, custom key function, max 500 entries
ADK.Plugin.register({ADK.Plugin.Cache,
ttl_ms: 60_000,
max_size: 500,
key_fn: fn context -> {context.user_id, context.user_content} end
})Options
:ttl_ms— cache entry TTL in milliseconds. Default300_000(5 minutes).:key_fn— functioncontext -> keyfor cache lookup. Default uses:erlang.phash2/1on the user message text.:max_size— max entries before oldest eviction. Default1000.
Behaviour
init/1— creates an ETS table with a unique name.before_run/2— computes cache key; returns cached events on hit (not expired).after_run/3— stores result in cache with timestamp; evicts oldest if over max size.
Summary
Types
@type config() :: [ ttl_ms: pos_integer(), key_fn: (ADK.Context.t() -> term()), max_size: pos_integer() ]
@type state() :: %{ ttl_ms: pos_integer(), key_fn: (ADK.Context.t() -> term()), max_size: pos_integer(), table: :ets.tid() }