Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elixir Cookbook

You're reading from   Elixir Cookbook Unleash the full power of programming in Elixir with over 60 incredibly effective recipes

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher Packt
ISBN-13 9781784397517
Length 236 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Paulo Pereira Paulo Pereira
Author Profile Icon Paulo Pereira
Paulo Pereira
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Command Line 2. Data Types and Structures FREE CHAPTER 3. Strings and Binaries 4. Modules and Functions 5. Processes and Nodes 6. OTP – Open Telecom Platform 7. Cowboy and Phoenix 8. Interactions A. Installation and Further Reading Index

Using an ETS table to share the state


In the Creating a stateful server process (messages with counters) and Using agents as an abstraction around states recipes, we saw that in order to maintain the state in our processes, we had to be passing the function calls to the state.

This solves the problem when a process needs to maintain states, but what if we need to share some data between multiple processes?

One of the solutions is the use of a structure that allows concurrent access and is really effective in the retrieval of data. This structure is called ETS.

Note

ETS means Erlang Term Storage, and it is an in-memory store.

In this recipe, we will create a small wrapper around an ETS table that can be used as a key/value store.

Getting ready

To create our key/value store, let's create new module ets_store.ex and add the following code:

defmodule EtsStore do
  @table_id __MODULE__

  def init do
    :ets.new(@table_id, [:public, :named_table])
  end

  def insert(key, value) do
    :ets.insert...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime