-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ian Vaughan
committed
Oct 5, 2018
1 parent
9d06f22
commit 5881802
Showing
3 changed files
with
68 additions
and
25 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
defmodule SlotSync.Cache.Redis do | ||
use GenServer | ||
|
||
@spec start_link() :: :ignore | {:error, any()} | {:ok, pid()} | ||
def start_link, do: GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
|
||
def get(shift), do: GenServer.call(__MODULE__, {:get, shift}) | ||
def set(shift), do: GenServer.cast(__MODULE__, {:set, shift}) | ||
def del(shift), do: GenServer.cast(__MODULE__, {:del, shift}) | ||
def del_all, do: GenServer.cast(__MODULE__, {:del_all}) | ||
|
||
# Callbacks | ||
|
||
@impl true | ||
@spec init(any()) :: {:ok, pid()} | ||
def init(_) do | ||
{:ok, conn} = Redix.start_link("redis://localhost:6379/3", name: :redix) | ||
{:ok, conn} | ||
end | ||
|
||
@impl true | ||
def handle_call({:get, shift}, _caller, conn) do | ||
{:ok, shift} = Redix.command(conn, ["GET", shift["id"]]) | ||
|
||
{:reply, decode(shift), conn} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:set, shift}, conn) do | ||
{:ok, _shift} = Redix.command(conn, ["SET", shift["id"], shift |> Poison.encode!()]) | ||
|
||
{:noreply, conn} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:del, shift}, conn) do | ||
{:ok, _shift} = Redix.command(conn, ["DEL", shift["id"]]) | ||
|
||
{:noreply, conn} | ||
end | ||
|
||
@impl true | ||
def handle_cast({:del_all}, conn) do | ||
# {:ok, _shift} = Redix.command(conn, ["SET", shift["id"], shift |> Poison.encode!()]) | ||
|
||
{:noreply, conn} | ||
end | ||
|
||
defp decode(nil), do: nil | ||
defp decode(shift), do: shift |> Poison.decode!() | ||
end |
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