Redix example usage

hi, i’m trying to use redis with my phoenix app, but confused where i must define the conn variable, can i just define it in config file ? and call Redix anywhere ? thank you

2 Likes

From the documentation (https://hexdocs.pm/redix/readme.html#usage) it seems as if it were returned by Redix.start_link.

1 Like
defmodule DigitalWeb.PageController do
  use DigitalWeb, :controller

  def use_redix(conn, _params) do
  	{:ok, conn} = Redix.start_link("redis://localhost:6379/3", name: :redix)
  	Redix.command(conn, ["SET", "mykey", "foo"])
  end

  def use_redix_again(conn, _params) do
  	{:ok, conn} = Redix.start_link("redis://localhost:6379/3", name: :redix)
  	Redix.command(conn, ["SET", "mykey", "foo"])
  end
end

i mean, i want to define start_link once, and call Redix.command everywhere

There is a chapter Real-world usage.

5 Likes

geez, yeah, by default, add {Redix, name: :redix} in lib/appname/application.ex

when I use this I have this error: {:error, %Redix.Error{message: "NOAUTH Authentication required."}
how can I import my Redis Password on it ?

iex(2)> Redix.command(:redix, ["PING"])
{:error, %Redix.Error{message: "NOAUTH Authentication required."}}

here you go lib/redix/start_options.ex#L21

{Redix, name: :redix, password: "password"}
3 Likes

code is the ultimate doc