Nebulex v2.0.0-rc.0 Nebulex.Cache.Stats View Source

This module defines the supported built-in stats.

By default, each adapter is responsible for providing stats support. However, Nebulex suggests supporting the built-in stats described in this module, which are also supported by the built-in adapters.

Usage

First of all, we define a cache:

defmodule MyApp.Cache do
  use Nebulex.Cache,
    otp_app: :nebulex,
    adapter: Nebulex.Adapters.Local
end

Then we configure it enabling the stats, like so:

config :my_app, MyApp.Cache,
  stats: true,
  gc_interval: 86_400_000, #=> 1 day
  max_size: 200_000,
  gc_cleanup_min_timeout: 10_000,
  gc_cleanup_max_timeout: 900_000

Remember to add the cache on your application's supervision tree.

Since we are using a built-in adapter and the stats have been enabled (stats: true), the stat counters will be automatically fed by the adapter.

You can ask for the current stats values at any time by calling:

Nebulex.Cache.Stats.info(MyApp.Cache)

Using stats helpers

You can inject the stats helpers in the cache like this:

defmodule MyApp.Cache do
  use Nebulex.Cache,
    otp_app: :nebulex,
    adapter: Nebulex.Adapters.Local

  # Use stats helpers
  use Nebulex.Cache.Stats
end

Retrieving stats info

MyApp.Cache.stats_info()

By calling this injected helper, the function Nebulex.Cache.Stats.info/1 is called under-the-hood, but the cache name is resolved automatically.

Dispatching telemetry events

MyApp.Cache.dispatch_stats()

MyApp.Cache.dispatch_stats(event_prefix: [:my_cache, :stats])

By calling this injected helper, the function Nebulex.Cache.Stats.dispatch/2 is called under-the-hood, but the cache name is resolved automatically.

Telemetry events

Integrating telemetry is very easy since with the helper function MyApp.Cache.dispatch_stats/1 (via the __using__ macro) described previously you can emit telemetry events with the current stats at any time. What we need to resolve is, how to make it in such a way that every X period of time the stats are emitted automatically.

To do so, we can use :telemetry_poller and define a custom measurement:

:telemetry_poller.start_link(
  measurements: [
    {MyApp.Cache, :dispatch_stats, []},
  ],
  # configure sampling period - default is :timer.seconds(5)
  period: :timer.seconds(10),
  name: :my_cache_stats_poller
)

Or you can also start the :telemetry_poller process along with your application supervision tree, like so:

def start(_type, _args) do
  my_cache_stats_poller_opts = [
    measurements: [
      {MyApp.Cache, :dispatch_stats, []},
    ],
    period: :timer.seconds(10),
    name: :my_cache_stats_poller
  ]

  children = [
    {MyApp.Cache, []},
    {:telemetry_poller, my_cache_stats_poller_opts}
  ]

  opts = [strategy: :one_for_one, name: MyApp.Supervisor]
  Supervisor.start_link(children, opts)
end

See Nebulex Telemetry Guide.

Link to this section Summary

Functions

Emits a telemetry event when called with the current stats count.

Increments the counter's stat stat by the given incr value.

Returns the struct Nebulex.Cache.Stats with the current stats values for the given cache name or counter reference. Normally, the cache name is passed so that the counter reference is retrieved and handled internally.

Initializes the Erlang's counter to be used for the calling Cache and feed the stat values; see the module documentation for more information about the supported stats.

Link to this section Types

Specs

stat() :: :hits | :misses | :writes | :evictions | :expirations

Specs

t() :: %Nebulex.Cache.Stats{
  evictions: non_neg_integer(),
  expirations: non_neg_integer(),
  hits: non_neg_integer(),
  misses: non_neg_integer(),
  writes: non_neg_integer()
}

Link to this section Functions

Link to this function

dispatch(cache_or_name, opts \\ [])

View Source

Specs

dispatch(atom(), Keyword.t()) :: :ok

Emits a telemetry event when called with the current stats count.

The :measurements map will include the current count for each stat:

  • :hits - Current hits count.
  • :misses - Current misses count.
  • :writes - Current writes count.
  • :evictions - Current evictions count.
  • :expirations - Current expirations count.

The telemetry :metadata map will include the following fields:

  • :cache - The cache module, or the name (if an explicit name has been given to the cache).

Additionally, you can add your own metadata fields by given the option :metadata.

Options

  • :event_prefix – The prefix of the telemetry event. Defaults to [:nebulex, :cache].

  • :metadata – A map with additional metadata fields. Defaults to %{}.

Examples

iex> Nebulex.Cache.Stats.dispatch(MyCache)
:ok

iex> Nebulex.Cache.Stats.dispatch(
...>   MyCache,
...>   event_prefix: [:my_cache],
...>   metadata: %{tag: "tag1"}
...> )
:ok
Link to this function

incr(counter, stat, incr \\ 1)

View Source

Specs

incr(:counters.counters_ref() | nil, stat(), integer()) :: :ok

Increments the counter's stat stat by the given incr value.

Examples

Nebulex.Cache.Stats.incr(stat_counter, :hits)

Nebulex.Cache.Stats.incr(stat_counter, :writes, 10)

NOTE: This function is normally called by the adapter in case it supports the Nebulex suggested stats; the adapter should feed Nebulex.Cache.Stats.t() counters.

See built-in adapters for more information about the usage.

Specs

info(:counters.counters_ref() | atom() | nil) :: t() | nil

Returns the struct Nebulex.Cache.Stats with the current stats values for the given cache name or counter reference. Normally, the cache name is passed so that the counter reference is retrieved and handled internally.

Returns nil if the stats are disabled or if the adapter doesn't support this feature.

Example

iex> Nebulex.Cache.Stats.info(MyCache)
%Nebulex.Cache.Stats{
  evictions: 0,
  expirations: 0,
  hits: 0,
  misses: 0,
  writes: 0
}

Specs

init(Keyword.t()) :: :counters.counters_ref() | nil

Initializes the Erlang's counter to be used for the calling Cache and feed the stat values; see the module documentation for more information about the supported stats.

Returns nil is the option :stats is set to false or it is not set at all; the stats will be skipped.

Example

Nebulex.Cache.Stats.init(opts)

NOTE: This function is normally called by the adapter in case it supports the Nebulex suggested stats; the adapter should feed Nebulex.Cache.Stats.t() counters.

See built-in adapters for more information about the usage.