StoredFrequencyCapPolicy class

FrequencyCapPolicy implementation — memory-authoritative (4.0 audit).

The in-memory state is the single source of truth for every decision:

  • Persisted history/last-impression state is hydrated once (bounded — a hanging or corrupt store degrades to session-only capping rather than blocking every full-screen ad).
  • After hydration, canShow and recordImpression decide/mutate synchronously in memory: an impression recorded by one controller's (un-awaited) dismiss handler binds the very next check from any other controller. Before this, min-gap/hourly state was read back from the store, so a check racing an in-flight write could let two full-screen ads run back to back — the exact thing the global gap exists to prevent.
  • Persistence is write-behind and serialized: each record enqueues a snapshot on a single write chain, so concurrent read-modify-write races (which used to lose impressions on disk) cannot happen, and a throwing store never surfaces or breaks the chain.

Cap semantics: session counts (maxPerSession) are per process lifetime and never persisted; hourly counts (maxPerHour) and minimum gaps (minGap) survive restarts via KeyValueStore. Timestamps from a clock that was ahead (dead RTC, user skipping cooldowns) are ignored and pruned — a future-dated stamp must never block ads forever (ADR-037). The clock is injectable for tests.

Durability note: impressions recorded in the last moments before the process dies may not have persisted (write-behind) — the cost is a slightly loose cap after a crash, never a wrong in-session decision.

Implemented types

Constructors

StoredFrequencyCapPolicy({required KeyValueStore store, required Map<String, FrequencyCap> slotCaps, required FrequencyCap globalCap, Set<String> globalCapExemptSlots = const {}, DateTime now()?})
Creates a policy.

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

allowsNow(String slot) → bool
The same answer, synchronously, from what is known right now — a hint for UI (FullScreenAdController.canShow). canShow is the authority.
override
canShow(String slot) → Future<bool>
Whether slot may show now (its own cap AND the global cap).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
recordImpression(String slot) → Future<void>
Records an impression for slot (and globally).
override
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited