Spaces:
Sleeping
Sleeping
File size: 1,337 Bytes
2e92879 76486d3 2e92879 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
defmodule Srh.Redis.ClientWorker do
use GenServer
def start_link(connection_info) do
GenServer.start_link(__MODULE__, connection_info, [])
end
def init(connection_info) do
Process.send(self(), :create_connection, [])
{
:ok,
%{
connection_info: connection_info,
redix_pid: nil
}
}
end
def redis_command(worker, command_array) do
GenServer.call(worker, {:redis_command, command_array})
end
def handle_call({:redis_command, command_array}, _from, %{redix_pid: redix_pid} = state)
when is_pid(redix_pid) do
case Redix.command(redix_pid, command_array) do
{:ok, res} ->
{:reply, {:ok, res}, state}
{:error, res} ->
{:reply, {:error, res}, state}
end
end
def handle_call(_msg, _from, state) do
{:reply, :ok, state}
end
def handle_cast(_msg, state) do
{:noreply, state}
end
# TODO: Handle host / port connections
def handle_info(
:create_connection,
%{
connection_info: %{
"connection_string" => connection_string
}
} = state
)
when is_binary(connection_string) do
{:ok, pid} = Redix.start_link(connection_string)
{:noreply, %{state | redix_pid: pid}}
end
def handle_info(_msg, state) do
{:noreply, state}
end
end
|