Skip to content

Instantly share code, notes, and snippets.

@micahboyd
Created April 29, 2018 23:29
Show Gist options
  • Select an option

  • Save micahboyd/8dd2e2d5bd9c4f28209536ef46da2e62 to your computer and use it in GitHub Desktop.

Select an option

Save micahboyd/8dd2e2d5bd9c4f28209536ef46da2e62 to your computer and use it in GitHub Desktop.
Elixir GenServer practice 2
defmodule KeyValueStore do
use GenServer
def start, do: GenServer.start(KeyValueStore, nil)
def put(pid, key, value) do
GenServer.cast(pid, {:put, key, value})
end
def get(pid, key) do
GenServer.call(pid, {:get, key})
end
def init(_), do: {:ok, %{}}
def handle_cast({:put, key, value}, state) do
{:no_reply, Map.put(state, key, value)}
end
def handle_call({:get, key}, _, state) do
{:reply, Map.get(state, key), state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment