memoir

Memoir is a lightweight, Rails-inspired caching library for Elixir with a pluggable backend architecture.


Keywords
caching, elixir-lang
License
GPL-3.0

Documentation

Memoir

Caching that feels native to Elixir

Join on Discord Memoir License: GPL 3.0 Elixir

Memoir brings effortless and expressive caching to Elixir. Inspired by the simplicity of Rails’ fetch API, Memoir gives you:

  • A clean cache/3 block interface

  • Pluggable backends (ETS, Cachex, or your own)

  • Minimal setup


Installation

def deps do
  [
    {:memoir, "~> 0.2.2"}
  ]
end

Start the application by adding it to your supervision tree:

children = [
  Memoir
]

Usage

Memoir is typically used to cache expensive function calls:

Memoir.cache({:user, 123}, ttl: :timer.minutes(5)) do
  expensive_user_lookup(123)
end

You can also interact with the cache directly:

Memoir.put({:user, 123}, "value", ttl: :timer.minutes(5))

Memoir.get({:user, 123})

Memoir.delete({:user, 123})

Memoir.clear()

Configuration

You can configure Memoir in your config.exs:

config :memoir,
  adapter: Memoir.Adapters.Cachex,
  adapter_opts: [ttl: 300_000]

You can also configure a cache per module like so:

defmodule Greeter do
  use Memoir,
    name: :greeter_cache,
    adapter: Memoir.Adapters.MyAdapter,
    ttl: :timer.minutes(5)

  def greet(name) do
    cache({:greet, name}) do # This will use the configured cache but can be overriden
      "Hello, #{name}!"
    end
  end
end

License

Memoir is released under the GPL-3.0. See LICENCE