Child spec for Wraper module of Cachex

This my application

use Application

  @impl true
  def start(_type, _args) do
    children = [
      Getmsg.Repo,
      {Cachex, name: :some_name}
    ]

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

I want change {Cachex, name: :some_name} to Getmsg.Cache, where Getmsg.Cache is wraper for Cachex module.

From documentation of Supervisor i’ve learned that supervisor will invoke method child_spec/1. I implemented this method

defmodule Getmsg.Cache do
  def child_spec([]) do
    %{
      id: Getmsg.Cache,
      start: {Cachex, :start_link, [name: :getmsg_cache]}
    }
  end
end

But i’m getting error:

** (Mix) Could not start application getmsg: Getmsg.Application.start(:normal, []) returned an error: shutdown: failed to start child: Getmsg.Cache
    ** (EXIT) :invalid_name

What’s i’m doing wrong?

2 Likes

The easiest way, would be to define your child_spec/1 using Cachex.child_spec/1 like that:

defmodule Getmsg.Cache do
  def child_spec(_opts) do
    Cachex.child_spec(name: :some_name)
  end
end
3 Likes