Skip to content

Commit 9993617

Browse files
committed
ottimizzate update e insert
1 parent 894a349 commit 9993617

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed

src/Console/ProcessCacheInvalidationEventsCommand.php

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ protected function processEvents(int $shardId, int $priority, int $limit, int $t
181181

182182
// Mark all events in the group as processed
183183
foreach ($eventsGroup as $event) {
184-
$eventsToUpdate[] = $event->id;
184+
$eventsToUpdate[] = ['id' => $event->id, 'event_time' => $event->event_time, 'partition_key' => $event->partition_key];
185185
}
186186
} else {
187187
// Within the invalidation window, skip invalidation
188188
// Mark all events except the last one as processed
189189
$eventsToProcess = $eventsGroup->slice(0, -1);
190190
foreach ($eventsToProcess as $event) {
191-
$eventsToUpdate[] = $event->id;
191+
$eventsToUpdate[] = ['id' => $event->id, 'event_time' => $event->event_time, 'partition_key' => $event->partition_key];
192192
}
193193
// The last event remains unprocessed
194194
}
@@ -304,12 +304,12 @@ protected function updateLastInvalidationTimes(array $identifiers): void
304304

305305
foreach ($identifiers as $key) {
306306
[$type, $identifier] = explode(':', $key, 2);
307-
// Qui si può usare la partizione
308-
$partitionCache_invalidation_timestamps = $this->helper->getCacheInvalidationTimestampsPartitionName();
307+
// Anche qui non si può usare la partizione perchè nel caso dell'update potrebbe non essere la partizione giusta temporalmente
308+
//$partitionCache_invalidation_timestamps = $this->helper->getCacheInvalidationTimestampsPartitionName();
309309

310-
//DB::table('cache_invalidation_timestamps')
311-
DB::table(DB::raw("`cache_invalidation_timestamps` PARTITION ({$partitionCache_invalidation_timestamps})"))
312-
->updateOrInsert(
310+
DB::table('cache_invalidation_timestamps')
311+
//DB::table(DB::raw("`cache_invalidation_timestamps` PARTITION ({$partitionCache_invalidation_timestamps})"))
312+
->updateOrInsert(
313313
['identifier_type' => $type, 'identifier' => $identifier],
314314
['last_invalidated' => $now]
315315
)
@@ -377,38 +377,40 @@ protected function processBatch(array $batchIdentifiers, array $eventsToUpdate,
377377
}
378378

379379
$shards = config('super_cache_invalidate.total_shards', 10);
380-
$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsPartitionName($shard, $priority);
381-
$partitionKey = ($priority * $shards) + $shard + 1;
382-
foreach ($eventsToUpdate as $eventToUpdateId) {
383-
while ($attempts < $maxAttempts && !$updatedOk) {
384-
// Begin transaction for the batch
385-
DB::beginTransaction();
386-
try {
387-
// Disabilita i controlli delle chiavi esterne e dei vincoli univoci
388-
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
389-
DB::statement('SET UNIQUE_CHECKS=0;');
390-
// Mark event as processed
391-
DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events})"))
392-
->where('id', $eventToUpdateId)
393-
->where('partition_key', $partitionKey)
394-
->update(['processed' => 1])
395-
;
396-
// Riattiva i controlli
397-
DB::statement('SET UNIQUE_CHECKS=1;');
398-
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
399-
// Commit transaction
400-
DB::commit();
401-
$updatedOk = true;
402-
} catch (\Throwable $e) {
403-
// Rollback transaction on error
404-
DB::rollBack();
405-
$attempts++;
406-
$this->warn(now()->toDateTimeString() . ": Tentativo $attempts di $maxAttempts: " . $e->getMessage());
407-
// Logica per gestire i tentativi falliti
408-
if ($attempts >= $maxAttempts) {
409-
// Salta il record dopo il numero massimo di tentativi
410-
throw $e;
411-
}
380+
//$partitionCache_invalidation_events = $this->helper->getCacheInvalidationEventsPartitionName($shard, $priority);
381+
382+
while ($attempts < $maxAttempts && !$updatedOk) {
383+
//$partitionCache_invalidation_events_processed = $this->helper->getCacheInvalidationEventsProcessedPartitionName($shard, $priority, $eventToUpdate['event_time']);
384+
385+
// Begin transaction for the batch
386+
//DB::beginTransaction();
387+
try {
388+
// Disabilita i controlli delle chiavi esterne e dei vincoli univoci
389+
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
390+
DB::statement('SET UNIQUE_CHECKS=0;');
391+
392+
// Mark event as processed
393+
//DB::table(DB::raw("`cache_invalidation_events` PARTITION ({$partitionCache_invalidation_events}, {$partitionCache_invalidation_events_processed})"))
394+
DB::table("cache_invalidation_events")
395+
->whereIn('id', array_column($eventsToUpdate, 'id'))
396+
->whereIn('partition_key', array_column($eventsToUpdate, 'partition_key'))
397+
->update(['processed' => 1])
398+
;
399+
// Riattiva i controlli
400+
DB::statement('SET UNIQUE_CHECKS=1;');
401+
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
402+
// Commit transaction
403+
//DB::commit();
404+
$updatedOk = true;
405+
} catch (\Throwable $e) {
406+
// Rollback transaction on error
407+
//DB::rollBack();
408+
$attempts++;
409+
$this->warn(now()->toDateTimeString() . ": Tentativo $attempts di $maxAttempts: " . $e->getMessage());
410+
// Logica per gestire i tentativi falliti
411+
if ($attempts >= $maxAttempts) {
412+
// Salta il record dopo il numero massimo di tentativi
413+
throw $e;
412414
}
413415
}
414416
}

src/Helpers/SuperCacheInvalidationHelper.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Facades\Log;
77
use Illuminate\Support\Facades\Redis;
8-
8+
use \Illuminate\Support\Carbon;
99
class SuperCacheInvalidationHelper
1010
{
1111
/**
@@ -47,7 +47,7 @@ public function insertInvalidationEvent(
4747
$insertOk = false;
4848

4949
while ($attempts < $maxAttempts && !$insertOk) {
50-
DB::beginTransaction();
50+
//DB::beginTransaction();
5151

5252
try {
5353
// Cerca di bloccare il record per l'inserimento
@@ -74,9 +74,9 @@ public function insertInvalidationEvent(
7474
}
7575
*/
7676
$insertOk = true;
77-
DB::commit(); // Completa la transazione
77+
//DB::commit(); // Completa la transazione
7878
} catch (\Throwable $e) {
79-
DB::rollBack(); // Annulla la transazione in caso di errore
79+
//DB::rollBack(); // Annulla la transazione in caso di errore
8080
$attempts++;
8181
Log::error("SuperCacheInvalidate: impossibile eseguire insert, tentativo $attempts di $maxAttempts: " . $e->getMessage());
8282
// Logica per gestire i tentativi falliti
@@ -153,7 +153,7 @@ public function getCacheInvalidationEventsPartitionName(int $shardId, int $prior
153153
return '';
154154
}
155155

156-
public function getCacheInvalidationTimestampsPartitionName(): string
156+
public function getCacheInvalidationTimestampsPartitionName(Carbon $event_time): string
157157
{
158158
$now = now();
159159
$partitionValueId = ($now->year * 100 + $now->weekOfYear) + 1;
@@ -171,4 +171,30 @@ public function getCacheInvalidationTimestampsPartitionName(): string
171171
}
172172
return '';
173173
}
174+
175+
public function getCacheInvalidationEventsProcessedPartitionName(int $shardId, int $priorityId, string $event_time): string
176+
{
177+
// Calcola il valore della partizione
178+
$shards = config('super_cache_invalidate.total_shards', 10);
179+
$priorities = [0, 1];
180+
$eventTime = Carbon::parse($event_time);
181+
$partitionValueId = ($eventTime->year * 10000) + ($eventTime->weekOfYear * 100) + ($priorityId * $shards) + $shardId;
182+
// Partitions for processed events
183+
for ($year = $eventTime->year; $year <= ($eventTime->year+1); $year++) {
184+
for ($week = $eventTime->weekOfYear; $week <= $eventTime->weekOfYear+1; $week++) {
185+
foreach ($priorities as $priority) {
186+
for ($shard = 0; $shard < $shards; $shard++) {
187+
$partitionKey = ($year * 10000) + ($week * 100) + ($priority * $shards) + $shard;
188+
$partitionValue = $partitionKey + 1;
189+
$partitionName = "p_s{$shard}_p{$priority}_{$year}w{$week}";
190+
if ($partitionValueId < $partitionValue) {
191+
return $partitionName;
192+
}
193+
}
194+
}
195+
}
196+
}
197+
198+
return '';
199+
}
174200
}

0 commit comments

Comments
 (0)