Skip to content

Instantly share code, notes, and snippets.

@fsword
Created August 15, 2014 09:29
Show Gist options
  • Save fsword/574acd9f93c4c3cbf3af to your computer and use it in GitHub Desktop.
Save fsword/574acd9f93c4c3cbf3af to your computer and use it in GitHub Desktop.
exactor
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
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