Using Cachex inside a Plug

I’m exploring using Cachex to implement a simple one time password (OTP). In its simplest form, there is a custom Plug that looks like the following:

defmodule MyAppWeb.OTP do
  import Plug.Conn

  def init(options), do: options

  def call(%Plug.Conn{path_params: path_params} = conn, opts) do
    access_code = path_params["code"]

    case Cachex.get(:passcode, :"#{access_code}") do
      {:ok, %{user_id: user_id}} -> ...

      _ ->
        conn |> send_resp(:unauthorized, "") |> halt()

The issue here is that Cachex.get(:passcode, :"#{access_code}" will always give me nil. Similarly, if I use Cachex.keys(:passcode), it will return an empty list while I’m sure there are keys that I had added via iex.

Here’s how Cachex is initialised in application.ex

  def start(_type, _args) do
    children = [
      # Start the Telemetry supervisor
      %{id: :cachex_passcode, start: {Cachex, :start_link, [[name: :passcode]]}},

I have a feeling there is something missing in the setup?

Hey @tbm206 can you show how you are adding these keys? Are you adding them inside a single terminal where you run an interactive phoenix server ie iex -S mix phx.server or do you have two different terminals, one with the server and the other with iex ?

I run phoenix in a docker container. I then exec into it and run iex -S mix. That has worked well with other tasks so far but maybe Cachex is different.

The exact command I use is:

Cachex.put(:passcode, :"#{uuid}", %{user_id: user_id}, ttl: :timer.minutes(30))

What’s confusing is that the :passcode cache is available in the interactive terminal.