97 lines
3.9 KiB
PHP
97 lines
3.9 KiB
PHP
<?php
|
||
use App\Jobs\CacheBladeViewJob;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
/**
|
||
* Dispatch cache blade view jobs
|
||
*
|
||
* This helper function dispatches CacheBladeViewJob for specified views.
|
||
* It can be used to refresh cached blade views after data updates.
|
||
*
|
||
* @param array $cacheViews Optional array of cache views to dispatch.
|
||
* If provided, only these views will be dispatched (default views will be ignored).
|
||
* If empty, default cache views will be dispatched.
|
||
* Format: [['view' => 'view.path', 'cache' => 'cache-name'], ...]
|
||
* @return void
|
||
*/
|
||
function dispatchCacheBladeViews(array $cacheViews = [])
|
||
{
|
||
|
||
// Default cache views that should be refreshed after spool-related operations
|
||
$defaultCacheViews = [
|
||
|
||
[
|
||
'view' => 'admin-ajax.spool-area-release-no-cache',
|
||
'cache' => 'spool-area-release'
|
||
],
|
||
[
|
||
'view' => 'admin-ajax.spool-list-no-cache',
|
||
'cache' => 'spool-list'
|
||
],
|
||
[
|
||
'view' => 'admin.type.spool-release.spool-list-chart-no-cache',
|
||
'cache' => 'spool-list-chart'
|
||
],
|
||
];
|
||
|
||
// If cacheViews is empty, use defaultCacheViews; otherwise use only cacheViews
|
||
if (empty($cacheViews)) {
|
||
$uniqueCacheViews = $defaultCacheViews;
|
||
} else {
|
||
// Remove duplicates based on cache name (keep the last one)
|
||
$seen = [];
|
||
$uniqueCacheViews = [];
|
||
|
||
// Reverse to process from end, so we keep the last occurrence
|
||
foreach (array_reverse($cacheViews) as $cacheView) {
|
||
// Validate array structure
|
||
if (!is_array($cacheView) || !isset($cacheView['cache']) || !isset($cacheView['view'])) {
|
||
continue;
|
||
}
|
||
|
||
$cacheName = $cacheView['cache'];
|
||
if (!in_array($cacheName, $seen)) {
|
||
$seen[] = $cacheName;
|
||
$uniqueCacheViews[] = $cacheView;
|
||
}
|
||
}
|
||
|
||
// Reverse back to original order
|
||
$uniqueCacheViews = array_reverse($uniqueCacheViews);
|
||
}
|
||
|
||
// Dispatch job for each cache view
|
||
foreach ($uniqueCacheViews as $cacheView) {
|
||
if (isset($cacheView['view']) && isset($cacheView['cache'])) {
|
||
try {
|
||
// Rate Limiting (Debounce): Aynı cache view için 60 saniyede bir kez job oluştur.
|
||
// Batch excel gibi aynı anda binlerce satırın import edildiği senaryolarda kuyruğun dolmasını engeller.
|
||
$rateLimitKey = "rate_limit_dispatch_{$cacheView['cache']}";
|
||
|
||
if (!\Illuminate\Support\Facades\Cache::has($rateLimitKey)) {
|
||
// 60 saniyeliğine kilitle
|
||
\Illuminate\Support\Facades\Cache::put($rateLimitKey, true, 60);
|
||
|
||
// Job'u 15 saniye gecikmeli gönder (arka plandaki güncellemelerin bitmesi için zaman tanı)
|
||
CacheBladeViewJob::dispatch($cacheView['view'], $cacheView['cache'])->delay(now()->addSeconds(15));
|
||
|
||
Log::info('CacheBladeViewJob dispatched to queue with 15s delay (Rate limit started)', [
|
||
'view' => $cacheView['view'],
|
||
'cache' => $cacheView['cache']
|
||
]);
|
||
} else {
|
||
// Log::debug('CacheBladeViewJob skipped due to rate limit (debounce)', ['cache' => $cacheView['cache']]);
|
||
}
|
||
} catch (\Exception $e) {
|
||
// Silently fail to prevent breaking the main operation
|
||
Log::error('Failed to dispatch CacheBladeViewJob', [
|
||
'view' => $cacheView['view'],
|
||
'cache' => $cacheView['cache'],
|
||
'error' => $e->getMessage()
|
||
]);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|