Skip to content

am-kantox/easy_ets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CircleCI    EasyETS

The very simple :ets wrapper simplifying cross-process :ets handling (like Agent, but :ets).

Intro

EasyETS is a very thin simple ETS wrapper, simplifying the trivial usage of ETS as key-value store. It is not intended to replicate :ets module functionality by any mean. It might be used as a drop-in to avoid process-based Agent as key-value store.

It exposes only CRUD functionality of ETS, alongside with Access behaviour.

Built on top of :ets, it’s not distributed. Tables created are sets; public and named by default. This might be changed by passing {table_name, options} tuple instead of just table name to the initializer (see below.)

Usage

There are two ways EasyETS might be used: as a standalone module, or as a module extension.

Standalone usage

iex> EasyETS.new(MyApp.MyModuleToBeGenerated)

iex> MyApp.MyModuleToBeGenerated.ets_put(:foo, 42)
%MyApp.MyModuleToBeGenerated{table: MyApp.MyModuleToBeGenerated}

iex> MyApp.MyModuleToBeGenerated.ets_get(:foo, 42)
42

iex> term = %{data: MyApp.MyModuleToBeGenerated.ets_put(:value, 42)}
iex> update_in(term, [:data, :value], fn _ -> "👍" end)
iex> get_in(term, [:data, :value])
"👍"

The table is actually managed by the EasyETS application, so it won’t be destroyed if the process called EasyETS.new/1 exits.

Module

defmodule MyApp.MyModuleBackedByTable do
  use EasyETS

  ...
end

One might override EasyETS.ets_table_name/0 in the module to change the name of the table.

Interface exported

EasyETS exports the simplest possible interface for CRUD on purpose. Whether one needs more sophisticated :ets operations, it’s still possible through %MyApp.MyModuleBackedByTable{}.table (yes, it’s a struct underneath.) The latter holds the reference to the respective :ets table.

@doc "Updates the value in the table under the key passed"
@spec ets_put(key :: term(), value :: term()) :: EasyETS.t()

@doc "Retrieves the value from the table stored under the key passed"
@spec ets_get(key :: term(), default :: any()) :: term()

@doc "Deletes the value from the table stored under the key passed"
@spec ets_del(key :: term()) :: EasyETS.t()

@doc "Returns all the values from the table"
@spec ets_all() :: list()

Access behaviour

Modules produced / updated by EasyETS do support Access behaviour.

Env́io support

Modules produced / updated by EasyETS do send broadcast messages on both :update and :delete actions. See Env́io documentation on how to subscribe to them.

Each message is sent to two channels: :all (all the updates managed by NimbleCSV) and the channel with the name equal to the name of the table updated.

Installation

def deps do
  [
    {:easy_ets, "~> 0.1"}
  ]
end

About

The very simple ETS wrapper simplifying cross-process ETS handling (like `Agent`, but `:ets`).

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages