Packages
SuperWorker is a powerful Elixir library for working with supervisors and background jobs. It provides a much simpler approach than traditional supervisors. This library is currently under development and is unstable, so it is not recommended for production use.
Current section
Files
Jump to
Current section
Files
lib/supervisor/term_storage.ex
defmodule SuperWorker.TermStorage do
@moduledoc """
A wrapper module for storing terms in :persistent_term.
Good choice for storing data that is rarely change.
Data is can acessed by all processes in the same node.
Key was prefixed with the module name to avoid key collision.
Note:
By default, TermStorage will be limited to 1GB of memory.
If you need to store more data, you can increase the limit by setting the `:persistent_term` option in the Erlang VM.
"""
require Logger
require SuperWorker.Log
@me __MODULE__
@doc """
Get the value of the key from the storage.
"""
def get(key) do
SuperWorker.Log.debug(fn -> "SuperWorker, TermStorage, get key: #{inspect(key)}" end)
case :persistent_term.get({@me, key}, nil) do
nil -> {:error, :not_found}
value -> {:ok, value}
end
end
@doc """
Put the value of the key to the storage.
"""
def put(key, value) do
:persistent_term.put({@me, key}, value)
end
@doc """
Delete the key from the storage.
"""
def delete(key) do
:persistent_term.erase({@me, key})
end
@doc """
Get all key/value in the storage.
"""
def get_all do
Enum.filter(
:persistent_term.get(),
fn
{{mod, _}, _} -> mod == @me
_ -> false
end
)
end
end