# `ImagePipe.Source.CachePolicy`
[🔗](https://github.com/hlindset/image_pipe/blob/v0.2.0/image_pipe/lib/image_pipe/source/cache_policy.ex#L1)

Cache storage and freshness policy for originals and processed images.

    ImagePipe.config(
      source_cache_policy: [storage: :origin, freshness: {:fallback, 300}],
      sources: [...]
    )

`:source_cache_policy` in `ImagePipe.config/1` sets the policy for every
source. A source's own `:cache_policy` replaces it field by field. All
durations are in seconds. See
[source cache settings](cache.md#source-cache-settings) and
[caching and freshness](caching-and-freshness.md).

An [immutable source](caching-and-freshness.md#immutable-sources) has no
freshness deadline, even when it inherits a finite `:freshness`, but still
needs `:storage` to allow storing. Its own `:cache_policy` can't set a
`:freshness` duration or a `:stale_while_revalidate` window.

## Options

* `:storage` (`:origin`, `:allow`, or `:deny`) - Whether originals and processed images may be stored. `:origin`, the default, stores unless the origin sends `no-store` or `private`. `:allow` stores despite them, and `:deny` never stores. `Vary: *` from the origin prevents storage in every mode.

* `:freshness` (`:origin`, `{:fallback, seconds}`, or `{:force, seconds}`) - How long a stored copy stays fresh. `:origin`, the default, uses the origin's lifetime and revalidates on every request when there is none. `{:fallback, seconds}` applies only when the origin sends no lifetime. `{:force, seconds}` replaces the origin's lifetime, including `no-cache`. Neither makes a response storable that `:storage` doesn't allow.

* `:stale_while_revalidate` (`:origin`, `:disabled`, or `{:force, seconds}`) - How long an expired copy may still be served while it is refreshed. `:origin`, the default, uses the origin's `stale-while-revalidate`, which an origin `no-cache`, `must-revalidate`, `proxy-revalidate`, or `s-maxage` turns off. `:disabled` serves no expired copies. `{:force, seconds}` sets the window, whatever the origin sends.

# `t`

```elixir
@type t() :: keyword()
```

# `merge`

```elixir
@spec merge(t(), t()) :: t()
```

Merges validated policies field by field, with source settings taking precedence.

# `validate`

```elixir
@spec validate(term()) :: {:ok, t()} | {:error, String.t()}
```

---

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