Skip to content

Instantly share code, notes, and snippets.

@blackode
Created April 3, 2018 13:49
Show Gist options
  • Save blackode/3f3c5c579c8863f35e26bac0e7a4bd2a to your computer and use it in GitHub Desktop.
Save blackode/3f3c5c579c8863f35e26bac0e7a4bd2a to your computer and use it in GitHub Desktop.
State preserving after server collapse
defmodule Bank.Cache do
use GenServer
def start_link initial_balance do
GenServer.start_link(__MODULE__, initial_balance)
end
def init(initial_balance) do
{:ok, initial_balance}
end
def get_balance(pid) do
GenServer.call pid, :get
end
def save_balance(new_balance, pid) do
GenServer.cast(pid,{:save, new_balance})
end
def handle_call(:get, _from, balance) do
{:reply, balance, balance}
end
def handle_cast({:save, new_balance}, _balance) do
{:noreply, new_balance}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment