Skip to content

Commit a9b1bd8

Browse files
committed
ottimizzate insert
1 parent 0bca3d5 commit a9b1bd8

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

src/Console/ProcessCacheInvalidationEventsCommand.php

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,7 @@ private function getStoreFromConnectionName(string $connection_name): ?string
5959
return null;
6060
}
6161

62-
protected function getCache_invalidation_eventsPartitionName(int $shardId, int $priorityId): string
63-
{
64-
// Calcola il valore della partizione
65-
$shards = config('super_cache_invalidate.total_shards', 10);
66-
$priorities = [0, 1];
67-
68-
$partitionStatements = [];
69-
70-
$partitionValueId = ($priorityId * $shards) + $shardId + 1;
71-
72-
// Partitions for unprocessed events
73-
foreach ($priorities as $priority) {
74-
for ($shard = 0; $shard < $shards; $shard++) {
75-
$partitionName = "p_unprocessed_s{$shard}_p{$priority}";
76-
$partitionValue = ($priority * $shards) + $shard + 1;
77-
if ($partitionValueId < $partitionValue) {
78-
return $partitionName;
79-
}
80-
}
81-
}
8262

83-
return '';
84-
}
8563

8664
/**
8765
* Process cache invalidation events.
@@ -100,7 +78,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
10078
$invalidationWindow = config('super_cache_invalidate.invalidation_window');
10179

10280
// Fetch a batch of unprocessed events
103-
$partitionCache_invalidation_events = $this->getCache_invalidation_eventsPartitionName($shardId, $priority, 0, $processingStartTime);
81+
$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsPartitionName($shardId, $priority);
10482

10583
$events = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))
10684
//->from(DB::raw("`{$this->from}` PARTITION ({$partitionsString})"))
@@ -131,6 +109,9 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
131109
$counter = 0;
132110

133111
// Fetch associated identifiers for the events
112+
// TODO JB 31/12/2024: per adesso commentato, da riattivare quando tutto funziona alla perfezione usando la partizione
113+
$associations = collect();
114+
/*
134115
$eventIds = $events->pluck('id')->all();
135116
136117
//retrive associated identifiers related to fetched event id
@@ -140,6 +121,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
140121
->get()
141122
->groupBy('event_id')
142123
;
124+
*/
143125

144126
// Prepare list of all identifiers to fetch last invalidation times
145127
$allIdentifiers = [];
@@ -215,7 +197,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
215197

216198
// When we reach the batch size, process the accumulated identifiers
217199
if ($counter % $tagBatchSize === 0) {
218-
$this->processBatch($batchIdentifiers, $eventsToUpdate);
200+
$this->processBatch($batchIdentifiers, $eventsToUpdate, $shardId, $priority);
219201

220202
// Reset the accumulators
221203
$batchIdentifiers = [];
@@ -228,7 +210,7 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
228210
}
229211

230212
// Process any remaining identifiers in the batch
231-
$this->processBatch($batchIdentifiers, $eventsToUpdate);
213+
$this->processBatch($batchIdentifiers, $eventsToUpdate, $shardId, $priority);
232214
}
233215

234216
/**
@@ -338,7 +320,7 @@ protected function updateLastInvalidationTimes(array $identifiers): void
338320
*
339321
* @throws \Throwable
340322
*/
341-
protected function processBatch(array $batchIdentifiers, array $eventsToUpdate): void
323+
protected function processBatch(array $batchIdentifiers, array $eventsToUpdate, int $shard, int $priority): void
342324
{
343325
$maxAttempts = 5;
344326
$attempts = 0;
@@ -395,7 +377,8 @@ protected function processBatch(array $batchIdentifiers, array $eventsToUpdate):
395377

396378
try {
397379
// Mark events as processed
398-
DB::table('cache_invalidation_events')
380+
$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsPartitionName($shard, $priority);
381+
DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))
399382
->whereIn('id', $eventsToUpdate)
400383
->update(['processed' => 1])
401384
;
@@ -512,7 +495,7 @@ public function handle(): void
512495
try {
513496
$this->processEvents($shardId, $priority, $limit, $tagBatchSize, $connection_name);
514497
} catch (\Throwable $e) {
515-
$this->error('Si è verificato un errore in ' . __METHOD__ . ': ' . $e->getMessage());
498+
$this->error(now() . ' Si è verificato un errore in ' . __METHOD__ . ': ' . $e->getMessage());
516499
} finally {
517500
$this->helper->releaseShardLock($shardId, $priority, $lockValue, $connection_name);
518501
}

src/Helpers/SuperCacheInvalidationHelper.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,13 @@ public function insertInvalidationEvent(
5151

5252
try {
5353
// Cerca di bloccare il record per l'inserimento
54-
$eventId = DB::table('cache_invalidation_events')->insertGetId($data);
54+
$partitionCache_invalidation_events = $this->getCacheInvalidationEventsPartitionName($shard, $priority);
55+
56+
$eventId = DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))->insertGetId($data);
5557

5658
// Insert associated identifiers
59+
// TODO JB 31/12/2024: per adesso commentato, da riattivare quando tutto funziona alla perfezione usando la partizione
60+
/*
5761
if (!empty($associatedIdentifiers)) {
5862
$associations = [];
5963
foreach ($associatedIdentifiers as $associated) {
@@ -67,11 +71,13 @@ public function insertInvalidationEvent(
6771
}
6872
DB::table('cache_invalidation_event_associations')->insert($associations);
6973
}
74+
*/
7075
$insertOk = true;
7176
DB::commit(); // Completa la transazione
7277
} catch (\Throwable $e) {
7378
DB::rollBack(); // Annulla la transazione in caso di errore
7479
$attempts++;
80+
Log::error("SuperCacheInvalidate: impossibile eseguire insert, tentativo $attempts di $maxAttempts: " . $e->getMessage());
7581
// Logica per gestire i tentativi falliti
7682
if ($attempts >= $maxAttempts) {
7783
// Salta il record dopo il numero massimo di tentativi
@@ -123,4 +129,28 @@ public function releaseShardLock(int $shardId, int $priority, string $lockValue,
123129
Redis::connection($connection_name)->del($lockKey);
124130
}
125131
}
132+
133+
public function getCacheInvalidationEventsPartitionName(int $shardId, int $priorityId): string
134+
{
135+
// Calcola il valore della partizione
136+
$shards = config('super_cache_invalidate.total_shards', 10);
137+
$priorities = [0, 1];
138+
139+
$partitionStatements = [];
140+
141+
$partitionValueId = ($priorityId * $shards) + $shardId + 1;
142+
143+
// Partitions for unprocessed events
144+
foreach ($priorities as $priority) {
145+
for ($shard = 0; $shard < $shards; $shard++) {
146+
$partitionName = "p_unprocessed_s{$shard}_p{$priority}";
147+
$partitionValue = ($priority * $shards) + $shard + 1;
148+
if ($partitionValueId < $partitionValue) {
149+
return $partitionName;
150+
}
151+
}
152+
}
153+
154+
return '';
155+
}
126156
}

0 commit comments

Comments
 (0)