Kantoor.Bodies (Kantoor v1.0.0)

Bounded cache of raw message bodies. Karta stays the system of record; this table only saves a round trip for mail that was pushed with its bytes (and holds drafts, which Karta never sees).

Config (config :kantoor, Kantoor.Bodies): :budget_bytes (default 2 GiB, the compressed bytes kept per instance), :sweep_interval_ms (default 60_000, or :infinity to disable the timer).

Summary

Functions

The configured budget in compressed bytes.

Drops the cached body of a message (e.g. after expunge).

Read-through fetch. A miss asks Karta for the raw bytes and caches them. Returns {:error, :unavailable} when Karta cannot deliver; never raises.

A cache hit as {:ok, raw} (refreshing its LRU time), else :miss.

Caches raw for message_id. Never raises: a failure (or a body that alone exceeds the budget) just means the next read goes to Karta. Options: pinned: true for bodies that exist nowhere else.

Evicts least-recently-read unpinned bodies until total_bytes/0 is within budget. Returns the number of evicted bodies.

Sweeper interval in ms (or :infinity).

Total compressed bytes in the cache.

Functions

budget()

@spec budget() :: non_neg_integer()

The configured budget in compressed bytes.

delete(message_id)

@spec delete(Ecto.UUID.t()) :: :ok

Drops the cached body of a message (e.g. after expunge).

fetch(message)

@spec fetch(Kantoor.Mailboxes.Message.t()) :: {:ok, binary()} | {:error, :unavailable}

Read-through fetch. A miss asks Karta for the raw bytes and caches them. Returns {:error, :unavailable} when Karta cannot deliver; never raises.

get(message_id)

@spec get(Ecto.UUID.t()) :: {:ok, binary()} | :miss

A cache hit as {:ok, raw} (refreshing its LRU time), else :miss.

put(message_id, raw, opts \\ [])

@spec put(Ecto.UUID.t(), binary(), keyword()) :: :ok | :skipped | {:error, term()}

Caches raw for message_id. Never raises: a failure (or a body that alone exceeds the budget) just means the next read goes to Karta. Options: pinned: true for bodies that exist nowhere else.

sweep(budget \\ budget())

@spec sweep(non_neg_integer()) :: non_neg_integer()

Evicts least-recently-read unpinned bodies until total_bytes/0 is within budget. Returns the number of evicted bodies.

sweep_interval()

@spec sweep_interval() :: pos_integer() | :infinity

Sweeper interval in ms (or :infinity).

total_bytes()

@spec total_bytes() :: non_neg_integer()

Total compressed bytes in the cache.