Работа над журналом для ст. мед сестер
This commit is contained in:
64
app/Infrastructure/Reports/Services/ReportRuntimeService.php
Normal file
64
app/Infrastructure/Reports/Services/ReportRuntimeService.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user