簡體   English   中英

如何在phoenix elixir聊天應用程序的監督樹中運行Redix並從其他模塊訪問

[英]How can I run Redix in supervision tree in phoenix elixir chat app and access from different module

我想在我的聊天應用程序中使用{:redix,“〜> 0.6.1”}十六進制包,並從監督樹開始

{:ok, conn} = Redix.start_link()
{:ok, conn} = Redix.start_link(host: "example.com", port: 5000)
{:ok, conn} = Redix.start_link("redis://localhost:6379/3", name: :redix)


Redix.command(conn, ["SET", "mykey", "foo"])

但是當我嘗試將連接開始鏈接放到子進程時卻給出了錯誤

children = [
      # Start the Ecto repository
      supervisor(PhoenixChat.Repo, []),
      # Start the endpoint when the application starts
      supervisor(PhoenixChat.Endpoint, []),
      # Start your own worker by calling: PhoenixChat.Worker.start_link(arg1, arg2, arg3)
      # worker(PhoenixChat.Worker, [arg1, arg2, arg3]),
      supervisor(PhoenixChat.Presence, []),

      #supervisor(Phoenix.PubSub.Redis, [:chat_pubsub, host: "127.0.0.1"])
    ]

如何啟動Redix連接並將數據存儲到Redis?

您要執行的操作是Register進程ID。 為此,通常可以在opts中指定其名稱,如下所示:

worker(Redix, [[], [name: RedixConnection]])

通常,在注冊過程后,您可以使用它的名稱來代替PID(總是在文檔中查閱,但這是常見的模式),如下所示:

Redix.command(RedixConnection, ["PING"])

大多數情況下,一個連接是不夠的。 您可能想要使用某種池化機制,例如poolboy 文檔中有一個非常整齊的頁面供您閱讀,這稱為“ Real-world usage 它可能會回答與此主題相關的大多數問題。

也請考慮根據您的原因使用內置的Erlang / Elixir解決方案。 我不知道您的確切用例,但您可能想查看ETS,DTS和Mnesia。

  children = [
      # Start the Ecto repository
      supervisor(PhoenixChat.Repo, []),
      # Start the endpoint when the application starts
      supervisor(PhoenixChat.Endpoint, []),
      # Start your own worker by calling: PhoenixChat.Worker.start_link(arg1, arg2, arg3)
      # worker(PhoenixChat.Worker, [arg1, arg2, arg3]),
      supervisor(PhoenixChat.Presence, []),
      worker(Redix, [[], [name: :redix]]),

      #supervisor(Phoenix.PubSub.Redis, [:chat_pubsub, host: "127.0.0.1"])
    ]




Redix.command(:redix, ["SET", "key", "value"])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM