Pockets is an Elixir wrapper around Erlang ETS and DETS, Erlang's built-in solutions for memory- and disk-based term storage. Pockets aims to provide a simple and familiar interface for caching and persisting data by implementing many of the functions found in the built-in Map
and Keyword
modules. A pocket may hold data in memory or on disk.
For those needing more power or versatility than what :ets
or :dets
can offer, Elixir includes
:mnesia
.
Secondly, the docs on erlang.org are a bit rough to look at for Elixir developers, so
this package acts as a case study of the differences between the powerful built-in :ets
and :dets
libraries.
In case it was too subtle, "Pockets" is a name that includes "ETS" for mnemonic purposes.
Add pockets
to your list of dependencies in mix.exs
:
def deps do
[
{:pockets, "~> 0.1.0"}
]
end
A simple memory-based cache requires no special arguments:
iex> Pockets.new(:my_cache)
{:ok, :my_cache}
iex> Pockets.put(:my_cache, :a, "Apple") |> Pockets.put(:b, "boy") |> Pockets.put(:c, "Charlie")
:my_cache
iex> Pockets.get(:my_cache, :c)
"Charlie"
Using a disk-based cache is appropriate when you need your data to persist. Just supply a file path as the second argument to Pockets.new/3
:
iex> Pockets.new(:on_disk, "/tmp/cache.dets")
{:ok, :on_disk}
You can easily populate your pocket with existing data:
iex> Pockets.new(:on_disk, "/tmp/cache.dets")
{:ok, :on_disk}
iex> Pockets.merge(:on_disk, %{x: "xylophone", y: "yellow"})
:on_disk
You can easily inspect your data, e.g. using Pockets.to_map/1
:
iex> Pockets.to_map(:on_disk)
%{x: "xylophone", y: "yellow"}
See the Pockets
module documentation for more info!
"pocket" by Hilmi Hidayat from the Noun Project