| Conditions | 13 |
| Paths | 201 |
| Total Lines | 129 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 55 | protected function processEvents(int $shardId, int $priority, int $limit, int $tagBatchSize): void |
||
| 56 | { |
||
| 57 | $processingStartTime = now(); |
||
| 58 | $invalidationWindow = config('super_cache_invalidate.invalidation_window'); |
||
| 59 | |||
| 60 | // Fetch a batch of unprocessed events |
||
| 61 | $events = DB::table('cache_invalidation_events') |
||
| 62 | ->where('processed', 0) |
||
| 63 | ->where('shard', $shardId) |
||
| 64 | ->where('priority', $priority) |
||
| 65 | ->where('event_time', '<', $processingStartTime) |
||
| 66 | ->orderBy('event_time') |
||
| 67 | ->limit($limit) |
||
| 68 | ->get() |
||
| 69 | ; |
||
| 70 | |||
| 71 | if ($events->isEmpty()) { |
||
| 72 | // No more events to process |
||
| 73 | return; |
||
| 74 | } |
||
| 75 | |||
| 76 | // Group events by type and identifier |
||
| 77 | $eventsByIdentifier = $events->groupBy(function ($event) { |
||
| 78 | return $event->type . ':' . $event->identifier; |
||
| 79 | }); |
||
| 80 | |||
| 81 | $batchIdentifiers = []; |
||
| 82 | $eventsToUpdate = []; |
||
| 83 | $counter = 0; |
||
| 84 | |||
| 85 | // Fetch associated identifiers for the events |
||
| 86 | $eventIds = $events->pluck('id')->all(); |
||
| 87 | |||
| 88 | //retrive associated identifiers related to fetched event id |
||
| 89 | $associations = DB::table('cache_invalidation_event_associations') |
||
| 90 | ->whereIn('event_id', $eventIds) |
||
| 91 | ->get() |
||
| 92 | ->groupBy('event_id') |
||
| 93 | ; |
||
| 94 | |||
| 95 | // Prepare list of all identifiers to fetch last invalidation times |
||
| 96 | $allIdentifiers = []; |
||
| 97 | |||
| 98 | foreach ($eventsByIdentifier as $key => $eventsGroup) { |
||
| 99 | $allIdentifiers[] = $key; |
||
| 100 | foreach ($eventsGroup as $event) { |
||
| 101 | $eventAssociations = $associations->get($event->id, collect()); |
||
| 102 | foreach ($eventAssociations as $assoc) { |
||
| 103 | $assocKey = $assoc->associated_type . ':' . $assoc->associated_identifier; |
||
| 104 | $allIdentifiers[] = $assocKey; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // Fetch last invalidation times in bulk |
||
| 110 | $lastInvalidationTimes = $this->getLastInvalidationTimes(array_unique($allIdentifiers)); |
||
| 111 | |||
| 112 | foreach ($eventsByIdentifier as $key => $eventsGroup) { |
||
| 113 | // Extract type and identifier |
||
| 114 | [$type, $identifier] = explode(':', $key, 2); |
||
| 115 | |||
| 116 | // Get associated identifiers for the events |
||
| 117 | $associatedIdentifiers = []; |
||
| 118 | foreach ($eventsGroup as $event) { |
||
| 119 | $eventAssociations = $associations->get($event->id, collect()); |
||
| 120 | foreach ($eventAssociations as $assoc) { |
||
| 121 | $assocKey = $assoc->associated_type . ':' . $assoc->associated_identifier; |
||
| 122 | $associatedIdentifiers[$assocKey] = [ |
||
| 123 | 'type' => $assoc->associated_type, |
||
| 124 | 'identifier' => $assoc->associated_identifier, |
||
| 125 | ]; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // Build a list of all identifiers to check |
||
| 130 | $identifiersToCheck = [$key]; |
||
| 131 | $identifiersToCheck = array_merge($identifiersToCheck, array_keys($associatedIdentifiers)); |
||
| 132 | |||
| 133 | $lastInvalidationTimesSubset = array_intersect_key($lastInvalidationTimes, array_flip($identifiersToCheck)); |
||
| 134 | |||
| 135 | $shouldInvalidate = $this->shouldInvalidateMultiple($identifiersToCheck, $lastInvalidationTimesSubset, $invalidationWindow); |
||
| 136 | |||
| 137 | if ($shouldInvalidate) { |
||
| 138 | // Proceed to invalidate |
||
| 139 | $latestEvent = $eventsGroup->last(); |
||
| 140 | |||
| 141 | // Accumulate identifiers and events |
||
| 142 | $batchIdentifiers[] = [ |
||
| 143 | 'type' => $type, |
||
| 144 | 'identifier' => $identifier, |
||
| 145 | 'event' => $latestEvent, |
||
| 146 | 'associated' => array_values($associatedIdentifiers), |
||
| 147 | ]; |
||
| 148 | |||
| 149 | // Update last invalidation times for all identifiers |
||
| 150 | $this->updateLastInvalidationTimes($identifiersToCheck); |
||
| 151 | |||
| 152 | // Mark all events in the group as processed |
||
| 153 | foreach ($eventsGroup as $event) { |
||
| 154 | $eventsToUpdate[] = $event->id; |
||
| 155 | } |
||
| 156 | } else { |
||
| 157 | // Within the invalidation window, skip invalidation |
||
| 158 | // Mark all events except the last one as processed |
||
| 159 | $eventsToProcess = $eventsGroup->slice(0, -1); |
||
| 160 | foreach ($eventsToProcess as $event) { |
||
| 161 | $eventsToUpdate[] = $event->id; |
||
| 162 | } |
||
| 163 | // The last event remains unprocessed |
||
| 164 | } |
||
| 165 | |||
| 166 | $counter++; |
||
| 167 | |||
| 168 | // When we reach the batch size, process the accumulated identifiers |
||
| 169 | if ($counter % $tagBatchSize === 0) { |
||
| 170 | $this->processBatch($batchIdentifiers, $eventsToUpdate); |
||
| 171 | |||
| 172 | // Reset the accumulators |
||
| 173 | $batchIdentifiers = []; |
||
| 174 | $eventsToUpdate = []; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | if (empty($batchIdentifiers)) { |
||
| 179 | return; |
||
| 180 | } |
||
| 181 | |||
| 182 | // Process any remaining identifiers in the batch |
||
| 183 | $this->processBatch($batchIdentifiers, $eventsToUpdate); |
||
| 184 | } |
||
| 429 |