PostHog.FeatureFlags.FlagDefinitionCacheProvider behaviour (posthog v2.15.0)

Copy Markdown View Source

Optional shared cache contract for local feature flag definitions.

Configure a provider as {module, provider_state} using :flag_definition_cache_provider. The definition loader bounds and isolates every callback. Cached values must be maps containing flags, group_type_mapping, and cohorts (string or atom keys are accepted).

A minimal provider can coordinate fetching and keep the complete envelope in an application-owned cache:

defmodule MyApp.PostHogDefinitionCache do
  @behaviour PostHog.FeatureFlags.FlagDefinitionCacheProvider

  def should_fetch_flag_definitions(state), do: state.fetch_owner?.()
  def get_flag_definitions(state), do: state.read.()
  def on_flag_definitions_received(state, definitions), do: state.write.(definitions)
  def shutdown(_state), do: :ok
end

config :posthog,
  secret_key: System.fetch_env!("POSTHOG_SECRET_KEY"),
  flag_definition_cache_provider: {MyApp.PostHogDefinitionCache, provider_state}

Summary

Callbacks

Reads definitions from the shared cache, or returns nil when it is empty.

Stores a complete definition envelope fetched from PostHog.

Returns whether this SDK instance should fetch fresh definitions from PostHog.

Releases resources owned by the provider.

Types

definitions()

@type definitions() :: map()

provider_state()

@type provider_state() :: any()

Callbacks

get_flag_definitions(provider_state)

@callback get_flag_definitions(provider_state()) :: definitions() | nil

Reads definitions from the shared cache, or returns nil when it is empty.

on_flag_definitions_received(provider_state, definitions)

@callback on_flag_definitions_received(provider_state(), definitions()) :: :ok

Stores a complete definition envelope fetched from PostHog.

should_fetch_flag_definitions(provider_state)

@callback should_fetch_flag_definitions(provider_state()) :: boolean()

Returns whether this SDK instance should fetch fresh definitions from PostHog.

shutdown(provider_state)

@callback shutdown(provider_state()) :: :ok

Releases resources owned by the provider.