Skip to content

Instantly share code, notes, and snippets.

@gausby
Created November 17, 2015 17:20
Show Gist options
  • Save gausby/8b2e379c0bbbff2ee4d0 to your computer and use it in GitHub Desktop.
Save gausby/8b2e379c0bbbff2ee4d0 to your computer and use it in GitHub Desktop.
defmodule Store do
def start_link do
Agent.start_link(fn -> %{} end)
end
def set(pid, key, value) do
Agent.update(pid, fn state ->
Map.put(state, key, value)
end)
end
def get(pid, key) do
Agent.get(pid, fn state ->
Map.get(state, key)
end)
end
end
{:ok, pid} = Store.start_link
Store.set(pid, :foo, "foo")
Store.set(pid, :bar, "bar")
Store.set(pid, :baz, "baz")
{Store.get(pid, :foo), Store.get(pid, :bar)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment