Nebulex.Caching.Decorators.Context (Nebulex v3.0.0-rc.1)

View Source

Decorator context.

Summary

Types

t()

Decorator context type.

Types

t()

@type t() :: %Nebulex.Caching.Decorators.Context{
  args: [any()],
  arity: non_neg_integer(),
  decorator: :cacheable | :cache_evict | :cache_put,
  function_name: atom(),
  module: module()
}

Decorator context type.

The decorator context defines the following keys:

  • :decorator - Decorator's name.
  • :module - The invoked module.
  • :function_name - The invoked function name
  • :arity - The arity of the invoked function.
  • :args - The arguments that are given to the invoked function.

Caveats about the :args

The following are some caveats about the context's :args to keep in mind:

  • Only arguments explicitly assigned to a variable will be included.
  • Ignored or underscored arguments will be ignored.
  • Pattern-matching expressions without a variable assignment will be ignored. Therefore, if there is a pattern-matching and you want to include its value, it has to be explicitly assigned to a variable.

For example, suppose you have a module with a decorated function:

defmodule MyApp.SomeModule do
  use Nebulex.Caching

  alias MyApp.Cache

  @decorate cacheable(cache: Cache, key: &__MODULE__.key_generator/1)
  def get_something(x, _y, _, {_, _}, [_, _], %{a: a}, %{} = z) do
    # Function's logic
  end

  def key_generator(context) do
    # Key generation logic
  end
end

The generator will be invoked like so:

key_generator(%Nebulex.Caching.Decorators.Context{
  decorator: :cacheable,
  module: MyApp.SomeModule,
  function_name: :get_something,
  arity: 7,
  args: [x, z]
})

As you may notice, only the arguments x and z are included in the context args when calling the key_generator/1 function.