我的Phoenix应用程序最初加载时,我需要建立Redis连接。在阅读文档时,我认为代码可以在/config/dev.exs
或/config/config.exs
中使用,但是我用作Redis接口的Redix依赖项未加载在/config
以下导致/config
中的参考错误:
Redix.start_link("redis://localhost:6379/3", name: :redix)
我只想在应用加载时调用一次。我应该在我的Phoenix应用程序中将该呼叫放在哪里?
答案 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