重新加载Elixir中的配置(使用Mix.Config?)

时间:2018-06-21 16:41:36

标签: elixir mix

在我的config.exs文件中,

config :my_app,
  foobar: [%{foo: "bar", keywords: ["hello", "world!"]},
           %{foo: "xyz", keywords: ["bloop"]}]

此列表在代码中检索

Application.get_env(:my_app, :foobar)

但是,如果我要更新此foobar配置,则更改将在我重新启动应用程序后才会生效。

或者,还有更好的方法可以解决此问题吗?我最初的解决方案是从文件中解析列表列表。每当我修改文件时,效果都很好,但是现在我想使用地图列表。

2 个答案:

答案 0 :(得分:0)

我不知道任何允许动态重新加载配置的解决方案。由于配置标志会影响编译,因此很难,例如:

structure(list(group = c(918893L, 918893L, 918894L, 918895L, 
918895L, 918895L, 918895L, 918895L, 918896L, 918896L, 918896L, 
918896L, 918896L, 918896L, 918897L, 918948L, 919051L, 918974L, 
919056L, 918944L, 919068L, 919250L, 919181L, 919247L, 919072L, 
919122L, 919077L, 918935L, 918988L, 919083L), X1 = structure(c(NA, 
1L, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, NA, NA, 2L, NA, 
NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("0", 
"1"), class = "factor"), X2 = structure(c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("0", "1"), class = "factor"), 
X3 = structure(c(1L, NA, 1L, 1L, 1L, NA, 1L, 1L, 1L, NA, 
1L, 1L, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA), .Label = c("0", "1"), class = "factor"), 
X4 = structure(c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_), .Label = "1", class = "factor"), X5 = structure(c(NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), .Label = c("0", 
"1"), class = "factor"), n = c(926L, 1778L, 2521L, 3727L, 
3750L, 3765L, 4668L, 4910L, 5040L, 5465L, 5586L, 5649L, 5914L, 
6419L, 6791L, 95820L, 278882L, 141727L, 288089L, 89158L, 
309150L, 633594L, 511987L, 626841L, 316258L, 405562L, 325873L, 
73109L, 166452L, 334959L)), row.names = c(NA, -30L), class = "data.frame", 
.Names = c("group", 
"X1", "X2", "X3", "X4", "X5", "n"))

在此摘录中,defmodule Foo do @foo Application.get(:foo, __MODULE__) def foo, do: @foo end 将在编译期间进行计算。

不过,您可以使用@foo更改配置,但这只会影响运行时片段。

答案 1 :(得分:0)

如何使用代码中的配置?使用Application.get_env / 2检索值时,应进行更改。还是因为配置文件需要重新编译?