View Source Cachex.Hook behaviour (Cachex v3.6.0)

Module controlling hook behaviour definitions.

This module defines the hook implementations for Cachex, allowing the user to add hooks into the command execution. This means that users can build plugin style listeners in order to do things like logging. Hooks can be registered to execute either before or after the Cachex command, and can be blocking as needed.

Link to this section Summary

Callbacks

Returns the actions this hook is expected to listen on.

Returns whether this hook is asynchronous or not.

Handles a cache notification.

Handles a provisioning call.

Returns an enumerable of provisions this hook requires.

Returns the timeout for all calls to this hook.

Returns the type of this hook.

Link to this section Callbacks

@callback actions() :: :all | [atom()]

Returns the actions this hook is expected to listen on.

This will default to the atom :all, which signals that all actions should be reported to the hook. If not this atom, an enumerable of atoms should be returned.

@callback async?() :: boolean()

Returns whether this hook is asynchronous or not.

Link to this callback

handle_notify(tuple, tuple, any)

View Source
@callback handle_notify(tuple(), tuple(), any()) :: {:ok, any()}

Handles a cache notification.

The first argument is the action being taken along with arguments, with the second argument being the results of the action (this can be nil for hooks) which fire before the action is executed.

Link to this callback

handle_provision({}, any)

View Source
@callback handle_provision({atom(), any()}, any()) :: {:ok, any()}

Handles a provisioning call.

The provided argument will be a Tuple dictating the type of value being provisioned along with the value itself. This can be used to listen on states required for hook executions (such as cache records).

@callback provisions() :: [atom()]

Returns an enumerable of provisions this hook requires.

The current provisions available to a hook are:

  • cache - a cache instance used to make cache calls from inside a hook with zero overhead.

This should always return an enumerable of atoms; in the case of no required provisions an empty enumerable should be returned.

@callback timeout() :: nil | integer()

Returns the timeout for all calls to this hook.

This will be applied to hooks regardless of whether they're synchronous or not; a behaviour change which shipped in v3.0 initially.

@callback type() :: :pre | :post

Returns the type of this hook.

This should return :post to fire after a cache action has occurred, and return :pre if it should fire before the action occurs.