PgFlowDashboard.Cache.MetricsCache (PgFlow v0.1.0)

Copy Markdown View Source

ETS-based cache for expensive dashboard aggregations.

Provides TTL-based caching with automatic expiration to reduce database load for frequently accessed metrics.

Summary

Functions

Returns a specification to start this module under a supervisor.

Gets a cached value, or computes and caches it if not present or expired.

Invalidates all cache entries matching a pattern.

Starts the metrics cache.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

fetch(key, compute_fn, opts \\ [])

@spec fetch(term(), (-> term()), keyword()) :: term()

Gets a cached value, or computes and caches it if not present or expired.

Uses a "single-flight" pattern to prevent cache stampede: when a cache miss occurs, the computation is serialized through the GenServer so only one caller computes the value while others wait for the result.

Options

  • :ttl - Time-to-live in milliseconds. Default: 5000ms.

Examples

MetricsCache.fetch(:overview_metrics, fn -> Queries.get_overview_metrics(repo) end)

invalidate_pattern(pattern)

@spec invalidate_pattern(term()) :: :ok

Invalidates all cache entries matching a pattern.

Examples

MetricsCache.invalidate_pattern(:overview_metrics)
MetricsCache.invalidate_pattern({:flow_stats, _})

start_link(opts \\ [])

Starts the metrics cache.