Mnemonics

Read only data store for Elixir: fast, concurrently, for large data & hot reloadable.

Hex.pm Build Status

Mnemonics is analogous to Ruby’s ActiveHash in it’s usecase.

Document.

Installation

Add mnemonics to your list of dependencies in mix.exs:

def deps do
  [
    {:mnemonics, "~> 0.2"}
  ]
end

Usage

Config Mnemonics ets_dir. config.exs:

config :mnemonics, :ets_dir, "priv/repo/seeds"

Create an example.ets by :ets.tab2file/3. Then put it into the ets_dir. The examples.ets stores {:example1, %{id: :example1}}.

priv/
└repo/
  └seeds/
    └examples.ets

Create an Example module, use Mnemonics & load.

defmodule Example do
  use Mnemonics, table_name: :examples
end

Example.load 1

We can lookup the table.

:ets.lookup Example.table_name(1), :example1

Let’s reload a new table. Put a new examples.ets into the ets_dir & load it with a new version number.

Example.load 2

We can lookup the new table.

:ets.lookup Example.table_name(2), :example1

snap = Mnemonics.Snap.snap Mnemonics.Snap.new, 2, %{}
:ets.lookup Example.table_name(snap), :example1

Mnemonics has cache function named Mnemonics.Snap.

snap = Mnemonics.Snap.snap Mnemonics.Snap.new, 2, %{}
get_and_update_in snap[:examples].cache[:example1], fn
  nil ->
    example = :ets.lookup Example.table_name(snap), :example1
    {example, example}
  example -> {example, example}
end

:ets.new/2 Option

  • Should :public or :protected. :protected (default) is recommended.
  • Can’t :named_table.
  • {:read_concurrency, true} is recommended.

Architechture

processes