在凤凰城建立Redis连接

时间:2020-02-12 16:50:05

标签: redis phoenix-framework

我的Phoenix应用程序最初加载时,我需要建立Redis连接。在阅读文档时,我认为代码可以在/config/dev.exs/config/config.exs中使用,但是我用作Redis接口的Redix依赖项未加载在/config

以下导致/config中的参考错误: Redix.start_link("redis://localhost:6379/3", name: :redix)

我只想在应用加载时调用一次。我应该在我的Phoenix应用程序中将该呼叫放在哪里?

1 个答案:

答案 0 :(得分:0)

{Redix, name: :redix}中的children数组中添加application.ex到redis进程到主管树。这意味着它将与您的应用程序一起启动:

    children = [
      # Start the Ecto repository
      MyApp.Repo,
      # Start the Telemetry supervisor
      AppWeb.Telemetry,
      # Start the PubSub system

      ....

      # Single Redis connection
      {Redix, name: :redix}
    ]

请参见https://hexdocs.pm/redix/real-world-usage.html

您可以签入iex -S mix

iex(1)> Redix.command(:redix, ["PING"])
{:ok, "PONG"}

现在您可以使用所有常规的Redix命令:https://hexdocs.pm/redix/readme.html#usage