Getting cachex up and running in my phoenix app

So I installed cachex in my phoenix app by adding it to my deps and performing a mix deps.get.

Now in the docs it says to " 2. Ensure cachex is started before your application:"

I’m not sure how to make sure it starts before my application, I have this so far and it is after my app starts:

  def application do
    [
      mod: {Realtime.Application, []},
      extra_applications: [:cachex, :logger, :runtime_tools, :extwitter]
    ]
  end

The 2nd part of the install is:

The typical use of Cachex is to set up using a Supervisor, so that it can be handled automatically:

Supervisor.start_link(
[ worker(Cachex, [:my_cache, ]) ]
)

So this would be the application.ex file. Do I add to the children array?

def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Start the Ecto repository
      Realtime.Repo,
      # Start the endpoint when the application starts
      RealtimeWeb.Endpoint,
      # Starts a worker by calling: Realtime.Worker.start_link(arg)
      # {Realtime.Worker, arg},
      RealtimeWeb.Presence
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Realtime.Supervisor]
    Supervisor.start_link(children, opts)
  end

Thanks!

1 Like

I added it to the children list like so:

def start(_type, _args) do
    # List all child processes to be supervised
    children = [
      # Start the Ecto repository
      Realtime.Repo,
      # Start the endpoint when the application starts
      RealtimeWeb.Endpoint,
      # Starts a worker by calling: Realtime.Worker.start_link(arg)
      # {Realtime.Worker, arg},
      RealtimeWeb.Presence,
      # Start cachex
      {Cachex, :cache_name_here},
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: Realtime.Supervisor]
    Supervisor.start_link(children, opts)
  end
3 Likes

I tried that but it didn’t work for me (but I was adding the options also).

Any idea on how to pass the options also?

I tried:

{Cachex, [:my_cache, []]}

Try:

{Cachex, :my_cache, [ commands: [last: ...], limit: ..., ... ]}

1 Like

You should try exactly as it is: {Cachex, :my_cache}