Cache.Strategy behaviour (elixir_cache v0.4.5)

View Source

Behaviour for strategy-based cache adapters.

Strategy adapters compose over existing cache adapters to provide higher-level caching patterns such as consistent hashing, multi-layer cascading, and refresh-ahead semantics.

Unlike regular adapters which implement Cache directly, strategy adapters receive the underlying adapter module and its resolved options so they can delegate operations appropriately.

Usage

Strategies are specified using the tuple format in use Cache:

use Cache,
  adapter: {Cache.HashRing, Cache.ETS},
  name: :my_cache,
  opts: [read_concurrency: true]

The first element is the strategy module, the second is the underlying adapter (or strategy-specific configuration).

Summary

Callbacks

Returns a supervisor child spec for the strategy.

Removes a value from the cache using the strategy's routing/layering logic.

Fetches a value from the cache using the strategy's routing/layering logic.

Returns the NimbleOptions schema for validating strategy-level opts.

Stores a value in the cache using the strategy's routing/layering logic.

Functions

Returns true if the given module implements the Cache.Strategy behaviour.

Callbacks

child_spec({})

@callback child_spec(
  {cache_name :: atom(), strategy_config :: term(), adapter_opts :: Keyword.t()}
) ::
  Supervisor.child_spec() | :supervisor.child_spec()

Returns a supervisor child spec for the strategy.

Receives the cache name, strategy config (the second element of the adapter tuple), and the resolved underlying adapter opts.

delete(cache_name, key, strategy_config, adapter_opts)

@callback delete(
  cache_name :: atom(),
  key :: atom() | String.t(),
  strategy_config :: term(),
  adapter_opts :: Keyword.t()
) :: :ok | ErrorMessage.t()

Removes a value from the cache using the strategy's routing/layering logic.

get(cache_name, key, strategy_config, adapter_opts)

@callback get(
  cache_name :: atom(),
  key :: atom() | String.t(),
  strategy_config :: term(),
  adapter_opts :: Keyword.t()
) :: ErrorMessage.t_res(any())

Fetches a value from the cache using the strategy's routing/layering logic.

opts_definition()

@callback opts_definition() :: Keyword.t()

Returns the NimbleOptions schema for validating strategy-level opts.

put(cache_name, key, ttl, value, strategy_config, adapter_opts)

@callback put(
  cache_name :: atom(),
  key :: atom() | String.t(),
  ttl :: pos_integer() | nil,
  value :: any(),
  strategy_config :: term(),
  adapter_opts :: Keyword.t()
) :: :ok | ErrorMessage.t()

Stores a value in the cache using the strategy's routing/layering logic.

Functions

strategy?(module)

@spec strategy?(module()) :: boolean()

Returns true if the given module implements the Cache.Strategy behaviour.