2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ElixirのCachexモジュールをSupervisor.start_link/1のchild_listにどのように指定すればよいのか?

2
Last updated at Posted at 2020-06-03

ドキュメントの通りに行う

Cachex version 3.2のドキュメントには、以下のように書かれている。

Supervisor.start_link(
  [ worker(Cachex, [ :my_cache, [] ]) ]
)

このままのコードを書いて、コンパイルすると、undefined function worker/2 というエラーになるので、モジュール名を指定しなければならないらしい。

Supervisor.start_link(
  [ Supervisor.Spec.worker(Cachex, [ :my_cache, [] ]) ]
)

これで、実行できた。

ハマる

Elixir version 1.10.3 のドキュメントによると、Supervisor.Specは、deprecated になっている。ということで、child_specを使ってみる。

Supervisor.start_link(
  [ Cachex.child_spec(:my_cache, []) ]
)

これはだめ。Cachex.child_specのarityは、1なので、コンパイルエラーになる。じゃあリストにするの?と、以下のように書き換えてみる。

Supervisor.start_link(
  [ Cachex.child_spec([:my_cache, []]) ]
)

これは、実行時にエラーになる。Cachex.child_spec/1を実行すると以下のように展開される。

iex(1)> Cachex.child_spec([:my_cache, []])
%{
  id: Cachex,
  start: {Cachex, :start_link, [[:my_cache, []]]}, 
  type: :supervisor
}

Cachex.start_linkのarityが、2なので、上記のchild_specデータでは、実行時エラーになって当たり前。

じゃあどうする?と、行き着いた結果が以下。

Supervisor.start_link(
  [ Supervisor.child_spec(Cachex,
                          start: {Cachex, :start_link, [:my_cache, []]}) ]
)

残された疑問

Cachex.child_spec/1 って何なの?

その後

Cachex v3.3.0 で、Cachex.start_linkのarityが、1になり、キャッシュの名前は、:nameオプションで指定するようになった。めでたし、めでたし。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?