AshCache.Resource (AshCache v0.1.0)

Copy Markdown View Source

Caches the read and generic actions you declare, each with its own lifetime.

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

  cache do
    read :get_by_sku, ttl: :timer.hours(1)
    action :exchange_rate, ttl: :timer.minutes(10)
    calculation :shipping_quote, ttl: :timer.hours(1)
  end
end

Each read is cached, keyed by the action, the whole query, the actor and the tenant (see AshCache.Key). A read lives as long as its own ttl, else the block's ttl (5 minutes by default); empty results are not cached unless you ask. Entries go in the shared :ash_cache store unless you set store. Every create, update or destroy clears the resource's cached reads after the transaction commits (see AshCache.Changes.InvalidateCache). So does a write to any resource listed in clear_when_changed.

Each action caches a generic action, keyed by the action, its arguments, the actor and the tenant, with the same ttl rules. On a hit its run is not called (see AshCache.Actions.RunThrough). Writes clear these entries too.

Each calculation caches a module calculation's result per record, keyed by its arguments, the actor, the tenant and every value it can read from the record (see AshCache.Calculations.CalculateThrough). It lives as long as its own ttl, else the block's. Writes do not clear these entries: a changed record gives a new key. Expression calculations are rejected; they already run in the database and cached reads hold them.

Actions and calculations without an entry are never cached.

Summary

Functions

cache(body)

(macro)