Memoir

Caching that feels native to Elixir

<img src="https://discordapp.com/api/guilds/823178343943897088/widget.png?style=shield" alt="Join on Discord"> <img src="https://img.shields.io/hexpm/v/memoir.svg" alt="Memoir"> <img src="https://img.shields.io/badge/License-GPL%203.0-blue.svg" alt="License: GPL 3.0"> <img src="https://img.shields.io/badge/Elixir-1.18.1-4e2a8e" alt="Elixir">

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


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