Created
August 15, 2014 09:29
-
-
Save fsword/574acd9f93c4c3cbf3af to your computer and use it in GitHub Desktop.
exactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Registry1 do | |
use ExActor.GenServer | |
definit do: initial_state(HashDict.new) | |
defcast create(name), state: names, do: put_name(names,name) | |
defcall lookup(name), state: names, do: reply(HashDict.fetch(names,name)) | |
def put_name(names, name) do | |
if HashDict.get(names, name) do | |
new_state(names) | |
else | |
{:ok, bucket} = KV.Bucket.start_link() | |
new_state(HashDict.put(names, name, bucket)) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Registry2 do | |
use GenServer | |
def start_link(opts \\ []) do | |
GenServer.start_link(__MODULE__, :ok, opts) | |
end | |
def lookup(server, name) do | |
GenServer.call(server, {:lookup, name}) | |
end | |
def create(server, name) do | |
GenServer.cast(server, {:create, name}) | |
end | |
def init(:ok) do | |
{:ok, HashDict.new} | |
end | |
def handle_call({:lookup, name}, _from, names) do | |
{:reply, HashDict.fetch(names, name), names} | |
end | |
def handle_cast({:create, name}, names) do | |
if HashDict.get(names, name) do | |
{:noreply, names} | |
else | |
{:ok, bucket} = KV.Bucket.start_link() | |
{:noreply, HashDict.put(names, name, bucket)} | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment