Update: Finished persistable todo server
This commit is contained in:
parent
09665c3b88
commit
55589a963f
80 changed files with 809 additions and 0 deletions
34
chapter7/persistable_todo_cache/lib/todo/cache.ex
Normal file
34
chapter7/persistable_todo_cache/lib/todo/cache.ex
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
defmodule Todo.Cache do
|
||||
use GenServer
|
||||
|
||||
def start() do
|
||||
GenServer.start(__MODULE__, nil)
|
||||
end
|
||||
|
||||
def server_process(cache_pid, todo_list_name) do
|
||||
GenServer.call(cache_pid, {:server_process, todo_list_name})
|
||||
end
|
||||
|
||||
@impl true
|
||||
def init(_) do
|
||||
Todo.Database.start()
|
||||
{:ok, %{}}
|
||||
end
|
||||
|
||||
@impl true
|
||||
def handle_call({:server_process, todo_list_name}, _, todo_servers) do
|
||||
case Map.fetch(todo_servers, todo_list_name) do
|
||||
{:ok, todo_server} ->
|
||||
{:reply, todo_server, todo_servers}
|
||||
|
||||
:error ->
|
||||
{:ok, new_todo_server} = Todo.Server.start(todo_list_name)
|
||||
|
||||
{
|
||||
:reply,
|
||||
new_todo_server,
|
||||
Map.put(todo_servers, todo_list_name, new_todo_server)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue