Mnho229

Mnho229

Elixir in Action - ch07 - implementing a cache help

Pretty new to Elixir and I’ve been going through Elixir in Action. I sense this issue could be solved if I drop down to a previous Elixir version but any advice to be able to do this on 1.11 would be very nice.

In 7.2.1 it talks about implementing a cache to manage multiple to-do lists.

Code from the book’s repo: https://github.com/sasa1977/elixir-in-action/tree/master/code_samples/ch07/todo_cache/lib/todo

The problem is, when I run these commands after iex -S mix

{:ok, cache} = Todo.Cache.start()
{:ok, #PID<0.154.0>}
Todo.Cache.server_process(cache, "Bob's List")
#PID<0.156.0>
Todo.Cache.server_process(cache, "Alice's Lists")

I receive the following error:

21:28:33.353 [error] GenServer #PID<0.154.0> terminating
** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.156.0>}}
    (todo_cache 0.1.0) lib/todo/cache.ex:29: Todo.Cache.handle_call/3
    (stdlib 3.13.2) gen_server.erl:706: :gen_server.try_handle_call/4
    (stdlib 3.13.2) gen_server.erl:735: :gen_server.handle_msg/6
    (stdlib 3.13.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Last message (from #PID<0.152.0>): {:server_process, "Bob's Lists"}
State: %{"Bob's List" => #PID<0.156.0>}
Client #PID<0.152.0> is alive

    (stdlib 3.13.2) gen.erl:208: :gen.do_call/4
    (elixir 1.11.2) lib/gen_server.ex:1024: GenServer.call/3
    (stdlib 3.13.2) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir 1.11.2) src/elixir.erl:280: :elixir.recur_eval/3
    (elixir 1.11.2) src/elixir.erl:265: :elixir.eval_forms/3
    (iex 1.11.2) lib/iex/evaluator.ex:261: IEx.Evaluator.handle_eval/5
    (iex 1.11.2) lib/iex/evaluator.ex:242: IEx.Evaluator.do_eval/3
    (iex 1.11.2) lib/iex/evaluator.ex:220: IEx.Evaluator.eval/3
** (exit) exited in: GenServer.call(#PID<0.154.0>, {:server_process, "Bob's Lists"}, 5000)
    ** (EXIT) an exception was raised:
        ** (MatchError) no match of right hand side value: {:error, {:already_started, #PID<0.156.0>}}
            (todo_cache 0.1.0) lib/todo/cache.ex:29: Todo.Cache.handle_call/3
            (stdlib 3.13.2) gen_server.erl:706: :gen_server.try_handle_call/4
            (stdlib 3.13.2) gen_server.erl:735: :gen_server.handle_msg/6
            (stdlib 3.13.2) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
    (elixir 1.11.2) lib/gen_server.ex:1027: GenServer.call/3

This seems to only happen when I change the string in the second parameter. Adding another clause for the case statement didn’t stop the error. How do I get past this?

Marked As Solved

APB9785

APB9785

Creator of ECSx

I just downloaded the files and compiled them with Elixir 1.11.3.

iex(1)> c("server.ex")
warning: ...
[Todo.Server]
iex(2)> c("cache.ex")
[Todo.Cache]
iex(3)> c("list.ex")
warning: ...
[Todo.List]
iex(4)> {:ok, cache} = Todo.Cache.start
{:ok, #PID<0.129.0>}
iex(5)> Todo.Cache.server_process(cache, "Bob's List")
#PID<0.132.0>
iex(6)> Todo.Cache.server_process(cache, "Alice's Lists")
#PID<0.134.0>

No errors. So there must be some differences, either between your code and the repo code, or in the commands you’re running. Glancing at the code, that string in the second parameter is only used as a map key. You should be able to pass any string as the name.

Also Liked

Mnho229

Mnho229

It did work when I cloned the code over. So I took another look, and then it turns out my server.ex had its name parameter filled in. And the files I was meticulously looking over might have been in a different folder.

This is embarrassing.

Thank you all for replying.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New

We're in Beta

About us Mission Statement