Raxol.Agent.Policy.Cache (Raxol Agent v2.6.0)

Copy Markdown View Source

Cache policy: short-circuit an operation when its :key_fn matches a cached value in the configured Raxol.Agent.Cache adapter.

Implementation: Raxol.Agent.PolicyApplier calls key_fn.(params) to compute the cache key. On hit ({:ok, value}), the wrapped operation does not run and the cached value is returned. On miss, the operation runs; on {:ok, value} the value is stored under the key with :ttl_ms and the result is returned. Errors are surfaced verbatim without caching.

Fields

  • storage -- {module, config} tuple resolved at construction by the .ets/1 and .postgrex/1 helpers
  • ttl_ms -- TTL in milliseconds; 0 means no expiry; must be >= 0
  • key_fn -- (params -> term()) that derives the cache key from the operation's input

Constructors

Policy.Cache.ets(ttl_ms: 300_000, key_fn: &(&1.user_id))
Policy.Cache.ets(ttl_ms: 60_000, key_fn: &(&1.id), table: :my_cache)
Policy.Cache.postgrex(ttl_ms: 60_000, key_fn: &(&1.id), conn: MyApp.Postgrex)

Bring-your-own adapter: pass :storage directly to the struct constructor for an adapter outside the two shipped defaults.

Summary

Functions

Construct a Cache policy backed by the Ets adapter. Required: :ttl_ms, :key_fn. Optional: :table (defaults to the Ets adapter's :raxol_agent_cache).

Construct a Cache policy backed by the Postgrex adapter. Required: :ttl_ms, :key_fn, :conn. Optional: :table.

Types

t()

@type t() :: %Raxol.Agent.Policy.Cache{
  key_fn: (term() -> term()),
  storage: {module(), map()},
  ttl_ms: non_neg_integer()
}

Functions

ets(opts)

@spec ets(keyword()) :: t()

Construct a Cache policy backed by the Ets adapter. Required: :ttl_ms, :key_fn. Optional: :table (defaults to the Ets adapter's :raxol_agent_cache).

postgrex(opts)

@spec postgrex(keyword()) :: t()

Construct a Cache policy backed by the Postgrex adapter. Required: :ttl_ms, :key_fn, :conn. Optional: :table.