Skip to content

Instantly share code, notes, and snippets.

@defp
Created May 24, 2014 06:22
Show Gist options
  • Save defp/037e63a70376b636423f to your computer and use it in GitHub Desktop.
Save defp/037e63a70376b636423f to your computer and use it in GitHub Desktop.
-module(kvs).
-export([start/0, store/2, lookup/1]).
start() -> register(kvs, spawn(fun() -> loop() end)).
store(Key, Value) -> rpc({store, Key, Value}).
lookup(Key) -> rpc({lookup, Key}).
rpc(Q) ->
kvs ! {self(), Q},
receive
{kvs, Reply} ->
Reply
end.
loop() ->
receive
{From, {store, Key, Value}} ->
put(Key, {ok, Value}),
From ! {kvs, true},
loop();
{From, {lookup, Key}} ->
From ! {kvs, get(Key)},
loop()
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment