6
6
use Illuminate \Support \Carbon ;
7
7
use Illuminate \Support \Facades \Cache ;
8
8
use Illuminate \Support \Facades \DB ;
9
+ use Illuminate \Support \Str ;
10
+ use Padosoft \SuperCacheInvalidate \Events \BatchCompletedEvent ;
9
11
use Padosoft \SuperCacheInvalidate \Helpers \SuperCacheInvalidationHelper ;
10
12
11
13
class ProcessCacheInvalidationEventsCommand extends Command
@@ -36,6 +38,7 @@ public function __construct(SuperCacheInvalidationHelper $helper)
36
38
private function getEventsToInvalidate (Carbon $ processingStartTime ): array
37
39
{
38
40
$ partitionCache_invalidation_events = $ this ->helper ->getCacheInvalidationEventsUnprocessedPartitionName ($ this ->shardId , $ this ->priority );
41
+
39
42
return DB ::table (DB ::raw ("`cache_invalidation_events` PARTITION ( {$ partitionCache_invalidation_events }) " ))
40
43
->select (['id ' , 'type ' , 'identifier ' , 'connection_name ' , 'partition_key ' , 'event_time ' ])
41
44
->where ('processed ' , '= ' , 0 )
@@ -131,6 +134,7 @@ protected function processEvents(): void
131
134
if (count ($ eventsToUpdate ) === 0 ) {
132
135
return ;
133
136
}
137
+
134
138
$ this ->processBatch (array_merge (...$ eventsAll ), $ eventsToUpdate );
135
139
}
136
140
@@ -139,6 +143,8 @@ protected function processBatch(array $allEvents, array $eventsToInvalidate): vo
139
143
// Separo le chiavi dai tags
140
144
$ keys = [];
141
145
$ tags = [];
146
+ // Prima di processare tutti gli eventi, assegno un batch_ID univoco a tutti gli eventi
147
+ $ batch_ID = (string ) Str::uuid ();
142
148
143
149
foreach ($ eventsToInvalidate as $ item ) {
144
150
switch ($ item ->type ) {
@@ -170,11 +176,14 @@ protected function processBatch(array $allEvents, array $eventsToInvalidate): vo
170
176
DB ::table ('cache_invalidation_events ' )
171
177
->whereIn ('id ' , array_map (fn ($ event ) => $ event ->id , $ allEvents ))
172
178
->whereIn ('partition_key ' , array_map (fn ($ event ) => $ event ->partition_key , $ allEvents ))
173
- ->update (['processed ' => 1 , 'updated_at ' => now ()])
179
+ ->update (['processed ' => 1 , 'batch_ID ' => $ batch_ID , ' updated_at ' => now ()])
174
180
;
175
181
// Riattiva i controlli
176
182
DB ::statement ('SET FOREIGN_KEY_CHECKS=1; ' );
177
183
DB ::statement ('SET UNIQUE_CHECKS=1; ' );
184
+
185
+ // A questo punto avviso il gescat che le chiavi/tags sono stati puliti, per cui può procedere alla pulizia della CDN
186
+ event (new BatchCompletedEvent ($ batch_ID , $ this ->shardId ));
178
187
}
179
188
180
189
/**
@@ -263,7 +272,7 @@ public function handle(): void
263
272
$ this ->logIf ('Starting Elaborazione ... ' . $ this ->invalidation_window );
264
273
// if (!$lockValue) {
265
274
// return;
266
- //}
275
+ // }
267
276
$ startTime = microtime (true );
268
277
try {
269
278
$ this ->processEvents ();
@@ -275,8 +284,6 @@ public function handle(): void
275
284
$ e ->getLine (),
276
285
$ e ->getTraceAsString ()
277
286
), 'error ' );
278
- } finally {
279
- // $this->helper->releaseShardLock($this->shardId, $this->priority, $lockValue, $this->connection_name);
280
287
}
281
288
$ executionTime = (microtime (true ) - $ startTime ) * 1000 ;
282
289
$ this ->logIf ('Fine Elaborazione - Tempo di esecuzione: ' . $ executionTime . ' millisec. ' );
0 commit comments