View Source Cachex.Spec (Cachex v4.0.2)

Specification definitions based around records and utilities.

This serves as the "parent" header file for Cachex, where all records and macros are located. It's designed as a single inclusion file which provides everything you might need to implement features in Cachex, and indeed when interacting with Cachex.

Most macros in here should be treated as reserved for internal use only, but those based around records can be freely used by consumers of Cachex.

Summary

Functions

Creates a cache record from the provided values.

Updates a cache record from the provided values.

Creates a command record from the provided values.

Updates a command record from the provided values.

Creates an entry record from the provided values.

Updates an entry record from the provided values.

Creates an expiration record from the provided values.

Updates an expiration record from the provided values.

Creates a hook record from the provided values.

Updates a hook record from the provided values.

Creates a hooks collection record from the provided values.

Updates a hooks record from the provided values.

Creates a router record from the provided values.

Updates a router record from the provided values.

Creates a warmer record from the provided values.

Updates a warmer record from the provided values.

Types

@type cache() ::
  {:cache, name :: atom(), commands :: map(), compressed :: boolean(),
   expiration :: expiration(), hooks :: hooks(), limit :: term(),
   ordered :: boolean(), router :: router(), transactions :: boolean(),
   warmers :: [warmer()]}
@type command() ::
  {:command, type :: :read | :write,
   execute :: (any() -> any() | {any(), any()})}
@type entries() :: entry() | [entry()]
@type entry() ::
  {:entry, key :: any(), value :: any(), modified :: number(),
   expiration :: number()}
@type expiration() ::
  {:expiration, default :: non_neg_integer(),
   interval :: non_neg_integer() | nil, lazy :: boolean()}
@type hook() ::
  {:hook, module :: atom(), args :: Keyword.t(), name :: GenServer.server()}
@type hooks() :: {:hooks, pre :: [hook()], post :: [hook()], service :: [hook()]}
@type router() :: {:router, options :: Keyword.t(), module :: atom(), state :: any()}
@type warmer() ::
  {:warmer, required :: boolean(), interval :: integer() | nil,
   module :: atom(), state :: any(), name :: GenServer.server()}

Functions

Link to this macro

cache(args \\ [])

View Source (macro)

Creates a cache record from the provided values.

A cache record is used to represent the internal state of a cache, and is used when executing calls. Most values in here will be other records defined in the main specification, and as such please see their documentation for further info.

Link to this macro

cache(record, args)

View Source (macro)
@spec cache(cache(), Keyword.t()) :: cache()

Updates a cache record from the provided values.

Link to this macro

command(args \\ [])

View Source (macro)

Creates a command record from the provided values.

A command is a custom action which can be executed against a cache instance. They consist of a type (:read/:write) and an execution function. The type determines what form the execution should take.

In the case of a :read type, an execution function is a simple (any -> any) form, which will return the returned value directly to the caller. In the case of a :write type, an execution should be (any -> { any, any }) where the value in the left side of the returned Tuple will be returned to the caller, and the right side will be set inside the backing cache table.

Link to this macro

command(record, args)

View Source (macro)
@spec command(command(), Keyword.t()) :: command()

Updates a command record from the provided values.

Link to this macro

entry(args \\ [])

View Source (macro)

Creates an entry record from the provided values.

An entry record represents a single entry in a cache table.

Each entry has a key/value, along with a touch time and ttl. These records should never be used outside of the Cachex codebase other than when debugging, as they can change at any time and should be regarded as internal only.

Link to this macro

entry(record, args)

View Source (macro)
@spec entry(entry(), Keyword.t()) :: entry()

Updates an entry record from the provided values.

Link to this macro

expiration(args \\ [])

View Source (macro)

Creates an expiration record from the provided values.

An expiration record contains properties defining expiration policies for a cache.

A default value can be provided which will then be added as a default TTL to all keys which do not have one set explicitly. This must be a positive millisecond integer.

The interval being controlled here is the Janitor service schedule; it controls how often the purge runs in the background of your application to remove expired records. This can be disabled completely by setting the value to nil. This is also a millisecond integer.

The lazy value determines whether or not records can be lazily removed on read. Since this is an expected behaviour it's enabled by default, but there are cases where you might wish to disable it (such as when consistency isn't that big an issue).

Link to this macro

expiration(record, args)

View Source (macro)
@spec expiration(expiration(), Keyword.t()) :: expiration()

Updates an expiration record from the provided values.

Link to this macro

hook(args \\ [])

View Source (macro)

Creates a hook record from the provided values.

Hook records contain the properties needed to start up a hook against a cache instance. There are several components in a hook record:

  • arguments to pass through to the hook init/1 callback.
  • a flag to set whether or not a hook should fire asynchronously.
  • the module name backing the hook, implementing the hook behaviour.
  • options to pass to the hook server instance (allowing for names, etc).
  • provisions to pass through to the hook provisioning callback.
  • a PID reference to a potentially running hook instance (optional).
  • the timeout to wait for a response when firing synchronous hooks.
  • the type of the hook (whether to fire before/after a request).

These values are mainly provided by the user, and this record might actually be replaced in future with just a behaviour and a set of macros (as this record is very noisy now).

Link to this macro

hook(record, args)

View Source (macro)
@spec hook(hook(), Keyword.t()) :: hook()

Updates a hook record from the provided values.

Link to this macro

hooks(args \\ [])

View Source (macro)

Creates a hooks collection record from the provided values.

Hooks records are just a pre-sorted collection of hook records, grouped by their type so that notifications internally do not have to iterate and group manually.

Link to this macro

hooks(record, args)

View Source (macro)
@spec hooks(hooks(), Keyword.t()) :: hooks()

Updates a hooks record from the provided values.

Link to this macro

router(args \\ [])

View Source (macro)

Creates a router record from the provided values.

A router record reprsents routing within a distributed cache. Each router record should have a valid routing module provided, which correct implements the behaviour defined in Cachex.Router.

Options to be passed on router state initialization can also be provided, but note that all other values inside this structure are for internal use and will be overwritten as needed.

Link to this macro

router(record, args)

View Source (macro)
@spec router(router(), Keyword.t()) :: router()

Updates a router record from the provided values.

Link to this macro

warmer(args \\ [])

View Source (macro)

Creates a warmer record from the provided values.

A warmer record represents cache warmer processes to be run to populate keys.

A warmer should have a valid module provided, which correctly implements the behaviour associated with Cachex.Warmer. A state can also be provided, which will be passed to the execution callback of the provided module (which defaults to nil). The flag :required determines if the warmer much execute on cache startup.

Link to this macro

warmer(record, args)

View Source (macro)
@spec warmer(warmer(), Keyword.t()) :: warmer()

Updates a warmer record from the provided values.