addSeconds(self::TTL_SECONDS), static function (): array { $counts = Product::query()->toBase() ->selectRaw('status as state, count(*) as aggregate') ->groupBy('status') ->pluck('aggregate', 'state'); return [ 'total' => (int) $counts->sum(), 'draft' => (int) ($counts['draft'] ?? 0), 'active' => (int) ($counts['active'] ?? 0), 'archived' => (int) ($counts['archived'] ?? 0), ]; }); } /** * Call after any write the observer cannot see, as bulk queries fire no * model events; it announces the change to open screens and bumps the * version, retiring every viewer scope's cached counts at once. */ public static function forget(): void { Cache::forever(self::CACHE_KEY.'.v', self::version() + 1); ModuleSignal::changed('product'); } /** One entry per version and per viewer scope — a scoped user never reads another scope's totals. */ private static function key(): string { return self::CACHE_KEY.'.'.self::version().StatsScope::suffix(); } /** * Get the cache version the counts are stored under. */ private static function version(): int { return (int) Cache::get(self::CACHE_KEY.'.v', 0); } }