197 lines
6.4 KiB
PHP
197 lines
6.4 KiB
PHP
<?php
|
||
|
||
namespace App\Console\Commands;
|
||
|
||
use Illuminate\Console\Command;
|
||
use Illuminate\Support\Facades\Cache;
|
||
|
||
class ClearStatisticsCache extends Command
|
||
{
|
||
/**
|
||
* The name and signature of the console command.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $signature = 'statistics:clear-cache
|
||
{--department= : ID отделения}
|
||
{--date= : Дата в формате YYYY-MM-DD}
|
||
{--all : Очистить весь кэш статистики}';
|
||
|
||
/**
|
||
* The description of the console command.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $description = 'Очистить кэш статистики';
|
||
|
||
/**
|
||
* Execute the console command.
|
||
*/
|
||
public function handle()
|
||
{
|
||
if ($this->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');
|
||
}
|
||
}
|