ETS with GenServer.call return (EXIT) no process: the process is not alive or there's no process currently associated with the given name

Hi, Please I need help.

When I call
Cache.put("google.com", "foo bar")
I got
:ok

when I call
Cache.get("google.com")
I got error
(EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

I don’t know what am doing wrong please help me.

use GenServer

  def start_link do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  @table_name :cache_table

  @impl true
  def init(state) do
    :ets.new(@table_name, [:named_table, :set, :public, read_concurrency: true])
    {:ok, state}
  end

  def get(url) do
    GenServer.call(__MODULE__, {:get, url})
  end

  def put(url, data) do
    GenServer.cast(__MODULE__, {:put, url, data})
  end

  @impl true
  def handle_call({:get, url}, _from, state) do
    reply =
      case :ets.lookup(@table_name, url) do
        [{^url, data}] ->
          {:ok, data}

        [] ->
          :error
      end

    {:reply, reply, state}
  end

  def handle_cast({:put, url, data}, _from, state) do
    :ets.insert(@table_name, {url, data})
    {:noreply, state}
  end

cast/2 succeeding is no indication that the GenServer is alive.

https://hexdocs.pm/elixir/1.17.2/GenServer.html#cast/2

How did you start the GenServer?

GenServer.handle_cast/2 is of arity 2, unlike GenServer.handle_call/3, which also contains the parameter allowing to respond later (namely from, the second parameter.)

Your GenServer.cast/2 call basically kills the server because the clause for handle_cast/2 is mandatory (unlike handle_info/2.)

@derek-zhou Thank you I appreciate.

@mudasobwa Thank you I appreciate I figured it out.