# `Cache.Strategy`
[🔗](https://github.com/mikaak/elixir_cache/blob/main/lib/cache/strategy.ex#L1)

Behaviour for strategy-based cache adapters.

Strategy adapters compose over existing cache adapters to provide higher-level
caching patterns such as consistent hashing, multi-layer cascading, and
refresh-ahead semantics.

Unlike regular adapters which implement `Cache` directly, strategy adapters
receive the underlying adapter module and its resolved options so they can
delegate operations appropriately.

## Usage

Strategies are specified using the tuple format in `use Cache`:

```elixir
use Cache,
  adapter: {Cache.HashRing, Cache.ETS},
  name: :my_cache,
  opts: [read_concurrency: true]
```

The first element is the strategy module, the second is the underlying adapter
(or strategy-specific configuration).

# `child_spec`

```elixir
@callback child_spec(
  {cache_name :: atom(), strategy_config :: term(), adapter_opts :: Keyword.t()}
) ::
  Supervisor.child_spec() | :supervisor.child_spec()
```

Returns a supervisor child spec for the strategy.

Receives the cache name, strategy config (the second element of the adapter
tuple), and the resolved underlying adapter opts.

# `delete`

```elixir
@callback delete(
  cache_name :: atom(),
  key :: atom() | String.t(),
  strategy_config :: term(),
  adapter_opts :: Keyword.t()
) :: :ok | ErrorMessage.t()
```

Removes a value from the cache using the strategy's routing/layering logic.

# `get`

```elixir
@callback get(
  cache_name :: atom(),
  key :: atom() | String.t(),
  strategy_config :: term(),
  adapter_opts :: Keyword.t()
) :: ErrorMessage.t_res(any())
```

Fetches a value from the cache using the strategy's routing/layering logic.

# `opts_definition`

```elixir
@callback opts_definition() :: Keyword.t()
```

Returns the NimbleOptions schema for validating strategy-level opts.

# `put`

```elixir
@callback put(
  cache_name :: atom(),
  key :: atom() | String.t(),
  ttl :: pos_integer() | nil,
  value :: any(),
  strategy_config :: term(),
  adapter_opts :: Keyword.t()
) :: :ok | ErrorMessage.t()
```

Stores a value in the cache using the strategy's routing/layering logic.

# `strategy?`

```elixir
@spec strategy?(module()) :: boolean()
```

Returns true if the given module implements the `Cache.Strategy` behaviour.

---

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