Created
May 24, 2014 06:28
-
-
Save defp/7bfa0ed9a78aecc1e9c4 to your computer and use it in GitHub Desktop.
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 Kvs do | |
def store(key, value) do | |
rpc({:store, key, value}) | |
end | |
def start do | |
Process.register spawn(fn -> loop() end), :kvs | |
end | |
def lookup(key) do | |
rpc({:lookup, key}) | |
end | |
defp rpc(q) do | |
send :kvs, {self(), q} | |
receive do | |
{:kvs, reply} -> | |
reply | |
end | |
end | |
defp loop do | |
receive do | |
{form, {:store, key, value}} -> | |
Process.put key, value | |
send form, {:kvs, true} | |
loop() | |
{form, {:lookup, key}} -> | |
send form, {:kvs, Process.get key} | |
loop() | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment