Penambahn P1 Dev
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\P1Dev;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class P1DataCache
|
||||
{
|
||||
public function key(string $kind, int|string $year, int|string $month, mixed $unit, string $employeeType): string
|
||||
{
|
||||
$unitKey = $unit === null || $unit === '' ? 'all' : (string) ((int) $unit === 0 ? 99 : (int) $unit);
|
||||
|
||||
return 'p1_snapshot_v1:'.(int) $year.':'.(int) $month.':'.$unitKey.':'.$kind.':'.$employeeType;
|
||||
}
|
||||
|
||||
public function remember(string $kind, int|string $year, int|string $month, mixed $unit, string $employeeType, Closure $load): array
|
||||
{
|
||||
return Cache::rememberForever($this->key($kind, $year, $month, $unit, $employeeType), $load);
|
||||
}
|
||||
|
||||
public function forget(int|string $year, int|string $month, mixed $unit): void
|
||||
{
|
||||
foreach (['', 'dosen', 'tendik', 'jtt', 'jtt-univ'] as $type) {
|
||||
foreach (['main', 'selisih', 'perbandingan'] as $kind) {
|
||||
Cache::forget($this->key($kind, $year, $month, $unit, $type));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function refresh(int|string $year, int|string $month, mixed $unit, string $employeeType, Closure $main, Closure $selisih): void
|
||||
{
|
||||
// Keep the previous snapshot intact if an upstream request fails.
|
||||
$mainData = $main();
|
||||
$selisihData = $selisih();
|
||||
$this->forget($year, $month, $unit);
|
||||
Cache::forever($this->key('main', $year, $month, $unit, $employeeType), $mainData);
|
||||
Cache::forever($this->key('selisih', $year, $month, $unit, $employeeType), $selisihData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user