| Total Complexity | 40 |
| Total Lines | 281 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ProcessCacheInvalidationEventsCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProcessCacheInvalidationEventsCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ProcessCacheInvalidationEventsCommand extends Command |
||
| 14 | { |
||
| 15 | protected $signature = 'supercache:process-invalidation |
||
| 16 | {--shard= : The shard number to process} |
||
| 17 | {--priority= : The priority level} |
||
| 18 | {--limit= : The maximum number of events to fetch per batch} |
||
| 19 | {--tag-batch-size= : The number of identifiers to process per invalidation batch} |
||
| 20 | {--connection_name= : The Redis connection name} |
||
| 21 | {--log_attivo= : Indica se il log delle operazioni è attivo (0=no, 1=si)}'; |
||
| 22 | protected $description = 'Process cache invalidation events for a given shard and priority'; |
||
| 23 | protected SuperCacheInvalidationHelper $helper; |
||
| 24 | protected bool $log_attivo = false; |
||
| 25 | protected string $connection_name = 'cache'; |
||
| 26 | protected int $tagBatchSize = 100; |
||
| 27 | protected int $limit = 1000; |
||
| 28 | protected int $priority = 0; |
||
| 29 | protected int $shardId = 0; |
||
| 30 | protected int $invalidation_window = 600; |
||
| 31 | |||
| 32 | public function __construct(SuperCacheInvalidationHelper $helper) |
||
| 36 | } |
||
| 37 | |||
| 38 | private function getEventsToInvalidate(Carbon $processingStartTime): array |
||
| 54 | ; |
||
| 55 | } |
||
| 56 | |||
| 57 | private function getStoreFromConnectionName(string $connection_name): ?string |
||
| 58 | { |
||
| 59 | // Cerca il nome dello store associato alla connessione Redis |
||
| 60 | foreach (config('cache.stores') as $storeName => $storeConfig) { |
||
| 61 | if ( |
||
| 62 | isset($storeConfig['driver'], $storeConfig['connection']) && |
||
| 63 | $storeConfig['driver'] === 'redis' && |
||
| 64 | $storeConfig['connection'] === $connection_name |
||
| 65 | ) { |
||
| 66 | return $storeName; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return null; |
||
| 71 | } |
||
| 72 | |||
| 73 | private function logIf(string $message, string $level = 'info') |
||
| 79 | } |
||
| 80 | |||
| 81 | protected function processEvents(): void |
||
| 139 | } |
||
| 140 | |||
| 141 | protected function processBatch(array $allEvents, array $eventsToInvalidate): void |
||
| 142 | { |
||
| 143 | // Separo le chiavi dai tags |
||
| 144 | $keys = []; |
||
| 145 | $tags = []; |
||
| 146 | // Prima di processare tutti gli eventi, assegno un batch_ID univoco a tutti gli eventi |
||
| 147 | $batch_ID = (string) Str::uuid(); |
||
| 148 | |||
| 149 | foreach ($eventsToInvalidate as $item) { |
||
| 150 | switch ($item->type) { |
||
| 151 | case 'key': |
||
| 152 | $keys[] = $item->identifier . '§' . $item->connection_name; |
||
| 153 | break; |
||
| 154 | case 'tag': |
||
| 155 | $tags[] = $item->identifier . '§' . $item->connection_name; |
||
| 156 | break; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->logIf('Invalido ' . count($keys) . ' chiavi e ' . count($tags) . ' tags' . ' per un totale di ' . count($allEvents) . ' events_ID'); |
||
| 161 | |||
| 162 | if (!empty($keys)) { |
||
| 163 | $this->invalidateKeys($keys); |
||
| 164 | } |
||
| 165 | |||
| 166 | if (!empty($tags)) { |
||
| 167 | // Escludo i tag fullpage |
||
| 168 | $tags = array_filter($tags, function ($item) { |
||
| 169 | return !str_contains($item, 'fullpage'); |
||
| 170 | }); |
||
| 171 | $this->invalidateTags($tags); |
||
| 172 | } |
||
| 173 | |||
| 174 | // Disabilita i controlli delle chiavi esterne e dei vincoli univoci |
||
| 175 | DB::statement('SET FOREIGN_KEY_CHECKS=0;'); |
||
| 176 | DB::statement('SET UNIQUE_CHECKS=0;'); |
||
| 177 | |||
| 178 | // Mark event as processed |
||
| 179 | // QUI NON VA USATA PARTITION perchè la cross partition è più lenta! Però è necessario utilizzare la $partition_key per sfruttare l'indice della primary key (id+partition_key) |
||
| 180 | DB::table('cache_invalidation_events') |
||
| 181 | ->whereIn('id', array_map(fn ($event) => $event->id, $allEvents)) |
||
| 182 | ->whereIn('partition_key', array_map(fn ($event) => $event->partition_key, $allEvents)) |
||
| 183 | ->update(['processed' => 1, 'batch_ID' => $batch_ID, 'updated_at' => now()]) |
||
| 184 | ; |
||
| 185 | // Riattiva i controlli |
||
| 186 | DB::statement('SET FOREIGN_KEY_CHECKS=1;'); |
||
| 187 | DB::statement('SET UNIQUE_CHECKS=1;'); |
||
| 188 | |||
| 189 | // A questo punto avviso il gescat che le chiavi/tags sono stati puliti, per cui può procedere alla pulizia della CDN |
||
| 190 | event(new BatchCompletedEvent($batch_ID, $this->shardId)); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Invalidate cache keys. |
||
| 195 | * |
||
| 196 | * @param array $keys Array of cache keys to invalidate |
||
| 197 | */ |
||
| 198 | protected function invalidateKeys(array $keys): void |
||
| 199 | { |
||
| 200 | $callback = config('super_cache_invalidate.key_invalidation_callback'); |
||
| 201 | |||
| 202 | // Anche in questo caso va fatto un loop perchè le chiavi potrebbero stare in database diversi |
||
| 203 | foreach ($keys as $keyAndConnectionName) { |
||
| 204 | [$key, $connection_name] = explode('§', $keyAndConnectionName); |
||
| 205 | |||
| 206 | // Metodo del progetto |
||
| 207 | if (is_callable($callback)) { |
||
| 208 | $callback($key, $connection_name); |
||
| 209 | continue; |
||
| 210 | } |
||
| 211 | // oppure di default uso Laravel |
||
| 212 | $storeName = $this->getStoreFromConnectionName($connection_name); |
||
| 213 | |||
| 214 | if ($storeName === null) { |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | Cache::store($storeName)->forget($key); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Invalidate cache tags. |
||
| 223 | * |
||
| 224 | * @param array $tags Array of cache tags to invalidate |
||
| 225 | */ |
||
| 226 | protected function invalidateTags(array $tags): void |
||
| 227 | { |
||
| 228 | $callback = config('super_cache_invalidate.tag_invalidation_callback'); |
||
| 229 | |||
| 230 | $groupByConnection = []; |
||
| 231 | |||
| 232 | // Anche in questo caso va fatto un loop perchè i tags potrebbero stare in database diversi, |
||
| 233 | // ma per ottimizzare possiamo raggruppare le operazioni per connessione |
||
| 234 | foreach ($tags as $tagAndConnectionName) { |
||
| 235 | // chiave e connessione |
||
| 236 | [$key, $connection] = explode('§', $tagAndConnectionName); |
||
| 237 | |||
| 238 | // Aggiungo la chiave alla connessione appropriata |
||
| 239 | $groupByConnection[$connection][] = $key; |
||
| 240 | } |
||
| 241 | if (is_callable($callback)) { |
||
| 242 | foreach ($groupByConnection as $connection_name => $arrTags) { |
||
| 243 | $callback($arrTags, $connection_name); |
||
| 244 | } |
||
| 245 | |||
| 246 | return; |
||
| 247 | } |
||
| 248 | foreach ($groupByConnection as $connection_name => $arrTags) { |
||
| 249 | $storeName = $this->getStoreFromConnectionName($connection_name); |
||
| 250 | if ($storeName === null) { |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | Cache::store($storeName)->tags($arrTags)->flush(); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Execute the console command. |
||
| 259 | */ |
||
| 260 | public function handle(): void |
||
| 294 | } |
||
| 295 | } |
||
| 296 |