AbsintheCacheFairy (absinthe_cache_fairy v0.2.3)
Provides the macro cache_resolve that replaces the Absinthe's resolve and
caches the result of the resolver for some time instead of calculating it
every time.
Link to this section Summary
Functions
Macro that's used instead of Absinthe's resolve. This resolver can perform
the following operations
Clears the whole cache. Slow.
The size of the cache in megabytes
Exposed as sometimes it can be useful to use it outside the macros.
Link to this section Functions
cache_key(name, args, opts \\ [])
Macro that's used instead of Absinthe's resolve. This resolver can perform
the following operations:
- Get the value from a cache if it is persisted. The resolver function is not evaluated at all in this case
- Evaluate the resolver function and store the value in the cache if it is not present there
- Handle the
Absinthe.Middlewar.AsyncandAbsinthe.Middleware.Dataloadermiddlewares. In order to handle them, the function that executes the actual evaluation is wrapped in a function that handles the cache interactions
There are 2 options for the passed function:
- It can be a captured named function because its name is extracted and used in the cache key.
- If the function is anonymous or a different name should be used, a second parameter with that name must be passed.
Just like resolve comming from Absinthe, cache_resolve supports the {:ok, value}
and {:error, reason} result tuples. The :ok tuples are cached while the :error
tuples are not.
But cache_resolve knows how to handle a third type of response format. When
{:nocache, {:ok, value}} is returned as the result the cache does not cache
the value and just returns {:ok, value}. This is particularly useful when
the result can't be constructed but returning an error will crash the whole query.
In such cases a default/filling value can be passed (0, nil, "No data", etc.)
and the next query will try to resolve it again
clear_all()
Clears the whole cache. Slow.
get(key)
get_or_store(cache_name \\ :graphql_cache, cache_key, resolver_fn)
size()
The size of the cache in megabytes
store(cache_name \\ :graphql_cache, cache_key, value)
wrap(cached_func, name, args \\ %{}, opts \\ [])
Exposed as sometimes it can be useful to use it outside the macros.
Gets a function, name and arguments and returns a new function that:
- On execution checks if the value is present in the cache and returns it
- If it's not in the cache it gets executed and the value is stored in the cache.
NOTE: cached_func is a function with arity 0. That means if you want to use it
in your code and you want some arguments you should use it like this:
Cache.wrap( fn ->
fetch_last_price_record(pair)end, :fetch_price_last_record, %{pair: pair} ).()