1
我是elixir和OTP的新手。如何將OTP中的模塊添加到我的混音項目中?
我想爲我的服務器使用GenServer.Behaviour
,但由於某些原因,elixir無法找到它。我創建了一個混合的項目,但是當我鍵入mix compile
我收到以下錯誤
==上的文件LIB/lecture3.ex == **(CompileError)編譯錯誤的lib/lecture3.ex:2:模塊GenServer.Behaviour未加載和找不到 (酏劑)擴展宏:Kernel.use/1 LIB/lecture3.ex:2:高速緩存(模塊)
我想我必須包括模塊,但如何?
mix.exs:
defmodule LECTURE3.Mixfile do
use Mix.Project
def project do
[app: :lecture3,
version: "0.1.0",
elixir: "~> 1.3",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps()]
end
def application do
[applications: [:logger]]
end
defp deps do
[]
end
end
lecture3.ex:
defmodule Cache do
use GenServer.Behaviour
def handle_cast({:put, url, page}, {pages, size}) do
new_pages = Dict.put(pages, url, page)
new_size = size + byte_size(page)
{:noreply, {new_pages, new_size}}
end
def handle_call({:get, url}, _from, {pages, size}) do
{:reply, pages[url], {pages, size}}
end
def handle_call({:size}, _from, {pages, size}) do
{:reply, size, {pages, size}}
end
def start_link do
:gen_server.start_link({:local,:cache}, __MODULE__, {HashDict.new, 0}, [])
end
def put(url, page) do
:gen_server.cast(:cache, {:put, url, page})
end
def get(url) do
:gen_server.call(:cache, {:get, url})
end
def size do
:gen_server.call(:cache, {:size})
end
end
defmodule CacheSupervisor do
def init(_args) do
workers = [worker(Cache, [])]
supervise(workers, strategy: :one_for_one)
end
def start_link(domain) do
:supervisor.start_link(__MODULE__, [domain])
end
end
Enum.map(["de","edu", "com" ,"it"], fn(x)-> CacheSupervisor.start_link(x)
end)
感謝幫助。我稍後會接受你的回答 –