Профиль хирургии
This commit is contained in:
45
app/Services/Base/BaseMetricService.php
Normal file
45
app/Services/Base/BaseMetricService.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
// app/Services/Base/BaseMetricService.php
|
||||
|
||||
namespace App\Services\Base;
|
||||
|
||||
use App\Contracts\MetricCalculatorInterface;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
abstract class BaseMetricService
|
||||
{
|
||||
protected array $cache = [];
|
||||
protected array $config = [];
|
||||
|
||||
abstract protected function getMetricId(): int;
|
||||
abstract protected function calculate(array $departmentIds, string $startDate, string $endDate): array;
|
||||
|
||||
public function getCached(array $departmentIds, string $startDate, string $endDate): array
|
||||
{
|
||||
$cacheKey = $this->getCacheKey($departmentIds, $startDate, $endDate);
|
||||
|
||||
if (isset($this->cache[$cacheKey])) {
|
||||
return $this->cache[$cacheKey];
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->calculate($departmentIds, $startDate, $endDate);
|
||||
$this->cache[$cacheKey] = $result;
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
Log::error("Error in " . static::class . ": " . $e->getMessage());
|
||||
return array_fill_keys($departmentIds, 0);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getCacheKey(array $departmentIds, string $startDate, string $endDate): string
|
||||
{
|
||||
return static::class . '_' . md5(implode(',', $departmentIds) . $startDate . $endDate);
|
||||
}
|
||||
|
||||
public function clearCache(): void
|
||||
{
|
||||
$this->cache = [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user