Caching that feels native to 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
def deps do
[
{:memoir, "~> 0.2.2"}
]
end
Start the application by adding it to your supervision tree:
children = [
Memoir
]
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()
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
Memoir is released under the GPL-3.0. See LICENCE