AshCache

Hex.pm HexDocs CI License

A cache for Ash resources. Reads come back from memory instead of the database, and the cache clears itself when the data changes.

TL;DR

defmodule MyApp.Shop.Product do
  use Ash.Resource,
    domain: MyApp.Shop,
    extensions: [AshCache.Resource]

  cache do
    read :get_by_sku
  end
end

The first get_by_sku read goes to the database and its results are saved. The next reads come from the cache, not the database. That's it.

Features

Installation

Add the package to mix.exs:

def deps do
  [
    {:ash_cache, "~> 0.1"},
    {:cachex, "~> 3.6"}
  ]
end

Cachex is the default store. To use another store, see the Storage guide.

Add the supervisor to your application. It starts the cache stores:

# lib/my_app/application.ex
children = [
  # ...
  {AshCache.Supervisor, otp_app: :my_app}
]

Usage

Set how long results are kept

ttl is in milliseconds. A read without one keeps its results for 5 minutes. Set ttl on the block for every read, or on one read:

cache do
  ttl :timer.hours(1)

  read :get_by_sku
  read :featured, ttl: :timer.minutes(1)
end

get_by_sku keeps results for an hour, featured for a minute.

Cache a generic action

For example, a call to another company's API:

cache do
  action :get_rate, ttl: :timer.minutes(10)
end

On a cache hit, the action's run function is not called.

Cache a slow calculation

A calculation written as an Elixir module is cached one result per record:

cache do
  calculation :shipping_quote, ttl: :timer.hours(1)
end

Writes don't clear these results; they run out with their ttl. See "A slow calculation" in Examples.

Clearing the cache

Writes through Ash clear the cache by themselves. To clear it when a related resource changes, or by hand after an import, see When the cache clears.

Learn more

Limits

For AI assistants

Instructions for coding agents (Claude Code, Cursor, Copilot, ...)

The file for agents is usage-rules.md. It ships inside the package, so after mix deps.get it is at deps/ash_cache/usage-rules.md. Point your agent at that file. If your project uses the usage_rules package, add :ash_cache to its list instead.

License

MIT. See LICENSE.