# `CRAM.Reference.Cache`
[🔗](https://github.com/baoulo/cram-elixir/blob/v0.1.0/lib/cram/reference/cache.ex#L1)

Checksum-addressed cache in a directory the caller created.

Files are named with the 32-digit MD5. A cache hit is hashed again before
the bases are returned. Sequence bytes are not written into errors.

# `t`

```elixir
@type t() :: %CRAM.Reference.Cache{directory: Path.t(), upstream: term()}
```

A checksum-named cache in front of another provider.

  * `:directory` — an existing directory. Files are named by the MD5
  * `:upstream` — a `CRAM.ReferenceProvider` consulted on a miss

    iex> {:ok, cache} = CRAM.Reference.Cache.new(System.tmp_dir!(), :unused)
    iex> cache.directory == System.tmp_dir!()
    true

# `fetch`

Return `name` from the cache, or from `upstream` on a miss.

`expected_md5` must be 32 hex digits and is the cache filename. A hit is
hashed again before it is returned. A checksum that is not 32 hex digits
is `reason: :reference_checksum_mismatch` and is not used as a path.
Upstream errors pass through. Bases are not written into the error.

    iex> dir = Path.join(System.tmp_dir!(), "cram-doctest-#{System.unique_integer([:positive])}")
    iex> File.mkdir_p!(dir)
    :ok
    iex> {:ok, local} = CRAM.Reference.Local.new("tutorials/fixtures/sq0.fa")
    iex> {:ok, cache} = CRAM.Reference.Cache.new(dir, local)
    iex> CRAM.Reference.Cache.fetch(cache, "sq0", "be19336b7e15968f7ac7dc82493d9cd8")
    {:ok, "TTCACCCA"}
    iex> File.rm_rf!(dir) && :ok
    :ok

# `new`

```elixir
@spec new(Path.t(), term()) :: {:ok, t()} | {:error, CRAM.Error.t()}
```

Cache accepted sequences in `directory`, which the caller must create.

`upstream` is any `CRAM.ReferenceProvider`. Returns `{:ok, cache}` when
`directory` exists. A missing directory is `reason: :reference_missing`.
Does not raise and does not create the directory.

    iex> {:ok, cache} = CRAM.Reference.Cache.new(System.tmp_dir!(), :unused)
    iex> is_binary(cache.directory)
    true
    iex> {:error, error} = CRAM.Reference.Cache.new("test/fixtures/missing-cache", :unused)
    iex> error.reason
    :reference_missing

---

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