# Using Cachex in Phoenix - working example?

**URL:** <https://forum.elixirforum.com/t/using-cachex-in-phoenix-working-example/12458>\
**Category:** Questions\
**Tags:** cache, phoenix\
**Created:** [February 12, 2018, 11:44pm UTC](https://forum.elixirforum.com/t/using-cachex-in-phoenix-working-example/12458 "2018-02-12T23:44:35Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![OvermindDL1](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/overminddl1/32/2677_2.png) [@OvermindDL1](https://forum.elixirforum.com/u/OvermindDL1)\
**Post date:** [February 13, 2018, 7:30pm UTC](https://forum.elixirforum.com/t/using-cachex-in-phoenix-working-example/12458/9 "2018-02-13T19:30:57Z")

</div>

> [@fireproofsocks](#):
>
> Ah, ok, that makes sense. I think I should submit a PR for the docs on that.

It’s already documented at:

> **[Cachex — Cachex v4.1.1](https://cachex.hexdocs.pm/Cachex.html)**

🙂

> [@fireproofsocks](#):
>
> That works, but I’m open to any comments/improvements/suggestions. Not sure how idiomatic that solution is. Thanks!

It works fine but not very idiomatic, I’d personally do it like (well I generally have the fallback built into the cache rather than on each call, but ignoring that part):

```elixir
  def get_cached_thing!(slug) do
    Cachex.get(:my_cache, slug, fallback: fn(slug) ->
        Repo.get_by!(Mything, slug: slug)
    end)
    |> case do
      {:error, _} -> nil # It's not common to return error strings, rather you'd probably want an error tuple
      {success, value} when success in [:ok, :loaded] -> value
    end
  end

```

Or maybe using ok/error tuples, whichever. 🙂

---

_[View the full topic](https://forum.elixirforum.com/t/using-cachex-in-phoenix-working-example/12458)._
