Redix 在 elixir 中使用

发布于 2023-10-01 16:54:36 字数 1024 浏览 35 评论 0

添加依赖:

  defp deps do
    [
      {:plug_cowboy, "~> 2.0"},
      {:hackney, "~> 1.17"},
      {:jason, ">= 1.0.0"},
      {:redix, "~> 1.1"}
    ]
  end

执行:

mix deps.get

在 application 中启动,添加到 superviser 树中:

defmodule Elixirapp.Application do
  @moduledoc false

  use Application
  require Logger

  @impl true
  def start(_type, _args) do
    children = [
      {Plug.Cowboy, scheme: :http, plug: Elixirapp.Router, options: [port: 8010]},
      {Redix, host: "localhost", name: :redix}, # name :redix is the conn
    ]

    opts = [strategy: :one_for_one, name: Elixirapp.Supervisor]
    Logger.info("Server listening ...")
    Supervisor.start_link(children, opts)
  end
end

这里的 name :redix 是 redix 客户端与 redis 的链接

使用方式:

{:ok, data} = Redix.command(:redix, ["SET", "name", "curry"])

这里的 atom :redix 是 Redix 启动时 name 参数指定的名称。

或者使用 pipe:

 {:ok, data} = :redix |> Redix.command(["GET", "name"])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

文章
评论
369 人气
更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文