Implementing the cast and call logic

Now we'll need to write our handlers for the cast (async) and call (sync) functions. We'll start with our cast, which inserts the data into the ETS table. handle_cast/2 is a function you need to implement for your GenServer to be able to deal with message casts, which takes in two arguments: the message you're sending (whatever data you're sending), and the current state of the GenServer, which in our case is going to store the table:

  def handle_cast({:write, row}, %{table: table}=state) do    :ets.insert(table, {@key, row})    {:noreply, state}  end

In our write function, we sent the cast with a tuple of the message and the data we wanted to write. The message was :write, and the data was row. We also pattern ...

Get Phoenix Web Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.