PhoenixKitWebAnalytics.ReportCache (PhoenixKitWebAnalytics v0.5.0)

Copy Markdown View Source

A short-lived cache for report results, so a busy site's admin pages don't re-run the same aggregates for every viewer and every refresh.

Finished days come from rollups and are cheap; what costs is the slice of raw events not rolled up yet (today). Caching a report for a few seconds means that slice is aggregated at most once per interval per filter, however many admins have the Overview open.

One computation per key at a time: when a report expires, the first caller recomputes it while everyone else gets the previous result (or, when there is none yet, waits for the one being computed) — so a restart or a dozen dashboards refreshing together cost one query, not a dozen.

30 seconds by default (config :phoenix_kit_web_analytics, report_cache_ms: ms); 0 turns it off (the test suite does). The table belongs to this process; without it running, fetch/3 just computes.

Summary

Functions

Returns a specification to start this module under a supervisor.

Drops every cached report (after a settings change or a rollup).

The cached value for key, or fun.() computed and stored for ttl_ms (the configured interval by default).

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

clear()

@spec clear() :: :ok

Drops every cached report (after a settings change or a rollup).

fetch(key, fun, ttl_ms \\ nil)

@spec fetch(term(), (-> value), non_neg_integer() | nil) :: value when value: term()

The cached value for key, or fun.() computed and stored for ttl_ms (the configured interval by default).