65 lines
1.9 KiB
PHP
65 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Infrastructure\Reports\Services;
|
|
|
|
use App\Models\MisMedicalHistory;
|
|
use App\Models\MisMigrationPatient;
|
|
use App\Models\MisStationarBranch;
|
|
use App\Models\Report;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Runtime-обвязка тяжёлых report-save операций: память, query log и cache.
|
|
*/
|
|
class ReportRuntimeService
|
|
{
|
|
public function prepareForHeavySave(): void
|
|
{
|
|
$connectionNames = array_unique(array_filter([
|
|
DB::getDefaultConnection(),
|
|
(new MisMedicalHistory)->getConnectionName(),
|
|
(new MisMigrationPatient)->getConnectionName(),
|
|
(new MisStationarBranch)->getConnectionName(),
|
|
]));
|
|
|
|
foreach ($connectionNames as $connectionName) {
|
|
try {
|
|
$connection = DB::connection($connectionName);
|
|
$connection->disableQueryLog();
|
|
$connection->flushQueryLog();
|
|
} catch (\Throwable) {
|
|
// best-effort cleanup only
|
|
}
|
|
}
|
|
|
|
if (function_exists('gc_collect_cycles')) {
|
|
gc_collect_cycles();
|
|
}
|
|
}
|
|
|
|
public function clearCacheAfterReportCreation(User $user, Report $report): void
|
|
{
|
|
$this->clearDailyCache($user, $report->created_at);
|
|
}
|
|
|
|
private function clearDailyCache(User $user, mixed $reportDate): void
|
|
{
|
|
$datesToClear = [
|
|
Carbon::parse($reportDate)->format('Y-m-d'),
|
|
Carbon::parse($reportDate)->subDay()->format('Y-m-d'),
|
|
];
|
|
|
|
foreach ($datesToClear as $date) {
|
|
Cache::forget($this->generateDailyCacheKey($user, $date));
|
|
}
|
|
}
|
|
|
|
private function generateDailyCacheKey(User $user, string $date): string
|
|
{
|
|
return 'daily_stats:'.$user->rf_department_id.':'.$date;
|
|
}
|
|
}
|