From ec979d6d7955dfb92b07bd1a87e095941ff80280 Mon Sep 17 00:00:00 2001 From: brusnitsyn Date: Tue, 10 Feb 2026 08:56:45 +0900 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=B0=D0=BD=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=BE=D1=87=D0=B8=D1=81=D1=82=D0=BA=D0=B8=20=D0=BA=D0=B5=D1=88?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D1=81=D1=82=D0=B0=D1=82=D0=B8?= =?UTF-8?q?=D1=81=D1=82=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/ClearStatisticsCache.php | 196 ++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 app/Console/Commands/ClearStatisticsCache.php diff --git a/app/Console/Commands/ClearStatisticsCache.php b/app/Console/Commands/ClearStatisticsCache.php new file mode 100644 index 0000000..19545df --- /dev/null +++ b/app/Console/Commands/ClearStatisticsCache.php @@ -0,0 +1,196 @@ +option('all')) { + // Очищаем весь кэш статистики + $this->clearAllStatisticsCache(); + $this->info('✅ Весь кэш статистики очищен'); + return 0; + } + + if ($departmentId = $this->option('department')) { + // Очищаем кэш для конкретного отделения + $this->clearDepartmentCache($departmentId); + $this->info("✅ Кэш статистики для отдела {$departmentId} очищен"); + return 0; + } + + if ($date = $this->option('date')) { + // Очищаем кэш за конкретную дату + $this->clearDateCache($date); + $this->info("✅ Кэш статистики за {$date} очищен"); + return 0; + } + + // Если опции не указаны, показываем помощь + $this->showHelp(); + return 1; + } + + /** + * Очистить весь кэш статистики + */ + private function clearAllStatisticsCache(): void + { + // Способ 1: Если используем теги + if (method_exists(Cache::store(), 'tags')) { + Cache::tags(['statistics'])->flush(); + } + // Способ 2: Очищаем все ключи со статистикой + else { + $this->clearStatisticsKeys(); + } + + $this->info("Очищено: весь кэш статистики"); + } + + /** + * Очистить кэш для отделения + */ + private function clearDepartmentCache(int $departmentId): void + { + if (method_exists(Cache::store(), 'tags')) { + Cache::tags(['statistics', 'department_' . $departmentId])->flush(); + } else { + // Ищем и удаляем ключи для отдела + $keys = Cache::get('statistics_keys_' . $departmentId, []); + foreach ($keys as $key) { + Cache::forget($key); + } + Cache::forget('statistics_keys_' . $departmentId); + } + + $this->info("Очищено: кэш для отдела {$departmentId}"); + } + + /** + * Очистить кэш за дату + */ + private function clearDateCache(string $date): void + { + if (method_exists(Cache::store(), 'tags')) { + Cache::tags(['statistics', 'date_' . $date])->flush(); + } else { + // Ищем ключи с этой датой + $prefix = config('cache.prefix', 'laravel'); + $pattern = "{$prefix}:statistics:*{$date}*"; + $this->clearKeysByPattern($pattern); + } + + $this->info("Очищено: кэш за дату {$date}"); + } + + /** + * Очистить ключи по шаблону (для Redis) + */ + private function clearKeysByPattern(string $pattern): void + { + $store = Cache::getStore(); + + if ($store instanceof \Illuminate\Cache\RedisStore) { + $redis = $store->getRedis(); + $keys = $redis->keys($pattern); + + if (!empty($keys)) { + $redis->del($keys); + $this->info("Удалено ключей: " . count($keys)); + } + } + } + + /** + * Очистить все ключи статистики + */ + private function clearStatisticsKeys(): void + { + $prefix = config('cache.prefix', 'laravel'); + + // Для Redis + $store = Cache::getStore(); + if ($store instanceof \Illuminate\Cache\RedisStore) { + $redis = $store->getRedis(); + $keys = $redis->keys("{$prefix}:statistics:*"); + + if (!empty($keys)) { + $redis->del($keys); + $this->info("Удалено ключей статистики: " . count($keys)); + } + } + // Для файлового кэша + elseif ($store instanceof \Illuminate\Cache\FileStore) { + $directory = storage_path('framework/cache/data'); + $this->clearFileCache($directory, 'statistics'); + } + } + + /** + * Очистить файловый кэш + */ + private function clearFileCache(string $directory, string $pattern): void + { + if (!is_dir($directory)) { + return; + } + + $files = glob($directory . '/*'); + $deleted = 0; + + foreach ($files as $file) { + if (is_file($file)) { + $content = file_get_contents($file); + if (strpos($content, $pattern) !== false) { + unlink($file); + $deleted++; + } + } + } + + $this->info("Удалено файлов кэша: " . $deleted); + } + + /** + * Показать справку + */ + private function showHelp(): void + { + $this->error('❌ Не указаны параметры команды'); + $this->line(''); + $this->line('📋 Доступные опции:'); + $this->line(' --all Очистить весь кэш статистики'); + $this->line(' --department=ID Очистить кэш для конкретного отдела'); + $this->line(' --date=YYYY-MM-DD Очистить кэш за конкретную дату'); + $this->line(''); + $this->line('📝 Примеры использования:'); + $this->line(' php artisan statistics:clear-cache --all'); + $this->line(' php artisan statistics:clear-cache --department=1'); + $this->line(' php artisan statistics:clear-cache --date=2024-01-15'); + } +}