View Source Cachex.Warmer behaviour (Cachex v4.0.3)

Module controlling cache warmer behaviour definitions.

This module defines the cache warming implementation for Cachex, allowing the user to register warmers against a cache to populate the tables periodically. Doing this allows for easy pulling against expensive values (such as those from a backing database or remote server), without risking heavy usage.

Warmers will block when the cache is started to ensure that the application will not complete booting up, until your cache has been warmed. This guarantees that there will not be any time where the application is available, without the desired data in the cache.

Warmers are fired on a schedule, and are exposed via a very simple behaviour of just a block to execute periodically.

Summary

Callbacks

Executes actions to warm a cache instance.

Callbacks

@callback execute(state :: any()) ::
  :ignore
  | {:ok, pairs :: [{key :: any(), value :: any()}]}
  | {:ok, pairs :: [{key :: any(), value :: any()}], options :: Keyword.t()}

Executes actions to warm a cache instance.

This can either return values to set in the cache, or the atom :ignore to signal that there's nothing to be set at this point in time. Values to be set should be returned as { :ok, pairs } where pairs is a list of { key, value } pairs to place into the cache via Cachex.put_many/3.

If you wish to provide expiration against the keys, you can return a Tuple in the form { :ok, pairs, opts } where opts is a list of options as accepted by the Cachex.put_many/3 function, thus allowing you to expire your warmed data. Unless this is provided, there is no explicit expiration associated with warmed values (as predicting the appropriate expiration is not possible).

The argument provided here is the one provided as state to the warmer records at cache configuration time; it will be nil if none was provided.