Hi, Please I need help.
When I call
Cache.put("google.com", "foo bar")
I got
:ok
when I call
Cache.get("google.com")
I got error
(EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started
I don’t know what am doing wrong please help me.
use GenServer
def start_link do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
@table_name :cache_table
@impl true
def init(state) do
:ets.new(@table_name, [:named_table, :set, :public, read_concurrency: true])
{:ok, state}
end
def get(url) do
GenServer.call(__MODULE__, {:get, url})
end
def put(url, data) do
GenServer.cast(__MODULE__, {:put, url, data})
end
@impl true
def handle_call({:get, url}, _from, state) do
reply =
case :ets.lookup(@table_name, url) do
[{^url, data}] ->
{:ok, data}
[] ->
:error
end
{:reply, reply, state}
end
def handle_cast({:put, url, data}, _from, state) do
:ets.insert(@table_name, {url, data})
{:noreply, state}
end