ADK.Plugin.Cache (ADK v0.0.1-alpha.1)

Copy Markdown View Source

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. Default 300_000 (5 minutes).
  • :key_fn — function context -> key for cache lookup. Default uses :erlang.phash2/1 on the user message text.
  • :max_size — max entries before oldest eviction. Default 1000.

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

config()

@type config() :: [
  ttl_ms: pos_integer(),
  key_fn: (ADK.Context.t() -> term()),
  max_size: pos_integer()
]

state()

@type state() :: %{
  ttl_ms: pos_integer(),
  key_fn: (ADK.Context.t() -> term()),
  max_size: pos_integer(),
  table: :ets.tid()
}