sullyMusty

sullyMusty

active_memory is a package to help bring the power of in memory storage with ETS and Mnesia to your Elixir application.

ActiveMemory provides a simple interface and configuration which abstracts the ETS and Mnesia specifics and provides a common interface called a Store.

Downloads have exceeded 200,000 thus seems prudent to check in on it. Updated for changes in newer versions of Elixir and ran security checks and scans to ensure it is safe – all good!

Showing Posts 1 to 2

sullyMusty

sullyMusty OP

Hi all :waving_hand: — a big update since I first announced ActiveMemory here.

v0.7 is now on Hex, and it’s grown from a simple ORM into a typed, attribute-queryable in-memory store for ETS and Mnesia — with the safety rails you’d want before trusting it with short-lived or sensitive data. Four highlights:

:locked: Atomic, take-once withdraw/1

withdraw/1 now fetches-and-deletes in a single atomic operation — :ets.select_delete/2 for ETS, a :mnesia.transaction/1 for Mnesia. Under concurrent access exactly one caller gets a given record; everyone else gets {:error, :not_found}. Great for one-time tokens and job-claim patterns:

{:ok, token} = TokenStore.withdraw(%{value: submitted_token})

:hourglass_not_done: TTL / record expiry

Give a table a ttl and its records expire automatically:

defmodule MyApp.Tokens.Token do
  use ActiveMemory.Table, type: :ets, ttl: :timer.hours(1)

  attributes do
    field(:value)
    field(:user_id)
  end
end

Enforcement is hybrid: a lazy read-filter means one/select/all/withdraw never return an expired record (exact and immediate), and a periodic sweep deletes them to reclaim memory (sweep_interval configurable, default 60s). Zero overhead for tables without a ttl.

:ring_buoy: Crash resilience (TableHeir)

An ETS table is owned by the process that creates it, so a Store crash used to mean losing the data. ActiveMemory now runs a tiny, stable heir process registered as the ETS :heir; on a crash the table transfers to it, and the restarted Store reclaims it — data intact. No configuration, no API change. (Mnesia tables were never affected — they already survive process crashes.)

:card_index_dividers: ActiveRepo — multiple tables, one process

For several tables under one supervised entry point:

defmodule MyApp.Repo do
  use ActiveMemory.ActiveRepo,
    tables: [MyApp.People.Person, MyApp.Tokens.Token]
end

MyApp.Repo.write(%Person{first: "Kara"})   # table inferred from the struct
MyApp.Repo.one(Token, %{value: raw})        # reads take the table explicitly

It’s named ActiveRepo (not Repo) so it won’t collide with your Ecto.Repo, and a single repo can mix ETS and Mnesia tables — each with its own ttl if you like.

Where it fits

ActiveMemory isn’t a cache — if you want key-based caching with eviction and hit/miss stats, Cachex and Nebulex are excellent. ActiveMemory is for structured records in memory that you query by their attributes:

SessionStore.select(%{user_id: user_id, active?: true})
StaffStore.select(match(:role == "admin" and :last_login < cutoff))

…the kind of query a key/value store can’t express without hand-rolled secondary indexes.

A note on concurrency

A question that came up at a conference recently: “isn’t the single GenServer a bottleneck?” No — the CRUD functions run in the caller’s process and delegate straight to the adapter, so reads/writes get full ETS/Mnesia concurrency. The GenServer only handles lifecycle, state, and the TTL sweep.


Docs: hexdocs.pm/active_memory · Repo: github.com/SullysMustyRuby/active_memory

Would love feedback, use cases, or contributions — and if anything in the new features doesn’t behave as documented, please open an issue. Thanks!

thiagomajesk

thiagomajesk

I kid you not, I was just trying to find this library on the forum yesterday, but I couldn’t remember the name, so I guess thanks for the reminder!? haha :see_no_evil_monkey:

I’ve been working on some games for the past few years, and I always end up creating custom loaders which end up being some version of this, so this library is going to be super helpful. Congratulations on the project! :rocket:

— All posts loaded —

Where Next? Top

Trending in Announcing Top

bluzky
You may know https://ui.shadcn.com/, a UI component library for React. I really love it’s design style and components. I’ve built some co...
387 15136 120
New
wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 11030 135
New
shahryarjb
The Chelekom project is a library of Phoenix and LiveView components generated via Mix tasks to fit developer needs seamlessly. One of i...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New

Other Trending Topics Top

mudasobwa
I am seeing a lot of aplications of Argumentum ad Vericundiam in software discussions. They do link some piece of writing and point us to...
New
bartblast
Hey folks, I just published a post about Hologram’s funding and where the project goes next - the short version: Curiosum as Main Spons...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
lawik
I was thinking since Goatmire Elixir turned out pretty good I should maybe do another one. 30th of Sep - 2nd of Oct this year./ The firs...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews