AshCache.Preparations.ReadThrough (AshCache v0.1.0)

Copy Markdown View Source

Read-through caching for a read action.

On a cache hit the query result is set via Ash.Query.set_result/2, so the data layer is never called. On a miss, an after_action hook caches the records the action returns.

The cache key is derived inside a before_action hook, not at preparation time: preparations run before the framework has applied everything that shapes the result — a get_by code interface, for example, attaches its primary-key filter after preparations, so a key derived earlier would collide the get with the unfiltered read.

Safety properties:

  • the cache key embeds actor and tenant (AshCache.Key), so a hit can only return records the same actor/tenant already read through the action's full authorization
  • empty results ([]/nil) are not cached unless cache_empty?: true, preventing a transient miss from being served for a whole TTL
  • Ash.Query.set_context(query, %{ash_cache: :bypass}) skips the cache for one query
  • identical misses running at once share one data-layer read (AshCache.Coalesce). The leader's lease is released in an around_transaction hook, which wraps the whole read, so a failed or raising read still lets its waiters go

Summary

Functions

init(opts)

Callback implementation for Ash.Resource.Preparation.init/1.

supports(opts)

Callback implementation for Ash.Resource.Preparation.supports/1.