|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Frosh\Tools\Command; |
| 6 | + |
| 7 | +use Frosh\Tools\Components\CacheRegistry; |
| 8 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 9 | +use Symfony\Component\Console\Command\Command; |
| 10 | +use Symfony\Component\Console\Input\InputInterface; |
| 11 | +use Symfony\Component\Console\Input\InputOption; |
| 12 | +use Symfony\Component\Console\Output\OutputInterface; |
| 13 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 14 | + |
| 15 | +#[AsCommand( |
| 16 | + name: 'frosh:redis-namespace:cleanup', |
| 17 | + description: 'Delete all Redis namespaces except the active one [Experimental]', |
| 18 | +)] |
| 19 | +class RedisNamespaceCleanupCommand extends Command |
| 20 | +{ |
| 21 | + public function __construct(private readonly CacheRegistry $cacheRegistry) |
| 22 | + { |
| 23 | + parent::__construct(); |
| 24 | + } |
| 25 | + |
| 26 | + protected function configure(): void |
| 27 | + { |
| 28 | + $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show what would be deleted without actually deleting'); |
| 29 | + } |
| 30 | + |
| 31 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 32 | + { |
| 33 | + $io = new SymfonyStyle($input, $output); |
| 34 | + $cacheAdapter = $this->cacheRegistry->get('cache.object'); |
| 35 | + $dryRun = $input->getOption('dry-run'); |
| 36 | + |
| 37 | + try { |
| 38 | + $redis = $cacheAdapter->getRedisOrFail(); |
| 39 | + } catch (\RuntimeException $e) { |
| 40 | + $io->error($e->getMessage()); |
| 41 | + |
| 42 | + return Command::FAILURE; |
| 43 | + } |
| 44 | + |
| 45 | + $activeNamespace = $cacheAdapter->getNamespace(); |
| 46 | + $io->title('Redis Namespace Cleanup'); |
| 47 | + $io->writeln(\sprintf('Active namespace: <info>%s</info>', $activeNamespace)); |
| 48 | + |
| 49 | + if ($dryRun) { |
| 50 | + $io->note('Running in dry-run mode - no keys will be deleted'); |
| 51 | + } |
| 52 | + |
| 53 | + // Group keys by namespace (first 10 characters) |
| 54 | + $namespaces = []; |
| 55 | + $totalKeys = 0; |
| 56 | + $keysToDelete = []; |
| 57 | + |
| 58 | + // Use SCAN to iterate through all keys efficiently |
| 59 | + $iterator = null; |
| 60 | + do { |
| 61 | + $keys = $redis->scan($iterator, null, 1000); |
| 62 | + if ($keys === false) { |
| 63 | + break; |
| 64 | + } |
| 65 | + |
| 66 | + foreach ($keys as $key) { |
| 67 | + ++$totalKeys; |
| 68 | + $namespace = substr($key, 0, 10); |
| 69 | + |
| 70 | + if (!isset($namespaces[$namespace])) { |
| 71 | + $namespaces[$namespace] = 0; |
| 72 | + } |
| 73 | + ++$namespaces[$namespace]; |
| 74 | + |
| 75 | + // Track keys that are not in the active namespace |
| 76 | + if ($namespace !== $activeNamespace) { |
| 77 | + $keysToDelete[] = $key; |
| 78 | + } |
| 79 | + } |
| 80 | + } while ($iterator > 0); |
| 81 | + |
| 82 | + // Display namespace summary |
| 83 | + $tableData = []; |
| 84 | + foreach ($namespaces as $namespace => $count) { |
| 85 | + $status = $namespace === $activeNamespace ? 'KEEP' : 'DELETE'; |
| 86 | + $tableData[] = [$namespace, $count, $status]; |
| 87 | + } |
| 88 | + |
| 89 | + $io->section('Namespace Summary'); |
| 90 | + $io->table( |
| 91 | + ['Namespace', 'Key Count', 'Action'], |
| 92 | + $tableData |
| 93 | + ); |
| 94 | + |
| 95 | + $deleteCount = \count($keysToDelete); |
| 96 | + |
| 97 | + if ($deleteCount === 0) { |
| 98 | + $io->success('No keys to delete - only the active namespace exists'); |
| 99 | + |
| 100 | + return Command::SUCCESS; |
| 101 | + } |
| 102 | + |
| 103 | + $io->writeln(\sprintf('Keys to delete: <comment>%d</comment> out of <comment>%d</comment> total keys', $deleteCount, $totalKeys)); |
| 104 | + |
| 105 | + if (!$dryRun) { |
| 106 | + if (!$io->confirm('Do you want to proceed with deleting these keys?', false)) { |
| 107 | + $io->warning('Operation cancelled'); |
| 108 | + |
| 109 | + return Command::SUCCESS; |
| 110 | + } |
| 111 | + |
| 112 | + $io->progressStart($deleteCount); |
| 113 | + |
| 114 | + // Delete keys in batches for better performance |
| 115 | + $batchSize = 1000; |
| 116 | + for ($i = 0; $i < $deleteCount; $i += $batchSize) { |
| 117 | + $batch = \array_slice($keysToDelete, $i, $batchSize); |
| 118 | + $redis->del(...$batch); |
| 119 | + $io->progressAdvance(\count($batch)); |
| 120 | + } |
| 121 | + |
| 122 | + $io->progressFinish(); |
| 123 | + $io->success(\sprintf('Successfully deleted %d keys from inactive namespaces', $deleteCount)); |
| 124 | + } else { |
| 125 | + $io->warning(\sprintf('Dry run complete - would have deleted %d keys', $deleteCount)); |
| 126 | + } |
| 127 | + |
| 128 | + return Command::SUCCESS; |
| 129 | + } |
| 130 | +} |
0 commit comments