I wish to write an extension to enable caching on certain read actions that define
cached? true inside a
How would I idiomatically proceed for the same in Ash? I wish to use Cachex for caching.
Ideally, I am thinking
use Ash.Resource, extensions: [MyCacheExtension]
read :read_by_id do
or a separate cache block which could be more flexible.
activate_for :read_by_id , ttl: 3000 # ms
The first one,
cached? true won’t be possible with the current DSL extension options (maybe some day, but probably not).
As for the latter, you can definitely do that kind of thing Caching read actions can be complex, because they can be filtered/sorted/limited, so my word of caution to you would be to do some early checks and raise errors if there are unexpected things provided to the query that you don’t know how to handle with your cache.
As for tips on how to do it, the best thing to do would be to look at a relatively simple extension that exists today,
AshArchival. You’ll want to do a similar thing that it does, except you are modifying read actions.
And then to do caching for Cachex, here is an example from
AshHq where we do exactly that. ash_hq/lib/ash_hq/docs/resources/library/preparations/check_cache.ex at main · ash-project/ash_hq · GitHub
By altering the appropriate read actions, you can attach similar behavior.
Thank you for examples.
Will explore this and post here whatever I end up with.