# `SystemOneDSL.Cache`
[🔗](https://github.com/boxxxie/jev_dsl/blob/v1.0.0/lib/system_one_dsl/cache.ex#L1)

Deterministic identity hashing, confidence-aware cacheability rules, and pluggable
two-tier (L1 fast key-value + L2 durable store) caching behaviour for `SystemOneDSL`.

## Cache Key Invariance & Auto-Invalidation

Every cache key (`"jev_<sha256>"`) is derived deterministically from:
1. The recursively canonicalized input `state` map (`canonicalize/1`).
2. The compile-time `%SystemOneDSL.Prepared{fingerprint: fp}` SHA-256 digest of the contract's
   instructions, question types, and criteria.
3. The resolved `ReqLLM` model spec (default `"typesafe:jev-latest"`), preventing
   cross-model collisions across providers or model versions.

## Telemetry Events

`SystemOneDSL.Cache` emits the following `:telemetry` events:
* `[:system_one_dsl, :cache, :hit]`
* `[:system_one_dsl, :cache, :miss]`
* `[:system_one_dsl, :cache, :write]`

# `batch_entry`

```elixir
@type batch_entry() :: {cache_key(), atom() | String.t(), map(), term()}
```

# `cache_key`

```elixir
@type cache_key() :: String.t()
```

# `get`

```elixir
@callback get(key :: cache_key(), opts :: keyword()) ::
  {:ok, term()} | :miss | {:error, term()}
```

# `get_many`
*optional* 

```elixir
@callback get_many(keys :: [cache_key()], opts :: keyword()) :: %{
  optional(cache_key()) =&gt; term()
}
```

# `put`

```elixir
@callback put(key :: cache_key(), value :: term(), opts :: keyword()) ::
  :ok | {:error, term()}
```

# `put_many`
*optional* 

```elixir
@callback put_many(entries :: [batch_entry()], opts :: keyword()) ::
  :ok | {:error, term()}
```

# `adapter_get`

```elixir
@spec adapter_get(module() | nil, cache_key(), keyword()) :: {:ok, term()} | :miss
```

Reads a key from the given `adapter` module implementing `SystemOneDSL.Cache`.

# `adapter_get_many`

```elixir
@spec adapter_get_many(module() | nil, [cache_key()], keyword()) :: %{
  optional(cache_key()) =&gt; term()
}
```

Batch-reads `keys` from `adapter` using `get_many/2` if exported, falling back to `adapter_get/3`.

# `adapter_put`

```elixir
@spec adapter_put(module() | nil, cache_key(), term(), keyword()) :: :ok
```

Writes a key/value to the given `adapter` module if `resolve_ttl/2` approves caching.

# `adapter_put_many`

```elixir
@spec adapter_put_many(module() | nil, [batch_entry()], keyword()) :: :ok
```

Batch-writes `[batch_entry()]` to an adapter via `put_many/2` if exported, falling back to `put/3`.

# `cacheable?`

```elixir
@spec cacheable?(term(), keyword()) :: boolean()
```

Returns `true` if a `%ReqLLM.Response{}` (or plain map double) meets the minimum
confidence threshold for long-lived caching (`:min_confidence`, default `0.65`).

# `canonicalize`

```elixir
@spec canonicalize(term()) :: term()
```

Recursively normalizes structs, maps, lists, and tuples into a canonical form with string keys
for deterministic binary serialization.

# `hash_identity`

```elixir
@spec hash_identity(String.t(), map()) :: cache_key()
```

Computes a deterministic `<prefix>_<sha256>` key for any map structure.

# `identity`

```elixir
@spec identity(term(), SystemOneDSL.Prepared.t() | String.t() | term(), keyword()) ::
  map()
```

Builds the canonical model-aware identity map combining the resolved `ReqLLM` model,
evaluation `state`, and contract `fingerprint`.

# `key`

```elixir
@spec key(term(), SystemOneDSL.Prepared.t() | String.t() | term(), keyword()) ::
  cache_key()
```

Computes a deterministic `"jev_<sha256>"` cache key from `state` and a `%SystemOneDSL.Prepared{}`
contract (or fingerprint string / contract key with `:contract_module`).

# `persistable?`

```elixir
@spec persistable?(term(), keyword()) :: boolean()
```

Returns `true` if the evaluation result is a valid `%ReqLLM.Response{}` (or cacheable double)
eligible for durable Tier-2 persistence.

# `resolve_ttl`

```elixir
@spec resolve_ttl(term(), keyword()) :: {:ok, pos_integer()} | :skip
```

Determines the appropriate TTL in seconds for an evaluation result (`:ok` tuple),
or `:skip` if the result is an error that should not be cached.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
