# `Shardex.Strategy.Cached`
[🔗](https://github.com/epinault/shardex/blob/v0.1.0/lib/shardex/strategy/cached.ex#L1)

Caches another strategy's results in ETS. Useful for directory strategies
that query a database.

    strategy: {Shardex.Strategy.Cached, inner: MyApp.ShardDirectory, ttl: :timer.minutes(5)}

Options:

  * `:inner` (required) - the wrapped strategy (module, `{module, opts}` or function)
  * `:ttl` - entry lifetime in milliseconds or `:infinity` (default 5 minutes)
  * `:cache_errors` - also cache `{:error, _}` results (default `false`)

Only cache misses reach the inner strategy, in one `route_many/2` call.
The cache is cleared on `add_shard`/`remove_shard`; use `Shardex.invalidate/2`
when an assignment changes.

Call `Shardex.invalidate/2` only after the new assignment is visible to the
inner strategy. Invalidation is not atomic with lookups: a lookup that missed
the cache concurrently can re-cache the old value after `invalidate/2`, where
it stays until `:ttl` expires. Choose `:ttl` to bound how long a stale route
is acceptable.

Expired entries are only replaced when their key is looked up again, so
memory grows with the number of distinct keys routed. If that matters, use a
bounded key space, or a finite `:ttl` plus a periodic `Shardex.invalidate(instance, :all)`.

---

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