2024-08-04 13:56:50 +02:00
|
|
|
defmodule Todo.Cache do
|
|
|
|
use GenServer
|
|
|
|
|
|
|
|
@impl GenServer
|
|
|
|
def init(_init_args) do
|
2024-08-07 08:21:57 +02:00
|
|
|
IO.puts("Starting #{__MODULE__}")
|
|
|
|
|
2024-08-04 13:56:50 +02:00
|
|
|
{:ok, %{}}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl GenServer
|
|
|
|
def handle_call({:server_process, name}, _from, state) do
|
|
|
|
case Map.fetch(state, name) do
|
|
|
|
{:ok, process} ->
|
|
|
|
{:reply, process, state}
|
|
|
|
|
|
|
|
:error ->
|
2024-08-07 10:03:31 +02:00
|
|
|
{:ok, new_process} = Todo.Server.start_link(name)
|
2024-08-04 13:56:50 +02:00
|
|
|
|
|
|
|
new_state = Map.put(state, name, new_process)
|
|
|
|
|
|
|
|
{:reply, new_process, new_state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-07 08:21:57 +02:00
|
|
|
def start_link(_) do
|
|
|
|
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
|
2024-08-04 13:56:50 +02:00
|
|
|
end
|
|
|
|
|
2024-08-07 08:21:57 +02:00
|
|
|
def server_process(name) do
|
|
|
|
GenServer.call(__MODULE__, {:server_process, name})
|
2024-08-04 13:56:50 +02:00
|
|
|
end
|
|
|
|
end
|