CaiWuXiTong/app/Console/Commands/WarmReportCaches.php
WuYongJie 374df29d2b 修复生产报表负载根因 (#257)
消除报表缓存穿透与后台千万级全表计数,降低同步频率并调整共享数据库资源。
2026-09-23 20:15:59 +08:00

50 lines
1.9 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Modules\EcommerceFinance\Repositories\StoreReceivingAlipayBillMirrorRepository;
use Modules\EcommerceFinance\Repositories\ThirdPartyWarehouseStorageMirrorRepository;
use Throwable;
class WarmReportCaches extends Command
{
protected $signature = 'business-mirror:warm-report-caches';
protected $description = '预热默认进销存与淘系支付宝报表,避免镜像切换后的首批请求重复扫表';
public function handle(
ThirdPartyWarehouseStorageMirrorRepository $inventory,
StoreReceivingAlipayBillMirrorRepository $alipay,
): int {
$failed = false;
try {
$month = now()->startOfMonth()->subMonth()->format('Y-m');
$result = $inventory->paginateMonthlyInventoryReport([
'month' => $month,
'product_code' => '',
'warehouse_provider' => '',
'category' => '',
], 1, 25);
$this->info(sprintf('进销存 %s 首页已预热,共 %d 行。', $month, $result['total']));
} catch (Throwable $exception) {
$failed = true;
Log::error('默认进销存报表缓存预热失败。', ['exception' => $exception::class]);
$this->error('默认进销存报表缓存预热失败。');
}
try {
$result = $alipay->paginate([], 'occurred_at', 'desc', 1, 10);
$this->info(sprintf('淘系支付宝首页已预热,共 %d 行。', $result['total']));
} catch (Throwable $exception) {
$failed = true;
Log::error('淘系支付宝报表缓存预热失败。', ['exception' => $exception::class]);
$this->error('淘系支付宝报表缓存预热失败。');
}
return $failed ? self::FAILURE : self::SUCCESS;
}
}