View Source Plausible.Cache behaviour (Plausible v0.0.1)

Caching interface specific for Plausible. Usage:

use Plausible.Cache

# - Implement the callbacks required
# - Optionally override `unwrap_cache_keys/1`
# - Populate the cache with `Plausible.Cache.Warmer`

Serves as a thin wrapper around Cachex, but the underlying implementation can be transparently swapped.

Even though the Cachex process is started, cache access is disabled during tests via the :plausible, Elixir.Plausible.Cache, enabled: bool() application env key. This can be overridden on case by case basis, using the child specs options.

The base_db_query/0 callback is used to generate the base query that is executed on every cache refresh.

There are two modes of refresh operation: :all and :updated_recently; the former will invoke the query as is and clear all the existing entries, while the latter will attempt to limit the query to only the records that have been updated in the last 15 minutes and try to merge the new results with existing cache entries.

Both refresh modes are normally executed periodically from within a warmer process; see: Plausible.Cache.Warmer. The reason for two modes is that the latter is lighter on the database and can be executed more frequently.

When Cache is disabled via application env, the get/1 function falls back to pure database lookups (implemented via get_from_source/1 callback. This should help with introducing cached lookups in existing code, so that no existing tests should break.

Refreshing the cache emits telemetry event defined as per telemetry_event_refresh/2.

Summary

Callbacks

Returns the base Ecto query used to refresh the cache

Supervisor child id, must be unique within the supervision tree

Counts all items at the source, an aggregate query most likely

Retrieves the item from the source, in case the cache is disabled

Unique cache name, used by underlying implementation

Optionally unwraps the keys of the cache items, in case one item is stored under multiple keys

Functions

Looks for application env value at :plausible, Elixir.Plausible.Cache, enabled: bool()

Callbacks

@callback base_db_query() :: Ecto.Query.t()

Returns the base Ecto query used to refresh the cache

@callback child_id() :: atom()

Supervisor child id, must be unique within the supervision tree

@callback count_all() :: integer()

Counts all items at the source, an aggregate query most likely

@callback get_from_source(any()) :: any()

Retrieves the item from the source, in case the cache is disabled

@callback name() :: atom()

Unique cache name, used by underlying implementation

@callback unwrap_cache_keys([any()]) :: [{any(), any()}]

Optionally unwraps the keys of the cache items, in case one item is stored under multiple keys

Functions

Looks for application env value at :plausible, Elixir.Plausible.Cache, enabled: bool()