feat: add server cache
This commit is contained in:
parent
c174e5b703
commit
8355f30948
2 changed files with 67 additions and 0 deletions
31
lib/todo/cache.ex
Normal file
31
lib/todo/cache.ex
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
defmodule Todo.Cache do
|
||||
use GenServer
|
||||
|
||||
@impl GenServer
|
||||
def init(_init_args) do
|
||||
{: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 ->
|
||||
new_process = Todo.Server.start()
|
||||
|
||||
new_state = Map.put(state, name, new_process)
|
||||
|
||||
{:reply, new_process, new_state}
|
||||
end
|
||||
end
|
||||
|
||||
def start() do
|
||||
GenServer.start(__MODULE__, nil)
|
||||
end
|
||||
|
||||
def server_process(pid, name) do
|
||||
GenServer.call(pid, {:server_process, name})
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue