AbsintheCacheFairy (absinthe_cache_fairy v0.2.0)

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

Link to this function

cache_key(name, args, opts \\ [])

Link to this macro

cache_resolve(captured_mfa_ast, opts \\ [])

(macro)

Macro that's used instead of Absinthe's resolve. This resolver can perform the following operations:

  1. Get the value from a cache if it is persisted. The resolver function is not evaluated at all in this case
  2. Evaluate the resolver function and store the value in the cache if it is not present there
  3. Handle the Absinthe.Middlewar.Async and Absinthe.Middleware.Dataloader middlewares. 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:

  1. It can be a captured named function because its name is extracted and used in the cache key.
  2. 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

Clears the whole cache. Slow.

Link to this function

get_or_store(cache_name \\ :graphql_cache, cache_key, resolver_fn)

The size of the cache in megabytes

Link to this function

store(cache_name \\ :graphql_cache, cache_key, value)

Link to this function

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:

  1. On execution checks if the value is present in the cache and returns it
  2. 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} ).()