Files
wiki_travel_site/app/Console/Commands/ClearCitiesCache.php
T

60 lines
1.7 KiB
PHP
Raw Normal View History

2026-04-28 16:10:27 +03:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Attributes\Description;
use Illuminate\Console\Attributes\Signature;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
#[Signature('cache:clear-cities')]
#[Description('Clear all cached city and attraction data')]
class ClearCitiesCache extends Command
{
/**
* Execute the console command.
*/
public function handle(): int
{
$this->info('Clearing cities cache...');
try {
// Get the Redis connection used by cache
$redis = Cache::getStore()->getRedis();
$prefix = config('cache.prefix');
// Get all cache keys that match our patterns
$patterns = [
'wikipedia:*',
'opentripmap:*',
'overpass:*',
];
$clearedCount = 0;
foreach ($patterns as $pattern) {
$fullPattern = $prefix . $pattern;
$keys = $redis->keys($fullPattern);
foreach ($keys as $key) {
// Remove the prefix to get the actual cache key
$cacheKey = str_replace($prefix, '', $key);
Cache::forget($cacheKey);
$clearedCount++;
}
}
if ($clearedCount === 0) {
$this->comment('No cache entries found to clear.');
} else {
$this->info("Successfully cleared {$clearedCount} cache entries.");
}
return Command::SUCCESS;
} catch (\Exception $e) {
$this->error("Error clearing cache: " . $e->getMessage());
return Command::FAILURE;
}
}
}