Files
citrus-cms/app/Functions/cache-blade-views.php
2026-04-28 21:14:25 +03:00

97 lines
3.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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()
]);
}
}
}
}