File: /home/xedaptot/ai.naniguide.com/database/seeders/Demo/Stage21_CacheWarmer.php
<?php
namespace Database\Seeders\Demo;
use App\Library\Cache\AppCache;
use App\Model\Campaign;
use App\Model\Customer;
use App\Model\MailList;
use App\Model\Segment;
use App\Services\Admin\AdminDashboardCache;
/**
* Stage 21 — warm AppCache manifest entries across the whole demo dataset.
*
* Cacheable models (Campaign, MailList, LocalCustomer, Segment)
* expose pre-computed counters through `AppCache::for($model)->read(...)`.
* The application normally refreshes these via lifecycle hooks (campaign
* finally-callback, etc.). DemoSeeder writes rows directly, so we call
* `AppCache::for($x)->refresh()` here as the LAST stage to make dashboards
* and reports show real numbers instead of 0.
*
* Order matters:
* 1. Segments (used by lists)
* 2. Mail lists (subscriber counts, used by campaigns)
* 3. Campaigns (need list counts)
* 4. Customers (LocalCustomer reads from lists)
*/
class Stage21_CacheWarmer extends AbstractDemoSeeder
{
public function run(): void
{
$this->time('warm segments cache', function () {
$count = 0;
Segment::query()->chunk(100, function ($segments) use (&$count) {
foreach ($segments as $s) {
try { AppCache::for($s)->refresh(); $count++; } catch (\Throwable $e) {}
}
});
$this->log(" → {$count} segments");
});
$this->time('warm mail_lists cache', function () {
$count = 0;
MailList::query()->chunk(50, function ($lists) use (&$count) {
foreach ($lists as $list) {
try { AppCache::for($list)->refresh(); $count++; } catch (\Throwable $e) {
$this->log(" list #{$list->id} refresh failed: " . $e->getMessage());
}
}
});
$this->log(" → {$count} mail lists");
});
$this->time('warm campaigns cache', function () {
$count = 0;
Campaign::query()->chunk(50, function ($campaigns) use (&$count) {
foreach ($campaigns as $c) {
try { AppCache::for($c)->refresh(); $count++; } catch (\Throwable $e) {
$this->log(" campaign #{$c->id} refresh failed: " . $e->getMessage());
}
}
});
$this->log(" → {$count} campaigns");
});
$this->time('warm customers cache', function () {
$count = 0;
foreach (Customer::all() as $customer) {
try {
// LocalCustomer holds the cache, not Customer.
$local = $customer->local();
if ($local) {
AppCache::for($local)->refresh();
$count++;
}
} catch (\Throwable $e) {
$this->log(" customer #{$customer->id} refresh failed: " . $e->getMessage());
}
}
$this->log(" → {$count} customers");
});
$this->time('warm admin dashboard cache', function () {
$scope = AppCache::for(AdminDashboardCache::instance());
foreach (['SentByDay_7', 'SentByDay_30', 'SentByDay_90'] as $key) {
$scope->refresh($key);
}
$this->log(' → SentByDay_{7,30,90}');
});
$this->log('cache warming complete — campaign reports + dashboards now show real numbers');
}
}