elixir-todo-list/lib/todo/cache.ex

26 lines
590 B
Elixir
Raw Permalink Normal View History

2024-08-04 13:56:50 +02:00
defmodule Todo.Cache do
2024-08-07 10:33:19 +02:00
use DynamicSupervisor
2024-08-04 13:56:50 +02:00
2024-08-07 10:33:19 +02:00
def start_link(init_arg) do
2024-08-07 08:21:57 +02:00
IO.puts("Starting #{__MODULE__}")
2024-08-07 10:33:19 +02:00
DynamicSupervisor.start_link(__MODULE__, init_arg, name: __MODULE__)
2024-08-04 13:56:50 +02:00
end
2024-08-07 10:33:19 +02:00
@impl DynamicSupervisor
def init(_) do
DynamicSupervisor.init(strategy: :one_for_one)
2024-08-04 13:56:50 +02:00
end
2024-08-07 10:33:19 +02:00
def server_process(todo_list_name) do
case start_child(todo_list_name) do
{:ok, pid} -> pid
{:error, {:already_started, pid}} -> pid
end
2024-08-04 13:56:50 +02:00
end
2024-08-07 10:33:19 +02:00
defp start_child(todo_list_name) do
DynamicSupervisor.start_child(__MODULE__, {Todo.Server, todo_list_name})
2024-08-04 13:56:50 +02:00
end
end