I am currently trying to make an atom for a database connection to Redis. Usually I would use the name
option in start
of GenServer
, but that is not available. In the Getting Started (Elixir 1.0.5), the example seems to show Process.register(pid, :kv)
as an option. Maybe there is a better option to make this process accessible?
Following is showing how it doesn't match the guard in query for the connection client.
iex(2)> redis_client = Exredis.start
#PID<0.367.0>
iex(3)> Process.register(redis_client, :redis_client)
true
iex(4)> :redis_client |> Exredis.query ["GET", "test"]
** (FunctionClauseError) no function clause matching in Exredis.query/2
(exredis) lib/exredis.ex:95: Exredis.query(:redis_client, ["GET", "test"])
iex(4)> redis_client |> Exredis.query ["GET", "test"]
:undefined
iex(5)> redis_client |> Exredis.query ["SET", "test", "value"]
"OK"
iex(6)> redis_client |> Exredis.query ["GET", "test"]
"value"
iex(7)> :redis_client
:redis_client
Your error most likely comes from these lines in ExRedis' source:
def query(client, command) when is_pid(client) and is_list(command), do:
client |> :eredis.q(command) |> elem(1)
As you can see, start_link/2
only accepts a pid as the first argument (when is_pid(client)
).
You can use Process.whereis/1
to find the pid of a registered process and pass that to ExRedis:
iex> :redis_client |> Process.whereis |> Exredis.query(["SET", "test", "value"])
"OK"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With