Karta.DNS.Cache (Karta v1.0.0)

A bounded cache of DNS answers with TTLs, in ETS.

Reads go straight to a protected ETS table and never wait for the owning process; writes go through it, which keeps the size bound exact.

  • TTL clamps. An answer is kept for its TTL, raised to :min_ttl (default 5 s) and lowered to :max_ttl (default 86,400 s, one day).
  • Negative caching (RFC 2308): NXDOMAIN and NODATA answers are kept for the TTL the answer derived from its SOA (Karta.DNS.Answer), clamped to :min_ttl..:negative_max_ttl (default 900 s). A negative answer without an SOA has no TTL and is not cached (RFC 2308 §5). Errors (timeouts, SERVFAIL) are never cached.
  • Bounded size. At most :max_entries answers (default 10,000). When a new answer would exceed it, the entries closest to expiry are dropped first. Expired entries are swept every :sweep_interval milliseconds (default 60,000).

A cached answer comes back with the TTL it has left. Times are seconds of the monotonic clock; get/4 and put/3 take the current time so tests can move it.

Summary

Functions

Returns a specification to start this module under a supervisor.

A cached answer for the name and type with its remaining TTL, or :miss.

The current time on the cache's clock (monotonic seconds).

Caches an answer (under its name and type) unless it has no TTL.

The number of cached answers, expired ones included until the next sweep.

Starts a cache. :name (an atom) is required; it also names the ETS table.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

get(cache, name, type, now \\ now())

@spec get(atom(), String.t(), Karta.DNS.Record.type(), integer()) ::
  {:ok, Karta.DNS.Answer.t()} | :miss

A cached answer for the name and type with its remaining TTL, or :miss.

now()

@spec now() :: integer()

The current time on the cache's clock (monotonic seconds).

put(cache, answer, now \\ now())

@spec put(atom(), Karta.DNS.Answer.t(), integer()) :: :ok

Caches an answer (under its name and type) unless it has no TTL.

size(cache)

@spec size(atom()) :: non_neg_integer()

The number of cached answers, expired ones included until the next sweep.

start_link(opts)

Starts a cache. :name (an atom) is required; it also names the ETS table.